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