reading a complete dip switch when it changes its value

Dependencies:   Hotboards_switches mbed

Fork of dipsw_change by Roman Valencia

Committer:
RomanValenciaP
Date:
Tue Mar 15 18:40:30 2016 +0000
Revision:
1:11a5f3209b40
Parent:
0:d5cdb8374e51
Child:
2:eb1ab05e9a0a
added header info

Who changed what in which revision?

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