Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalIn enc_pb (p5); 00004 InterruptIn enc_A (p6); 00005 InterruptIn enc_B (p7); 00006 PwmOut led1 (p22); 00007 PwmOut led2 (p23); 00008 00009 void enc_int_routine (void); 00010 00011 bool enc_A_state; 00012 bool enc_B_state; 00013 bool old_enc_A_state; 00014 bool old_enc_B_state; 00015 int val_min = 0; 00016 int val_max = 100; 00017 int val = ((val_max - val_min) / 2) + val_min; 00018 int val_old = val; 00019 00020 int main() { 00021 enc_pb.mode(PullUp); 00022 enc_A.mode(PullUp); 00023 enc_B.mode(PullUp); 00024 enc_A_state = enc_A; 00025 enc_B_state = enc_B; 00026 enc_A.rise(&enc_int_routine); 00027 enc_B.rise(&enc_int_routine); 00028 enc_A.fall(&enc_int_routine); 00029 enc_B.fall(&enc_int_routine); 00030 while (1) { 00031 if (val != val_old) { 00032 printf("\n\r%i",val); 00033 } // if (val != val_old) 00034 val_old = val; 00035 if (enc_pb) { 00036 led1 = val / 100.0; 00037 led2 = 1.0 - (val / 100.0); 00038 } else { 00039 printf("button\n\r"); 00040 led1 = 0; 00041 led2 = 0; 00042 } // if (enc_pb) 00043 } // while(1) 00044 } // end main 00045 00046 void enc_int_routine (void) { 00047 old_enc_A_state = enc_A_state; 00048 old_enc_B_state = enc_B_state; 00049 enc_A_state = enc_A; 00050 enc_B_state = enc_B; 00051 if (((enc_A_state != old_enc_A_state) && (enc_B_state != old_enc_B_state)) || ((enc_A_state == old_enc_A_state) && (enc_B_state == old_enc_B_state))) { 00052 val = val; 00053 } else { 00054 if (enc_A_state ^ old_enc_B_state) { 00055 val -= 1; 00056 } else { 00057 val += 1; 00058 } // (enc_A_state ^ old_enc_B_state) 00059 } // if ((enc_A_state != old_enc_A_state) && (enc_B_state != old_enc_B_state)) 00060 } // end enc_A_int_routine
Generated on Wed Jul 27 2022 08:35:49 by
1.7.2