napoleon leoni / PinDetect_Debounce_Library_Test

Dependencies:   C12832_lcd PinDetect mbed

Fork of Bootcamp-Pushbutton_Debounce_Interrupt by avnish aggarwal

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 #include "C12832_lcd.h"
00004 
00005 //Napoleon Leoni
00006 //***************************************************************************//
00007 //Test program for debounce on interrupt. Here I test the PinDetect Library  //
00008 //To check whether indeed the library is attaching the debounced pin to an   //
00009 //interrupt. The conclusion as shown below is that it is NOT!, the PinDetect //
00010 //has some nice features like key_pressed or key_pressedheld detection       //
00011 //but they are all working on polling the specified debounced pin on a Timer //
00012 //interrupt, which by default is set every 20 ms. This is not the same as    //
00013 //trigering an Interrupt on a leading or trailing edge of GPIO pin           //
00014 //
00015 //
00016 //                  TESTS RUN                                               //
00017 // I print to the LCD three important LPC1768 registers: 
00018 // ISER0..Interrupt set Enable register(address 0xE000E100), for this program 
00019 // the important bits are #4..Timer 3 interrupt and #21 External interrupt 3. 
00020 // Note that all GPIO interrupts are attached to the External Interrupt 3 
00021 // hardware EINT3. contents of ISER0 are accesible via: *(NVIC->ISER)
00022 //
00023 // IntEnR..GPIO interrupt enable on rising, shows which pins are attached to EINT3 
00024 // on their rising edge (these are only for mbed pins mapped to LPC pins on Port 0)
00025 // its contents are available using th macro LPC_GPIOINT->IO0IntEnR
00026 // IntEnF..GPIO interrupt enable on falling, shows which pins are attached to EINT3 
00027 // on their falling edge (these are only for mbed pins mapped to LPC pins on Port 0)
00028 // its contents are available using th macro LPC_GPIOINT->IO0IntEnF
00029 // 
00030 // Note the mapping below of MBED pins to the IntEnR/IntEnF bits:
00031 // IntEnR/IntEnF bit    LPC GPIO       MBED GPIO PIN   
00032 // 0                    P[0].0          p9                              
00033 // 1                    P[0].1          p10
00034 // 4                    P[0].4          p30
00035 // 5                    P[0].5          p29
00036 // 6                    P[0].6          p8
00037 // 7                    P[0].7          p7
00038 // 8                    P[0].8          p6
00039 // 9                    P[0].9          p5
00040 // 10                   P[0].10         p28
00041 // 11                   P[0].11         p27
00042 // 12                   P[0].12         p27
00043 // 15                   P[0].15         p13
00044 // 16                   P[0].16         p14
00045 // 17                   P[0].17         p12
00046 // 18                   P[0].18         p11
00047 // 23                   P[0].23         p15
00048 // 24                   P[0].24         p16
00049 // 25                   P[0].25         p17
00050 // 26                   P[0].26         p18
00051 // 30                   P[0].30         p19
00052 // 31                   P[0].31         p20
00053 //***********************************************************************************
00054 //   Test description                 Resulting Register Values                 Analysis
00055 // 1 DIRECTINT not defined            ISER0       0x0000 0010                   No GPIO interrupts enabled nor attached
00056 //   so only pinDetect(14)            IO0IntEnR   0x0000 0000                   only TIMER 3 Interrupt enabled so the pinDetect
00057 //   is used                          IO0IntEnF   0x0000 0000                   libary is simply polling the pin on a timer interrupt and 
00058 //                                                                              debouncing it. 
00059 //
00060 // 2 DIRECTINT defined                ISER0       0x0020 0010(when enabled)     GPIO interrupt is enabled and disabled periodically     
00061 //                                                0x0000 0010(when disabled)    when disabled the joystick up/down motion does not trigger
00062 //   so we define InterruptIn         IO0IntEnR   0x0080 0000                   LED1 as expected, however the pindetect functionality continues
00063 //   on pins 12 and 15                IO0IntEnF   0x0002 0000                   to work a further confirmation that the library is not working with
00064 //                                                                              an interrupt on the GPIO specified pin. Note that p15 is attached to 
00065 //                                                                              a rising edge GPIO interrupt and p12 to a falling edge interrupt.
00066 //***********************************************************************************
00067 
00068 #define DIRECTINT //constant to compare interrupt registers when using pindetect vs. using InterrupIn library
00069 
00070 DigitalOut myled(LED1);
00071 DigitalOut myled2(LED2);
00072 DigitalOut myled3(LED3);
00073 DigitalOut myled4(LED4);
00074 C12832_LCD lcd;
00075 
00076 #ifdef DIRECTINT
00077 InterruptIn down(p12);  // DOWN of joystickn note that mbed p12 corresponds to lpc P0[17], should appear as 0x0002 0000 in register LPC_GPIOINT->IO0IntEnR/F
00078 InterruptIn fire(p15);  // up of joystickn note that mbed p15 corresponds to lpc P0[23], should appear as 0x0080 0000 in register LPC_GPIOINT->IO0IntEnR/F
00079 #endif
00080 
00081 
00082 PinDetect pb(p14);      // center of joystickn note that mbed p14 corresponds to lpc P0[16] should appear as 0x0001 0000 in register LPC_GPIOINT->IO0IntEnR/F
00083 
00084 void ISR1() {           //interrupt service routine used to turn on LED 1 on rising edge of joystick up
00085     myled = 1;
00086 }
00087 
00088 void ISR2() {          //interrupt service routing used to turn off LED 1 on falling
00089     myled = 0;
00090 }
00091 
00092 // SPST Pushbutton debounced count demo using interrupts and callback
00093 // no external PullUp resistor needed
00094 // Pushbutton from P8 to GND.
00095 // A pb hit generates an interrupt and activates the callback function
00096 // after the switch is debounced
00097 
00098 // Global count variable
00099 int volatile count=0;
00100 
00101 // Callback routine is interrupt activated by a debounced pb hit
00102 void pb_hit_callback (void) {
00103     count++;
00104     myled4 = count & 0x01;
00105     myled3 = (count & 0x02)>>1;
00106     myled2 = (count & 0x04)>>2;
00107 }
00108 
00109 int main()
00110 {
00111 #ifdef DIRECTINT
00112    fire.rise(&ISR1);
00113    down.fall(&ISR2);
00114 #endif    
00115     // Use internal pullup for pushbutton
00116     //pb.mode(PullUp);                              // don't do this for app board
00117     // Delay for initial pullup to take effect
00118     wait(.01);
00119     // Setup Interrupt callback function for a pb hit
00120     pb.attach_deasserted(&pb_hit_callback);
00121     // Start sampling pb input using interrupts
00122     pb.setSampleFrequency();
00123 
00124     //Blink myled in main routine forever while responding to pb changes
00125     // via interrupts that activate the callback counter function
00126     bool disableInterrupt=false;
00127     int counter=0,tdelay=10;
00128     while (1) {
00129         wait(1);
00130         lcd.cls();
00131         lcd.locate(0,1);
00132         lcd.printf("ISER0=%#010X",*(NVIC->ISER)); //NVIC->ISER holds the address of the ISER0 register
00133         lcd.locate(0,10);
00134         lcd.printf("IO0 INTEN R=%#010X",LPC_GPIOINT->IO0IntEnR); //This address holds GPIO interrupt attach on rise edge state.
00135         lcd.locate(0,20);
00136         lcd.printf("IO0 INTEN F=%#010X",LPC_GPIOINT->IO0IntEnF ); //This address holds GPIO interrupt attach on falling edge state
00137 
00138 #ifdef DIRECTINT
00139         //This section of coe periodically (every 10 seconds) toggles the external interrupt 3 EINT3
00140         //state between enabled and disabled. This is the hardware interrupt used for th GPIO pins
00141         counter++;
00142         if(counter>=tdelay){
00143             disableInterrupt=!disableInterrupt;
00144             if(disableInterrupt)  NVIC_DisableIRQ(EINT3_IRQn);            //disable EINT3
00145             if(!disableInterrupt) NVIC_EnableIRQ(EINT3_IRQn);             //re-enable EINT3 
00146             counter=0;
00147         }
00148 #endif        
00149     }
00150 
00151 }