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