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