reading a complete dip switch

Dependencies:   Hotboards_switches mbed

Committer:
RomanValenciaP
Date:
Tue Mar 15 18:46:38 2016 +0000
Revision:
2:5799d5bb43c6
Parent:
1:2ed56eefabf0
added original author and contact info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RomanValenciaP 0:91c1916e3ed3 1
RomanValenciaP 2:5799d5bb43c6 2 /* Library: Hotboards_switches.h
RomanValenciaP 2:5799d5bb43c6 3 * Project: dip_switch
RomanValenciaP 2:5799d5bb43c6 4 * File: main.cpp
RomanValenciaP 2:5799d5bb43c6 5 * Author: Diego Perez
RomanValenciaP 2:5799d5bb43c6 6 * Modified by: Roman Valencia
RomanValenciaP 2:5799d5bb43c6 7 * Contact: http://www.hotboards.org/
RomanValenciaP 1:2ed56eefabf0 8 *
RomanValenciaP 0:91c1916e3ed3 9 * Read an entire dip switch composed by 8 interrupts
RomanValenciaP 0:91c1916e3ed3 10 */
RomanValenciaP 0:91c1916e3ed3 11
RomanValenciaP 0:91c1916e3ed3 12 #include "mbed.h"
RomanValenciaP 0:91c1916e3ed3 13 #include "Hotboards_switches.h"
RomanValenciaP 0:91c1916e3ed3 14
RomanValenciaP 0:91c1916e3ed3 15 // Creates a single sw object composed by 8 interrupts. theses interrupts will give us a
RomanValenciaP 0:91c1916e3ed3 16 // LOW(0) value when close because our dip switch works with pull-ups.
RomanValenciaP 0:91c1916e3ed3 17 Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 );
RomanValenciaP 0:91c1916e3ed3 18 // If your dip switch will gave you a HIGH(1) value when close, then we need to create the sw
RomanValenciaP 0:91c1916e3ed3 19 // object with an extra parameter:
RomanValenciaP 0:91c1916e3ed3 20 // Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 , 1 );
RomanValenciaP 0:91c1916e3ed3 21 // In any case the functions will return a HIGH(1) value any time the sw is closed
RomanValenciaP 0:91c1916e3ed3 22
RomanValenciaP 0:91c1916e3ed3 23 // For this example we will use the USB serial port, here we initialize it
RomanValenciaP 0:91c1916e3ed3 24 Serial pc(USBTX,USBRX);
RomanValenciaP 0:91c1916e3ed3 25
RomanValenciaP 0:91c1916e3ed3 26 int i, j;
RomanValenciaP 0:91c1916e3ed3 27 uint8_t value;
RomanValenciaP 0:91c1916e3ed3 28
RomanValenciaP 0:91c1916e3ed3 29 int main()
RomanValenciaP 0:91c1916e3ed3 30 {
RomanValenciaP 0:91c1916e3ed3 31 while(1)
RomanValenciaP 0:91c1916e3ed3 32 {
RomanValenciaP 0:91c1916e3ed3 33 // Reads the dip switch and puts it in value
RomanValenciaP 0:91c1916e3ed3 34 value = dip_sw.read();
RomanValenciaP 0:91c1916e3ed3 35 // Inverts the read value to make it coincide with the state of each
RomanValenciaP 0:91c1916e3ed3 36 // switch in binary
RomanValenciaP 0:91c1916e3ed3 37 value = ~value;
RomanValenciaP 0:91c1916e3ed3 38 // Sends throught USB serial the value in decimal
RomanValenciaP 0:91c1916e3ed3 39 pc.printf( "dec = " );
RomanValenciaP 0:91c1916e3ed3 40 pc.printf( "%d\r" , value );
RomanValenciaP 0:91c1916e3ed3 41 // Sends through USB serial the value in binary
RomanValenciaP 0:91c1916e3ed3 42 pc.printf( "bin = " );
RomanValenciaP 0:91c1916e3ed3 43 for(i=0;i<8;i++) //Extracts and shows the binary value of each sw
RomanValenciaP 0:91c1916e3ed3 44 {
RomanValenciaP 0:91c1916e3ed3 45 j = value&0x80; //Applies a mask(10000000) to extract the value of the bit in turn
RomanValenciaP 0:91c1916e3ed3 46 j>>=7; //Moves this bit 7 positions to the right
RomanValenciaP 0:91c1916e3ed3 47 pc.printf("%d",j); //Sends through USB serial the value of the extracted bit
RomanValenciaP 0:91c1916e3ed3 48 value <<=1; //Moves the position 1 bit to the left to extract the next bit
RomanValenciaP 0:91c1916e3ed3 49 }
RomanValenciaP 0:91c1916e3ed3 50 pc.printf( "\n\r" );
RomanValenciaP 0:91c1916e3ed3 51 wait( 1 );
RomanValenciaP 0:91c1916e3ed3 52 }
RomanValenciaP 0:91c1916e3ed3 53 }