
QEI_X1_LCD_test3
Dependencies: QEI TextLCD USBDevice mbed
main.cpp@0:b32c7162cd9a, 2014-08-29 (annotated)
- Committer:
- toucyy
- Date:
- Fri Aug 29 07:10:48 2014 +0000
- Revision:
- 0:b32c7162cd9a
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toucyy | 0:b32c7162cd9a | 1 | #include "mbed.h" |
toucyy | 0:b32c7162cd9a | 2 | #include "TextLCD.h" |
toucyy | 0:b32c7162cd9a | 3 | #include "QEI.h" |
toucyy | 0:b32c7162cd9a | 4 | #include "USBSerial.h" |
toucyy | 0:b32c7162cd9a | 5 | #define ROTATE_PER_REVOLUTIONS 48 |
toucyy | 0:b32c7162cd9a | 6 | |
toucyy | 0:b32c7162cd9a | 7 | Ticker timer1; |
toucyy | 0:b32c7162cd9a | 8 | Ticker timer2; |
toucyy | 0:b32c7162cd9a | 9 | Serial pc(USBTX, USBRX); // tx, rx pc hyper terminal |
toucyy | 0:b32c7162cd9a | 10 | Serial device(p9, p10); // tx, rx for opt fader xq |
toucyy | 0:b32c7162cd9a | 11 | TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7 lcd |
toucyy | 0:b32c7162cd9a | 12 | QEI wheel1(p16, p17, NC, ROTATE_PER_REVOLUTIONS, QEI::X4_ENCODING); //encoder |
toucyy | 0:b32c7162cd9a | 13 | QEI wheel2(p21, p22, NC, ROTATE_PER_REVOLUTIONS, QEI::X4_ENCODING); //encoder |
toucyy | 0:b32c7162cd9a | 14 | DigitalOut led1(LED1); |
toucyy | 0:b32c7162cd9a | 15 | DigitalOut led2(LED2); |
toucyy | 0:b32c7162cd9a | 16 | DigitalOut led3(LED3); |
toucyy | 0:b32c7162cd9a | 17 | DigitalOut led4(LED4); |
toucyy | 0:b32c7162cd9a | 18 | //BusOut myleds(LED1, LED2, LED3, LED4); |
toucyy | 0:b32c7162cd9a | 19 | |
toucyy | 0:b32c7162cd9a | 20 | int buf,cfd,predata,pulse1,pulse2; |
toucyy | 0:b32c7162cd9a | 21 | |
toucyy | 0:b32c7162cd9a | 22 | void disp() { |
toucyy | 0:b32c7162cd9a | 23 | lcd.locate(0,0); |
toucyy | 0:b32c7162cd9a | 24 | lcd.printf("ec1:%03d ec2:%03d\n", pulse1,pulse2); |
toucyy | 0:b32c7162cd9a | 25 | lcd.locate(0,1); |
toucyy | 0:b32c7162cd9a | 26 | lcd.printf("cfd:%03d\n", cfd); |
toucyy | 0:b32c7162cd9a | 27 | //pc.printf("encoder1: %02d\n\r", pulse1); |
toucyy | 0:b32c7162cd9a | 28 | //pc.printf("encoder2: %02d\n\r", pulse2); |
toucyy | 0:b32c7162cd9a | 29 | //pc.printf("cfd:%03d\n\r", cfd); |
toucyy | 0:b32c7162cd9a | 30 | led2=!led2; //check |
toucyy | 0:b32c7162cd9a | 31 | } |
toucyy | 0:b32c7162cd9a | 32 | |
toucyy | 0:b32c7162cd9a | 33 | void encoder() { |
toucyy | 0:b32c7162cd9a | 34 | pulse1 = wheel1.getPulses()/4; |
toucyy | 0:b32c7162cd9a | 35 | pulse2 = wheel2.getPulses()/4; |
toucyy | 0:b32c7162cd9a | 36 | led4=!led4; //check |
toucyy | 0:b32c7162cd9a | 37 | } |
toucyy | 0:b32c7162cd9a | 38 | |
toucyy | 0:b32c7162cd9a | 39 | int main() { |
toucyy | 0:b32c7162cd9a | 40 | device.format(7, Serial::None, 1); |
toucyy | 0:b32c7162cd9a | 41 | device.baud(30400); |
toucyy | 0:b32c7162cd9a | 42 | //pc.baud(921600); |
toucyy | 0:b32c7162cd9a | 43 | pc.printf("Opt Feder X1"); |
toucyy | 0:b32c7162cd9a | 44 | lcd.printf("Opt Feder X1"); |
toucyy | 0:b32c7162cd9a | 45 | wait(1); |
toucyy | 0:b32c7162cd9a | 46 | timer1.attach_us(&disp, 50000); //100ms |
toucyy | 0:b32c7162cd9a | 47 | timer2.attach_us(&encoder,10); //10ms |
toucyy | 0:b32c7162cd9a | 48 | while(1) { |
toucyy | 0:b32c7162cd9a | 49 | led3=!led3;//check |
toucyy | 0:b32c7162cd9a | 50 | if(device.readable()) { |
toucyy | 0:b32c7162cd9a | 51 | buf = device.getc(); |
toucyy | 0:b32c7162cd9a | 52 | if(buf == 127) |
toucyy | 0:b32c7162cd9a | 53 | { //to aboid freeze issue at 127 not fixed. |
toucyy | 0:b32c7162cd9a | 54 | buf = device.getc(); |
toucyy | 0:b32c7162cd9a | 55 | } |
toucyy | 0:b32c7162cd9a | 56 | cfd = buf; |
toucyy | 0:b32c7162cd9a | 57 | if(buf != predata) |
toucyy | 0:b32c7162cd9a | 58 | { |
toucyy | 0:b32c7162cd9a | 59 | if(buf >= 3) |
toucyy | 0:b32c7162cd9a | 60 | { |
toucyy | 0:b32c7162cd9a | 61 | led1 = 1; |
toucyy | 0:b32c7162cd9a | 62 | } |
toucyy | 0:b32c7162cd9a | 63 | else{ |
toucyy | 0:b32c7162cd9a | 64 | led1 =0; |
toucyy | 0:b32c7162cd9a | 65 | } |
toucyy | 0:b32c7162cd9a | 66 | //cfd = buf; |
toucyy | 0:b32c7162cd9a | 67 | predata = buf; |
toucyy | 0:b32c7162cd9a | 68 | |
toucyy | 0:b32c7162cd9a | 69 | }//if(x1 != predata) |
toucyy | 0:b32c7162cd9a | 70 | |
toucyy | 0:b32c7162cd9a | 71 | }//if(device.readable()) |
toucyy | 0:b32c7162cd9a | 72 | }//while(1) |
toucyy | 0:b32c7162cd9a | 73 | }//main |