Sik Chiu Chow
/
encoder
encoder
encoder01.cpp@2:92fd61600fa8, 2021-10-31 (annotated)
- Committer:
- ea78anana
- Date:
- Sun Oct 31 09:45:26 2021 +0000
- Revision:
- 2:92fd61600fa8
for 3 encoders
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ea78anana | 2:92fd61600fa8 | 1 | #include "mbed.h" |
ea78anana | 2:92fd61600fa8 | 2 | #include "QEI.h" |
ea78anana | 2:92fd61600fa8 | 3 | |
ea78anana | 2:92fd61600fa8 | 4 | QEI encoder1 (D14,D15,NC,2500); |
ea78anana | 2:92fd61600fa8 | 5 | QEI encoder2 (D8,D9,NC,2500); |
ea78anana | 2:92fd61600fa8 | 6 | QEI encoder3 (D4,D5,NC,2500); |
ea78anana | 2:92fd61600fa8 | 7 | Serial pc(USBTX, USBRX); |
ea78anana | 2:92fd61600fa8 | 8 | int counter1 = 0; |
ea78anana | 2:92fd61600fa8 | 9 | int cur_state1 = 0; |
ea78anana | 2:92fd61600fa8 | 10 | int counter2 = 0; |
ea78anana | 2:92fd61600fa8 | 11 | int cur_state2 = 0; |
ea78anana | 2:92fd61600fa8 | 12 | int counter3 = 0; |
ea78anana | 2:92fd61600fa8 | 13 | int cur_state3 = 0; |
ea78anana | 2:92fd61600fa8 | 14 | |
ea78anana | 2:92fd61600fa8 | 15 | int main(void) { |
ea78anana | 2:92fd61600fa8 | 16 | while(1){ |
ea78anana | 2:92fd61600fa8 | 17 | counter1 = encoder1.getPulses(); |
ea78anana | 2:92fd61600fa8 | 18 | counter2 = encoder2.getPulses(); |
ea78anana | 2:92fd61600fa8 | 19 | counter3 = encoder3.getPulses(); |
ea78anana | 2:92fd61600fa8 | 20 | if( counter1 >= cur_state1){ |
ea78anana | 2:92fd61600fa8 | 21 | cur_state1 = counter1; |
ea78anana | 2:92fd61600fa8 | 22 | printf("1: %d ", cur_state1); |
ea78anana | 2:92fd61600fa8 | 23 | }else if(counter1 < cur_state1){ |
ea78anana | 2:92fd61600fa8 | 24 | cur_state1 = counter1; |
ea78anana | 2:92fd61600fa8 | 25 | printf("1: %d ", cur_state1); |
ea78anana | 2:92fd61600fa8 | 26 | }; |
ea78anana | 2:92fd61600fa8 | 27 | |
ea78anana | 2:92fd61600fa8 | 28 | if( counter2 >= cur_state2){ |
ea78anana | 2:92fd61600fa8 | 29 | cur_state2 = counter2; |
ea78anana | 2:92fd61600fa8 | 30 | printf("2: %d ", cur_state2); |
ea78anana | 2:92fd61600fa8 | 31 | }else if(counter2 < cur_state2){ |
ea78anana | 2:92fd61600fa8 | 32 | cur_state2 = counter2; |
ea78anana | 2:92fd61600fa8 | 33 | printf("2: %d ", cur_state2); |
ea78anana | 2:92fd61600fa8 | 34 | }; |
ea78anana | 2:92fd61600fa8 | 35 | |
ea78anana | 2:92fd61600fa8 | 36 | if( counter3 >= cur_state3){ |
ea78anana | 2:92fd61600fa8 | 37 | cur_state3 = counter3; |
ea78anana | 2:92fd61600fa8 | 38 | printf("3: %d ", cur_state3); |
ea78anana | 2:92fd61600fa8 | 39 | }else if(counter3 < cur_state3){ |
ea78anana | 2:92fd61600fa8 | 40 | cur_state3 = counter3; |
ea78anana | 2:92fd61600fa8 | 41 | printf("3: %d ", cur_state3); |
ea78anana | 2:92fd61600fa8 | 42 | } |
ea78anana | 2:92fd61600fa8 | 43 | } |
ea78anana | 2:92fd61600fa8 | 44 | } |