Mbed LPC 1114 + Rotary Encoder

Mbed LPC 1114 + Rotary Encoder. Connect A ,B phrase of Rotary encoder to dp25 and dp26 of MCU, and pull up them. 

 Fig.1 Rotary Encoder

Fig.2 LPC 1114 MCU

Code

#include “mbed.h”
Serial device(dp16,dp15);
InterruptIn a_phrase(dp25);
InterruptIn b_
phrase(dp26);
uint8_t a,b,ab,c;
uint8_t pre_ab=0;
float count_=0;
void cal_pulse() {
    a = a_pharse;
    b = b_pharse;
    ab=b<<1|a;
    c=ab<<2|pre_ab;
    pre_ab=ab;
    if((c%5)!=0){
        c=c|0x06;
        if(c%3==0){
            count_+=1;   
        }
        else if(c%3!=0){
            count_-=1;
        }
    }
}
int main() {
    device.baud(9600);
    a_
phrase.fall(&cal_pulse);
    a_
phrase.rise(&cal_pulse);
    b_
phrase.fall(&cal_pulse);
    b_
phrase.rise(&cal_pulse);
    while(1) {      
        device.printf(“%2.2lfn”,count_);
        wait(0.01);
    }
}

Sponsored Links: