reading a complete dip switch
Dependencies: Hotboards_switches mbed
main.cpp@0:91c1916e3ed3, 2016-03-02 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Wed Mar 02 20:32:39 2016 +0000
- Revision:
- 0:91c1916e3ed3
- Child:
- 1:2ed56eefabf0
first release - recquires approval
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:91c1916e3ed3 | 1 | |
RomanValenciaP | 0:91c1916e3ed3 | 2 | /* |
RomanValenciaP | 0:91c1916e3ed3 | 3 | * Read an entire dip switch composed by 8 interrupts |
RomanValenciaP | 0:91c1916e3ed3 | 4 | */ |
RomanValenciaP | 0:91c1916e3ed3 | 5 | |
RomanValenciaP | 0:91c1916e3ed3 | 6 | #include "mbed.h" |
RomanValenciaP | 0:91c1916e3ed3 | 7 | #include "Hotboards_switches.h" |
RomanValenciaP | 0:91c1916e3ed3 | 8 | |
RomanValenciaP | 0:91c1916e3ed3 | 9 | // Creates a single sw object composed by 8 interrupts. theses interrupts will give us a |
RomanValenciaP | 0:91c1916e3ed3 | 10 | // LOW(0) value when close because our dip switch works with pull-ups. |
RomanValenciaP | 0:91c1916e3ed3 | 11 | Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 ); |
RomanValenciaP | 0:91c1916e3ed3 | 12 | // If your dip switch will gave you a HIGH(1) value when close, then we need to create the sw |
RomanValenciaP | 0:91c1916e3ed3 | 13 | // object with an extra parameter: |
RomanValenciaP | 0:91c1916e3ed3 | 14 | // Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 , 1 ); |
RomanValenciaP | 0:91c1916e3ed3 | 15 | // In any case the functions will return a HIGH(1) value any time the sw is closed |
RomanValenciaP | 0:91c1916e3ed3 | 16 | |
RomanValenciaP | 0:91c1916e3ed3 | 17 | // For this example we will use the USB serial port, here we initialize it |
RomanValenciaP | 0:91c1916e3ed3 | 18 | Serial pc(USBTX,USBRX); |
RomanValenciaP | 0:91c1916e3ed3 | 19 | |
RomanValenciaP | 0:91c1916e3ed3 | 20 | int i, j; |
RomanValenciaP | 0:91c1916e3ed3 | 21 | uint8_t value; |
RomanValenciaP | 0:91c1916e3ed3 | 22 | |
RomanValenciaP | 0:91c1916e3ed3 | 23 | int main() |
RomanValenciaP | 0:91c1916e3ed3 | 24 | { |
RomanValenciaP | 0:91c1916e3ed3 | 25 | while(1) |
RomanValenciaP | 0:91c1916e3ed3 | 26 | { |
RomanValenciaP | 0:91c1916e3ed3 | 27 | // Reads the dip switch and puts it in value |
RomanValenciaP | 0:91c1916e3ed3 | 28 | value = dip_sw.read(); |
RomanValenciaP | 0:91c1916e3ed3 | 29 | // Inverts the read value to make it coincide with the state of each |
RomanValenciaP | 0:91c1916e3ed3 | 30 | // switch in binary |
RomanValenciaP | 0:91c1916e3ed3 | 31 | value = ~value; |
RomanValenciaP | 0:91c1916e3ed3 | 32 | // Sends throught USB serial the value in decimal |
RomanValenciaP | 0:91c1916e3ed3 | 33 | pc.printf( "dec = " ); |
RomanValenciaP | 0:91c1916e3ed3 | 34 | pc.printf( "%d\r" , value ); |
RomanValenciaP | 0:91c1916e3ed3 | 35 | // Sends through USB serial the value in binary |
RomanValenciaP | 0:91c1916e3ed3 | 36 | pc.printf( "bin = " ); |
RomanValenciaP | 0:91c1916e3ed3 | 37 | for(i=0;i<8;i++) //Extracts and shows the binary value of each sw |
RomanValenciaP | 0:91c1916e3ed3 | 38 | { |
RomanValenciaP | 0:91c1916e3ed3 | 39 | j = value&0x80; //Applies a mask(10000000) to extract the value of the bit in turn |
RomanValenciaP | 0:91c1916e3ed3 | 40 | j>>=7; //Moves this bit 7 positions to the right |
RomanValenciaP | 0:91c1916e3ed3 | 41 | pc.printf("%d",j); //Sends through USB serial the value of the extracted bit |
RomanValenciaP | 0:91c1916e3ed3 | 42 | value <<=1; //Moves the position 1 bit to the left to extract the next bit |
RomanValenciaP | 0:91c1916e3ed3 | 43 | } |
RomanValenciaP | 0:91c1916e3ed3 | 44 | pc.printf( "\n\r" ); |
RomanValenciaP | 0:91c1916e3ed3 | 45 | wait( 1 ); |
RomanValenciaP | 0:91c1916e3ed3 | 46 | } |
RomanValenciaP | 0:91c1916e3ed3 | 47 | } |