This drive is working well with "LPD3806-600BM-G5-24G", and have Simple program

Dependencies:   mbed

Rotary_Encoder.cpp

Committer:
Yifan_Du
Date:
2019-02-02
Revision:
0:b2ffb830539c
Child:
3:6a91d523b146

File content as of revision 0:b2ffb830539c:

#include "Rotary_Encoder.h"

Rotary_Encoder::Rotary_Encoder(PinName White, PinName Green) : Green_Pin(Green), White_Pin(White)
{
    Green_Pin.rise(this, &Rotary_Encoder::Green_Pin_Rise);
    White_Pin.rise(this, &Rotary_Encoder::White_Pin_Rise);
}

void Rotary_Encoder::Green_Pin_Rise(void)
{
    if(White_Pin.read() == 0)
        Encoder_Counter++;
    else
        Encoder_Counter--;
}
    
void Rotary_Encoder::White_Pin_Rise(void)
{
    if(Green_Pin.read() == 0)
        Encoder_Counter--;
    else
        Encoder_Counter++;
}

char *Rotary_Encoder::Calculate_Direction(void)
{
    // Data processing    -2^32< "int" <2^32-1
    while(Encoder_Counter < -1275)
        Encoder_Counter = Encoder_Counter + 1200;
    while(Encoder_Counter > 1275)
        Encoder_Counter = Encoder_Counter - 1200;
    
    // Calculate the direction
    if(((-1275<=Encoder_Counter)&&(Encoder_Counter<=-1126)) ||  ((-76 <= Encoder_Counter)&&(Encoder_Counter <= 75)) || ((1126 <= Encoder_Counter)&&(Encoder_Counter <= 1275)))
        strcpy(Direction,"N");  // N 北
    else if (((76 <= Encoder_Counter)&&(Encoder_Counter <= 225)) || ((-1126 <= Encoder_Counter)&&(Encoder_Counter <= -975)))
        strcpy(Direction,"NE"); // NE 东北
    else if (((226 <= Encoder_Counter)&&(Encoder_Counter <= 375)) || ((-976 <= Encoder_Counter)&&(Encoder_Counter <= -825)))
        strcpy(Direction,"E");  // E  东
    else if (((376 <= Encoder_Counter)&&(Encoder_Counter <= 525)) || ((-826 <= Encoder_Counter)&&(Encoder_Counter <= -675)))
        strcpy(Direction,"SE"); // SE 东南
    else if (((526 <= Encoder_Counter)&&(Encoder_Counter <= 675)) || ((-676 <= Encoder_Counter)&&(Encoder_Counter <= -525)))
        strcpy(Direction,"S");  // S  南
    else if (((676 <= Encoder_Counter)&&(Encoder_Counter <= 825)) || ((-526 <= Encoder_Counter)&&(Encoder_Counter <= -375)))
        strcpy(Direction,"SW"); // SW 西南
    else if (((826 <= Encoder_Counter)&&(Encoder_Counter <= 975)) || ((-376 <= Encoder_Counter)&&(Encoder_Counter <= -225)))
        strcpy(Direction,"W");  // W  西
    else if (((927 <= Encoder_Counter)&&(Encoder_Counter <= 1125)) || ((-226 <= Encoder_Counter)&&(Encoder_Counter <= -75)))
        strcpy(Direction,"NW"); // NW 西北
    
    return Direction;
}