Timer, Timeout, Ticker, PWM, Debounce

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //------------------------------------
00004 // Hyperterminal configuration
00005 // 9600 bauds, 8-bit data, no parity
00006 //------------------------------------
00007 
00008 Serial pc(SERIAL_TX, SERIAL_RX);
00009 
00010 Ticker toggle_led_ticker;
00011 Timeout timedown;
00012 
00013 //define pins
00014 DigitalOut  led1(PA_9);
00015 InterruptIn  SW7(PB_3);
00016 InterruptIn  SW6(PA_10);
00017  
00018 //Variables
00019 static float tickertimer = 0.1;
00020 
00021 //Prototypes
00022 void toggle_led();
00023 void timeouttick();
00024 void interrupt();
00025 
00026 
00027 void timeouttick()
00028 {
00029     SW7.enable_irq();
00030     SW6.enable_irq();
00031 }
00032 
00033 void interruptSW7(){
00034    SW7.disable_irq();
00035    timedown.attach(&timeouttick, 0.03); //debounce
00036    if(tickertimer > 0.00f)
00037         tickertimer += 0.01f;
00038    if(tickertimer > 0.00f)
00039     {
00040         
00041         toggle_led_ticker.attach(&toggle_led, tickertimer);
00042     } 
00043     else 
00044     {
00045         toggle_led_ticker.detach();
00046     }
00047 }  
00048 
00049 void interruptSW6(){
00050    SW6.disable_irq();
00051    timedown.attach(&timeouttick, 0.03); //debounce
00052    
00053    tickertimer -= 0.01f;
00054    toggle_led_ticker.attach(&toggle_led, tickertimer);
00055 }    
00056    
00057 void toggle_led() {
00058     led1 = !led1;
00059 }
00060 
00061 int main() {
00062     
00063     pc.printf("Velkommen\r\n");
00064     //attach interrup to faling edge
00065     SW7.fall(&interruptSW7);
00066     SW6.fall(&interruptSW6);
00067     
00068     // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
00069     
00070     
00071     
00072     while (true) {
00073         pc.printf("tickertimer: %2.3f\r\n", tickertimer);
00074         pc.printf("t\r\n");
00075         // Do other things...
00076     }
00077 }