![](/media/cache/profiles/abed4d6893be8b9687eab0234e0cac09.jpg.50x50_q85.jpg)
reading a group of switches or one only
Dependencies: Hotboards_switches mbed
main.cpp@1:5a47f2b970eb, 2016-03-15 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Tue Mar 15 18:41:06 2016 +0000
- Revision:
- 1:5a47f2b970eb
- Parent:
- 0:03e6fa9721e8
- Child:
- 2:57ea023c5aec
added header info
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:03e6fa9721e8 | 1 | |
RomanValenciaP | 1:5a47f2b970eb | 2 | /* Library: Hotboards_switches.h |
RomanValenciaP | 1:5a47f2b970eb | 3 | * Project: inputs |
RomanValenciaP | 1:5a47f2b970eb | 4 | * File: main.cpp |
RomanValenciaP | 1:5a47f2b970eb | 5 | * Author: Román Valencia |
RomanValenciaP | 1:5a47f2b970eb | 6 | * |
RomanValenciaP | 0:03e6fa9721e8 | 7 | * Read the value from 4 sw's (dip switch) and also read the value from a single sw. |
RomanValenciaP | 0:03e6fa9721e8 | 8 | */ |
RomanValenciaP | 0:03e6fa9721e8 | 9 | |
RomanValenciaP | 0:03e6fa9721e8 | 10 | #include "mbed.h" |
RomanValenciaP | 0:03e6fa9721e8 | 11 | #include "Hotboards_switches.h" |
RomanValenciaP | 0:03e6fa9721e8 | 12 | |
RomanValenciaP | 0:03e6fa9721e8 | 13 | // Creates two sw objects, this interrupt will give us a LOW(0) value when close |
RomanValenciaP | 0:03e6fa9721e8 | 14 | // because our dip switch works with pull-ups. |
RomanValenciaP | 0:03e6fa9721e8 | 15 | Hotboards_switches dip1( PA_9 , PA_8 , PB_10 , PB_4 ); |
RomanValenciaP | 0:03e6fa9721e8 | 16 | Hotboards_switches sw1( PA_6 ); |
RomanValenciaP | 0:03e6fa9721e8 | 17 | // If your dip switch will gave you a HIGH(1) value when close, then we need to create |
RomanValenciaP | 0:03e6fa9721e8 | 18 | // the sw object with an extra parameter: |
RomanValenciaP | 0:03e6fa9721e8 | 19 | // Hotboards_switches dip1( PA_9 , PA_8 , PB_10 , PB_4 , 1 ); |
RomanValenciaP | 0:03e6fa9721e8 | 20 | // Hotboards_swtiches sw1( PA_6 , 1 ); |
RomanValenciaP | 0:03e6fa9721e8 | 21 | // In any case the function will return a HIGH(1) value any time the sw is closed |
RomanValenciaP | 0:03e6fa9721e8 | 22 | |
RomanValenciaP | 0:03e6fa9721e8 | 23 | //For this example we will use the USB serial port, here we initialize it |
RomanValenciaP | 0:03e6fa9721e8 | 24 | Serial pc(USBTX,USBRX); |
RomanValenciaP | 0:03e6fa9721e8 | 25 | |
RomanValenciaP | 0:03e6fa9721e8 | 26 | int i, j; |
RomanValenciaP | 0:03e6fa9721e8 | 27 | uint8_t binary, decimal; |
RomanValenciaP | 0:03e6fa9721e8 | 28 | |
RomanValenciaP | 0:03e6fa9721e8 | 29 | int main() |
RomanValenciaP | 0:03e6fa9721e8 | 30 | { |
RomanValenciaP | 0:03e6fa9721e8 | 31 | while(1) |
RomanValenciaP | 0:03e6fa9721e8 | 32 | { |
RomanValenciaP | 0:03e6fa9721e8 | 33 | //Asks for the state of the sw1 |
RomanValenciaP | 0:03e6fa9721e8 | 34 | if( sw1.read( ) ) |
RomanValenciaP | 0:03e6fa9721e8 | 35 | { |
RomanValenciaP | 0:03e6fa9721e8 | 36 | pc.printf( "sw1 = close (on)\r" ); |
RomanValenciaP | 0:03e6fa9721e8 | 37 | } |
RomanValenciaP | 0:03e6fa9721e8 | 38 | else |
RomanValenciaP | 0:03e6fa9721e8 | 39 | { |
RomanValenciaP | 0:03e6fa9721e8 | 40 | pc.printf( "sw1 = open (off)\r" ); |
RomanValenciaP | 0:03e6fa9721e8 | 41 | } |
RomanValenciaP | 0:03e6fa9721e8 | 42 | //Asks for the state of an specific sw from the dip switch dip1 |
RomanValenciaP | 0:03e6fa9721e8 | 43 | if( dip1.read( 2 ) ) |
RomanValenciaP | 0:03e6fa9721e8 | 44 | { |
RomanValenciaP | 0:03e6fa9721e8 | 45 | pc.printf( "dip sw2 = close (on)\r" ); |
RomanValenciaP | 0:03e6fa9721e8 | 46 | } |
RomanValenciaP | 0:03e6fa9721e8 | 47 | else |
RomanValenciaP | 0:03e6fa9721e8 | 48 | { |
RomanValenciaP | 0:03e6fa9721e8 | 49 | pc.printf( "dip sw2 = open (off)\r" ); |
RomanValenciaP | 0:03e6fa9721e8 | 50 | } |
RomanValenciaP | 0:03e6fa9721e8 | 51 | |
RomanValenciaP | 0:03e6fa9721e8 | 52 | // Reads the dip switch value and puts it in binary |
RomanValenciaP | 0:03e6fa9721e8 | 53 | binary = dip1.read(); |
RomanValenciaP | 0:03e6fa9721e8 | 54 | // Inverts the read value to make it coincide with the state of each |
RomanValenciaP | 0:03e6fa9721e8 | 55 | // switch in binary |
RomanValenciaP | 0:03e6fa9721e8 | 56 | binary = ~binary; |
RomanValenciaP | 0:03e6fa9721e8 | 57 | // Applies a mask(00001111) to extract the value of the dip switch |
RomanValenciaP | 0:03e6fa9721e8 | 58 | decimal = binary&0x0F; |
RomanValenciaP | 0:03e6fa9721e8 | 59 | // Sends throught USB serial the value in decimal |
RomanValenciaP | 0:03e6fa9721e8 | 60 | pc.printf( "dec = " ); |
RomanValenciaP | 0:03e6fa9721e8 | 61 | pc.printf( "%d\r" , decimal ); |
RomanValenciaP | 0:03e6fa9721e8 | 62 | // Sends through USB serial the value in binary |
RomanValenciaP | 0:03e6fa9721e8 | 63 | pc.printf( "bin = " ); |
RomanValenciaP | 0:03e6fa9721e8 | 64 | for(i=0;i<4;i++) //Extracts and shows the binary value of each sw |
RomanValenciaP | 0:03e6fa9721e8 | 65 | { |
RomanValenciaP | 0:03e6fa9721e8 | 66 | j = binary&0x08; //Applies a mask(00001000) to extract the value of the bit in turn |
RomanValenciaP | 0:03e6fa9721e8 | 67 | j>>=3; //Moves this bit 3 positions to the right |
RomanValenciaP | 0:03e6fa9721e8 | 68 | pc.printf("%d",j); //Sends through USB serial the value of the extracted bit |
RomanValenciaP | 0:03e6fa9721e8 | 69 | binary <<=1; //Moves the position 1 bit to the left to extract the next bit |
RomanValenciaP | 0:03e6fa9721e8 | 70 | } |
RomanValenciaP | 0:03e6fa9721e8 | 71 | pc.printf( "\n\r" ); |
RomanValenciaP | 0:03e6fa9721e8 | 72 | wait( 2 ); |
RomanValenciaP | 0:03e6fa9721e8 | 73 | } |
RomanValenciaP | 0:03e6fa9721e8 | 74 | } |