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@1:5c21ef62c975, 2022-01-13 (annotated)
- Committer:
- 4180_1
- Date:
- Thu Jan 13 14:29:07 2022 +0000
- Revision:
- 1:5c21ef62c975
- Parent:
- 0:3eea8ad2dbbc
ver 1.1 put lookup table in Flash using const; Added a few more comments
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 4180_1 | 0:3eea8ad2dbbc | 1 | #include "mbed.h" |
| 4180_1 | 0:3eea8ad2dbbc | 2 | #include "PinDetect.h" |
| 4180_1 | 0:3eea8ad2dbbc | 3 | //See http://makeatronics.blogspot.com/2013/02/efficiently-reading-quadrature-with.html |
| 4180_1 | 0:3eea8ad2dbbc | 4 | //for a detailed explanation of the RPG encoder counting algorithm |
| 4180_1 | 0:3eea8ad2dbbc | 5 | //uses Sparkfun RPG with RGB led on breakout board (#15141,11722,10597) |
| 4180_1 | 0:3eea8ad2dbbc | 6 | //place RPG PCB across a breadboard power bus strip for easier pin hookup! |
| 4180_1 | 0:3eea8ad2dbbc | 7 | InterruptIn RPG_A(p14,PullUp);//encoder A and B pins/bits use interrupts |
| 4180_1 | 0:3eea8ad2dbbc | 8 | InterruptIn RPG_B(p15,PullUp); |
| 4180_1 | 0:3eea8ad2dbbc | 9 | PinDetect RPG_PB(p16); //encode pushbutton switch "SW" on PCB |
| 4180_1 | 0:3eea8ad2dbbc | 10 | //PWM setup for RGB LED in enocder |
| 4180_1 | 0:3eea8ad2dbbc | 11 | PwmOut red(p21);//"R" pin |
| 4180_1 | 0:3eea8ad2dbbc | 12 | PwmOut blue(p22);//"G" pin |
| 4180_1 | 0:3eea8ad2dbbc | 13 | PwmOut green(p23);//"B" pin |
| 4180_1 | 0:3eea8ad2dbbc | 14 | //Note: also tie RPG PCB "C" pin to ground, "+" pin to 3.3 |
| 4180_1 | 0:3eea8ad2dbbc | 15 | //mbed status leds |
| 4180_1 | 0:3eea8ad2dbbc | 16 | DigitalOut ledPB(LED1); |
| 4180_1 | 0:3eea8ad2dbbc | 17 | DigitalOut red_adjust_mode(LED2); |
| 4180_1 | 0:3eea8ad2dbbc | 18 | DigitalOut green_adjust_mode(LED3); |
| 4180_1 | 0:3eea8ad2dbbc | 19 | DigitalOut blue_adjust_mode(LED4); |
| 4180_1 | 0:3eea8ad2dbbc | 20 | //Serial pc(USBTX,USBRX); |
| 4180_1 | 0:3eea8ad2dbbc | 21 | volatile int old_enc = 0; |
| 4180_1 | 0:3eea8ad2dbbc | 22 | volatile int new_enc = 0; |
| 4180_1 | 0:3eea8ad2dbbc | 23 | volatile int enc_count = 0; |
| 4180_1 | 0:3eea8ad2dbbc | 24 | //Instead of a slow 16 case statement use a faster table lookup of truth table |
| 4180_1 | 1:5c21ef62c975 | 25 | //index table with (previous encoder AB value<<2 | current current encoder AB value) |
| 4180_1 | 1:5c21ef62c975 | 26 | //to find -1(CCW),0,or 1(CW) change in count each time a bit changes state |
| 4180_1 | 1:5c21ef62c975 | 27 | //Always want Interrupt routines to run fast! |
| 4180_1 | 1:5c21ef62c975 | 28 | //const puts data in read only Flash instead of RAM |
| 4180_1 | 1:5c21ef62c975 | 29 | const int lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; |
| 4180_1 | 0:3eea8ad2dbbc | 30 | //Encoder bit change interrupt service routine |
| 4180_1 | 1:5c21ef62c975 | 31 | //called whenever one of the two A,B encoder bits change state |
| 4180_1 | 0:3eea8ad2dbbc | 32 | void Enc_change_ISR(void) |
| 4180_1 | 0:3eea8ad2dbbc | 33 | { |
| 4180_1 | 1:5c21ef62c975 | 34 | new_enc = RPG_A<<1 | RPG_B;//current encoder bits |
| 4180_1 | 1:5c21ef62c975 | 35 | //check truth table for -1,0 or +1 added to count |
| 4180_1 | 0:3eea8ad2dbbc | 36 | enc_count = enc_count + lookup_table[old_enc<<2 | new_enc]; |
| 4180_1 | 0:3eea8ad2dbbc | 37 | old_enc = new_enc; |
| 4180_1 | 0:3eea8ad2dbbc | 38 | } |
| 4180_1 | 1:5c21ef62c975 | 39 | //debounced RPG pushbutton switch callback |
| 4180_1 | 0:3eea8ad2dbbc | 40 | void PB_callback(void) |
| 4180_1 | 0:3eea8ad2dbbc | 41 | { |
| 4180_1 | 0:3eea8ad2dbbc | 42 | ledPB= !ledPB; |
| 4180_1 | 0:3eea8ad2dbbc | 43 | } |
| 4180_1 | 0:3eea8ad2dbbc | 44 | int main() |
| 4180_1 | 0:3eea8ad2dbbc | 45 | { |
| 4180_1 | 0:3eea8ad2dbbc | 46 | //turn off built-in RPG encoder RGB led |
| 4180_1 | 1:5c21ef62c975 | 47 | red = 1.0;//PWM value 1.0 is "off", 0.0 is full "on" |
| 4180_1 | 0:3eea8ad2dbbc | 48 | green = 1.0; |
| 4180_1 | 0:3eea8ad2dbbc | 49 | blue = 1.0; |
| 4180_1 | 0:3eea8ad2dbbc | 50 | //current color adjust set to red |
| 4180_1 | 0:3eea8ad2dbbc | 51 | red_adjust_mode = 1; |
| 4180_1 | 0:3eea8ad2dbbc | 52 | //debounce RPG center pushbutton |
| 4180_1 | 0:3eea8ad2dbbc | 53 | RPG_PB.mode(PullDown); |
| 4180_1 | 0:3eea8ad2dbbc | 54 | RPG_PB.attach_deasserted(&PB_callback); |
| 4180_1 | 0:3eea8ad2dbbc | 55 | RPG_PB.setSampleFrequency(); |
| 4180_1 | 0:3eea8ad2dbbc | 56 | // generate an interrupt on any change in either encoder bit (A or B) |
| 4180_1 | 0:3eea8ad2dbbc | 57 | RPG_A.rise(&Enc_change_ISR); |
| 4180_1 | 0:3eea8ad2dbbc | 58 | RPG_A.fall(&Enc_change_ISR); |
| 4180_1 | 0:3eea8ad2dbbc | 59 | RPG_B.rise(&Enc_change_ISR); |
| 4180_1 | 0:3eea8ad2dbbc | 60 | RPG_B.fall(&Enc_change_ISR); |
| 4180_1 | 0:3eea8ad2dbbc | 61 | while (true) { |
| 4180_1 | 0:3eea8ad2dbbc | 62 | // Scale/limit count to 0..100 |
| 4180_1 | 0:3eea8ad2dbbc | 63 | if (enc_count>100) enc_count = 100; |
| 4180_1 | 0:3eea8ad2dbbc | 64 | if (enc_count<0) enc_count = 0; |
| 4180_1 | 0:3eea8ad2dbbc | 65 | red = 1.0 - enc_count/100.0; |
| 4180_1 | 0:3eea8ad2dbbc | 66 | // pc.printf("%D\n\r",enc_count); |
| 4180_1 | 0:3eea8ad2dbbc | 67 | } |
| 4180_1 | 0:3eea8ad2dbbc | 68 | } |