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:46:50 2016 +0000
Revision:
2:eb1ab05e9a0a
Parent:
1:11a5f3209b40
added original author and contact info

Who changed what in which revision?

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