init

Dependencies:   PinDetect mbed

Committer:
emanuel22e
Date:
Fri Oct 14 19:30:56 2022 +0000
Revision:
0:319e7e166ef4
Test 1

Who changed what in which revision?

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