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