m b / Mbed 2 deprecated 4180Lab1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * This program reads in the state of the external DIP switch
00003  * and outputs it to the LED using the MCP23S17 I/O expander chip
00004  *
00005  * @author(s): Gedeon Nyengele & Mike Bossi
00006  * @date: 01/29/2016
00007  */
00008 #include "mbed.h"
00009 #include "wdt.h"
00010 #include "MCP23S17.h"
00011 #include "Nav_Switch.h"
00012 #include "mpr121.h"
00013 
00014 
00015 DigitalOut LED_1(p21);
00016 PwmOut LED_2(p22);
00017 DigitalIn switch1(p23);
00018 DigitalIn switch2(p24);
00019 DigitalIn switch3(p25);
00020 DigitalIn switch4(p26);
00021 
00022 DigitalOut led1(LED1);
00023 DigitalOut led2(LED2);
00024 DigitalOut led3(LED3);
00025 DigitalOut led4(LED4);
00026 
00027 
00028 InterruptIn interrupt(p27);
00029 
00030 I2C i2c(p9, p10);
00031 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00032 
00033 void fallInterrupt() {
00034     int key_code=0;
00035     int i=0;
00036     int value=mpr121.read(0x00);
00037     value +=mpr121.read(0x01)<<8;
00038     // LED demo mod by J. Hamblen
00039     //pc.printf("MPR value: %x \r\n", value);
00040     i=0;
00041     // puts key number out to LEDs for demo
00042     for (i=0; i<12; i++) {
00043         if (((value>>i)&0x01)==1) key_code=i+1;
00044         }
00045     led4=key_code & 0x01;
00046     led3=(key_code>>1) & 0x01;
00047     led2=(key_code>>2) & 0x01;
00048     led1=(key_code>>3) & 0x01;
00049 }
00050 
00051 // Create an SPI bus
00052     SPI spi(p5, p6, p7);
00053 
00054     // Device opcode, as defined in datasheet
00055 
00056 
00057     // Read switch state and show state on LED
00058     // Assumption: Switch connected to GPB0 (MCP23S17 pin 1)
00059     //             LED connected to GPA0 (MCP23S17 pin 21)
00060 
00061 Watchdog wdt;
00062 BusIn _pins(p28, p29, p30);
00063 
00064 int main()
00065 {
00066     
00067     _pins.mode(PullUp);
00068     Nav_Switch myNav( p18, p15, p16, p19, p17);
00069     interrupt.fall(&fallInterrupt);
00070     interrupt.mode(PullUp);
00071     char Opcode = 0x40;
00072 
00073     MCP23S17 chip(spi, p20, Opcode);
00074 
00075     // Set PORT_A pins to be output pins
00076     chip.direction(PORT_A, 0x00);
00077 
00078     // Set PORT_B pins to be input pins
00079     chip.direction(PORT_B, 0xFF);
00080     wdt.kick(5.0); 
00081     int switch_1 = 0;
00082     int switch_2 = 0;
00083     int switch_3 = 0;
00084     
00085   //  int switch_4 = 0;
00086     short lockUpCount = 0;
00087     
00088     switch3.mode(PullUp);
00089     switch4.mode(PullUp);
00090     
00091     wait(.45);
00092     
00093    
00094    while(1){
00095         
00096         
00097         wdt.kick();
00098         
00099         if(!switch4){
00100             lockUpCount++;
00101             if (lockUpCount > 100)
00102                 while(1) {}   
00103         }
00104         
00105         
00106         switch (_pins){
00107             case 0x7:
00108                 if(!switch1)
00109                     LED_1 = 1;
00110                  else
00111                     LED_1 = 0;
00112             
00113                 switch_1 = switch1.read();
00114                 switch_2 = switch2.read();
00115                 switch_3 = switch3.read();
00116         
00117                 LED_2 = !switch_1*0.25 + !switch_2 * 0.40 + !switch_3*0.35;
00118             break;
00119             
00120             case 0x6:
00121                 chip.write(PORT_A, chip.read(PORT_B) & 0x01);
00122             break;
00123             
00124             case 0x5:
00125                 mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
00126                 if(myNav.fire()) mbedleds = 0x0F;
00127             break;
00128             
00129             }
00130         
00131         wait(0.05);
00132     }
00133     
00134 }
00135