Rotarary control pulse generators

Dependencies:   PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 //See http://makeatronics.blogspot.com/2013/02/efficiently-reading-quadrature-with.html
00004 //for a detailed explanation of the RPG encoder counting algorithm
00005 //uses Sparkfun RPG with RGB led on breakout board (#15141,11722,10597)
00006 //place RPG PCB across a breadboard power bus strip for easier pin hookup!
00007 InterruptIn RPG_A(p14,PullUp);//encoder A and B pins/bits use interrupts
00008 InterruptIn RPG_B(p15,PullUp);
00009 PinDetect RPG_PB(p16); //encode pushbutton switch "SW" on PCB
00010 //PWM setup for RGB LED in enocder
00011 PwmOut red(p21);//"R" pin
00012 PwmOut blue(p22);//"G" pin
00013 PwmOut green(p23);//"B" pin
00014 //Note: also tie RPG PCB "C" pin to ground, "+" pin to 3.3
00015 //mbed status leds
00016 DigitalOut ledPB(LED1);
00017 DigitalOut red_adjust_mode(LED2);
00018 DigitalOut green_adjust_mode(LED3);
00019 DigitalOut blue_adjust_mode(LED4);
00020 //Serial pc(USBTX,USBRX);
00021 volatile int old_enc = 0;
00022 volatile int new_enc = 0;
00023 volatile int enc_count = 0;
00024 //Instead of a slow 16 case statement use a faster table lookup of truth table
00025 //index table with (previous encoder AB value<<2 | current current encoder AB value) 
00026 //to find -1(CCW),0,or 1(CW) change in count each time a bit changes state
00027 //Always want Interrupt routines to run fast!
00028 //const puts data in read only Flash instead of RAM
00029 const int lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
00030 //Encoder bit change interrupt service routine
00031 //called whenever one of the two A,B encoder bits change state
00032 void Enc_change_ISR(void)
00033 {
00034     new_enc = RPG_A<<1 | RPG_B;//current encoder bits
00035     //check truth table for -1,0 or +1 added to count
00036     enc_count = enc_count + lookup_table[old_enc<<2 | new_enc];
00037     old_enc = new_enc;
00038 }
00039 //debounced RPG pushbutton switch callback
00040 void PB_callback(void)
00041 {
00042     ledPB= !ledPB;
00043 }
00044 int main()
00045 {
00046 //turn off built-in RPG encoder RGB led
00047     red = 1.0;//PWM value 1.0 is "off", 0.0 is full "on"
00048     green = 1.0;
00049     blue = 1.0;
00050 //current color adjust set to red
00051     red_adjust_mode = 1;
00052 //debounce RPG center pushbutton
00053     RPG_PB.mode(PullDown);
00054     RPG_PB.attach_deasserted(&PB_callback);
00055     RPG_PB.setSampleFrequency();
00056 // generate an interrupt on any change in either encoder bit (A or B)
00057     RPG_A.rise(&Enc_change_ISR);
00058     RPG_A.fall(&Enc_change_ISR);
00059     RPG_B.rise(&Enc_change_ISR);
00060     RPG_B.fall(&Enc_change_ISR);
00061     while (true) {
00062         // Scale/limit count to 0..100
00063         if (enc_count>100) enc_count = 100;
00064         if (enc_count<0) enc_count = 0;
00065         red = 1.0 - enc_count/100.0;
00066 //        pc.printf("%D\n\r",enc_count);
00067     }
00068 }