![](/media/cache/profiles/abed4d6893be8b9687eab0234e0cac09.jpg.50x50_q85.jpg)
reading only one interrupt when it changes its value
Dependencies: Hotboards_switches mbed
main.cpp@1:af16896c2662, 2016-03-15 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Tue Mar 15 18:40:54 2016 +0000
- Revision:
- 1:af16896c2662
- Parent:
- 0:e4efa77cbd55
- Child:
- 2:19c591b8f837
added header info
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:e4efa77cbd55 | 1 | |
RomanValenciaP | 1:af16896c2662 | 2 | /* Library: Hotboards_switches.h |
RomanValenciaP | 1:af16896c2662 | 3 | * Project: input_change |
RomanValenciaP | 1:af16896c2662 | 4 | * File: main.cpp |
RomanValenciaP | 1:af16896c2662 | 5 | * Author: Román Valencia |
RomanValenciaP | 1:af16896c2662 | 6 | * |
RomanValenciaP | 0:e4efa77cbd55 | 7 | * Reads an input on pin PB_4, only when its value has been changed |
RomanValenciaP | 0:e4efa77cbd55 | 8 | */ |
RomanValenciaP | 0:e4efa77cbd55 | 9 | |
RomanValenciaP | 0:e4efa77cbd55 | 10 | #include "mbed.h" |
RomanValenciaP | 0:e4efa77cbd55 | 11 | #include "Hotboards_switches.h" |
RomanValenciaP | 0:e4efa77cbd55 | 12 | |
RomanValenciaP | 0:e4efa77cbd55 | 13 | // Creates a single sw object, this interrupt will give us a LOW(0) value when close |
RomanValenciaP | 0:e4efa77cbd55 | 14 | // because our dip switch works with pull-ups |
RomanValenciaP | 0:e4efa77cbd55 | 15 | Hotboards_switches sw( PB_4 ); |
RomanValenciaP | 0:e4efa77cbd55 | 16 | // If your dip switch will gave you a HIGH(1) value when close, then we need to create |
RomanValenciaP | 0:e4efa77cbd55 | 17 | // the sw object with an extra parameter: Hotboards_switches sw( PB_4 , 1 ); |
RomanValenciaP | 0:e4efa77cbd55 | 18 | // In any case the function will return a HIGH(1) value any time the sw is closed |
RomanValenciaP | 0:e4efa77cbd55 | 19 | |
RomanValenciaP | 0:e4efa77cbd55 | 20 | // For this example we will use the USB serial port, here we initialize it |
RomanValenciaP | 0:e4efa77cbd55 | 21 | Serial pc(USBTX,USBRX); |
RomanValenciaP | 0:e4efa77cbd55 | 22 | |
RomanValenciaP | 0:e4efa77cbd55 | 23 | int main() |
RomanValenciaP | 0:e4efa77cbd55 | 24 | { |
RomanValenciaP | 0:e4efa77cbd55 | 25 | while(1) |
RomanValenciaP | 0:e4efa77cbd55 | 26 | { |
RomanValenciaP | 0:e4efa77cbd55 | 27 | //Asks when the interrupt changes its value |
RomanValenciaP | 0:e4efa77cbd55 | 28 | if( sw.hasItChange( ) ) |
RomanValenciaP | 0:e4efa77cbd55 | 29 | { |
RomanValenciaP | 0:e4efa77cbd55 | 30 | // When the interrupt is close (or ON) the function will return a true value |
RomanValenciaP | 0:e4efa77cbd55 | 31 | // it doesn´t matter if our input is configured with pull-ups(LOW) or |
RomanValenciaP | 0:e4efa77cbd55 | 32 | // pull-downs(HIGH) |
RomanValenciaP | 0:e4efa77cbd55 | 33 | if( sw.read( ) ) |
RomanValenciaP | 0:e4efa77cbd55 | 34 | { |
RomanValenciaP | 0:e4efa77cbd55 | 35 | pc.printf( "sw = close (on)\n\r" ); |
RomanValenciaP | 0:e4efa77cbd55 | 36 | } |
RomanValenciaP | 0:e4efa77cbd55 | 37 | else |
RomanValenciaP | 0:e4efa77cbd55 | 38 | { |
RomanValenciaP | 0:e4efa77cbd55 | 39 | pc.printf( "sw = open (off)\n\r" ); |
RomanValenciaP | 0:e4efa77cbd55 | 40 | } |
RomanValenciaP | 0:e4efa77cbd55 | 41 | // Wait 250 ms, just to not query so often |
RomanValenciaP | 0:e4efa77cbd55 | 42 | wait_ms( 250 ); |
RomanValenciaP | 0:e4efa77cbd55 | 43 | } |
RomanValenciaP | 0:e4efa77cbd55 | 44 | } |
RomanValenciaP | 0:e4efa77cbd55 | 45 | } |