Complete

Dependencies:   mbed

Committer:
mikeb
Date:
Sat Jan 30 03:22:41 2016 +0000
Revision:
3:ecfa4fa0b5a7
Parent:
2:029b0a855b82
Complete;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:8f5b2af5e1d5 1 /**
mikeb 0:8f5b2af5e1d5 2 * This program reads in the state of the external DIP switch
mikeb 0:8f5b2af5e1d5 3 * and outputs it to the LED using the MCP23S17 I/O expander chip
mikeb 0:8f5b2af5e1d5 4 *
mikeb 0:8f5b2af5e1d5 5 * @author(s): Gedeon Nyengele & Mike Bossi
mikeb 0:8f5b2af5e1d5 6 * @date: 01/29/2016
mikeb 0:8f5b2af5e1d5 7 */
mikeb 0:8f5b2af5e1d5 8 #include "mbed.h"
mikeb 0:8f5b2af5e1d5 9 #include "wdt.h"
mikeb 0:8f5b2af5e1d5 10 #include "MCP23S17.h"
mikeb 2:029b0a855b82 11 #include "Nav_Switch.h"
mikeb 3:ecfa4fa0b5a7 12 #include "mpr121.h"
mikeb 0:8f5b2af5e1d5 13
mikeb 0:8f5b2af5e1d5 14
mikeb 0:8f5b2af5e1d5 15 DigitalOut LED_1(p21);
mikeb 0:8f5b2af5e1d5 16 PwmOut LED_2(p22);
mikeb 0:8f5b2af5e1d5 17 DigitalIn switch1(p23);
mikeb 0:8f5b2af5e1d5 18 DigitalIn switch2(p24);
mikeb 0:8f5b2af5e1d5 19 DigitalIn switch3(p25);
mikeb 0:8f5b2af5e1d5 20 DigitalIn switch4(p26);
mikeb 0:8f5b2af5e1d5 21
mikeb 3:ecfa4fa0b5a7 22 DigitalOut led1(LED1);
mikeb 3:ecfa4fa0b5a7 23 DigitalOut led2(LED2);
mikeb 3:ecfa4fa0b5a7 24 DigitalOut led3(LED3);
mikeb 3:ecfa4fa0b5a7 25 DigitalOut led4(LED4);
mikeb 3:ecfa4fa0b5a7 26
mikeb 3:ecfa4fa0b5a7 27
mikeb 3:ecfa4fa0b5a7 28 InterruptIn interrupt(p27);
mikeb 3:ecfa4fa0b5a7 29
mikeb 3:ecfa4fa0b5a7 30 I2C i2c(p9, p10);
mikeb 3:ecfa4fa0b5a7 31 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
mikeb 3:ecfa4fa0b5a7 32
mikeb 3:ecfa4fa0b5a7 33 void fallInterrupt() {
mikeb 3:ecfa4fa0b5a7 34 int key_code=0;
mikeb 3:ecfa4fa0b5a7 35 int i=0;
mikeb 3:ecfa4fa0b5a7 36 int value=mpr121.read(0x00);
mikeb 3:ecfa4fa0b5a7 37 value +=mpr121.read(0x01)<<8;
mikeb 3:ecfa4fa0b5a7 38 // LED demo mod by J. Hamblen
mikeb 3:ecfa4fa0b5a7 39 //pc.printf("MPR value: %x \r\n", value);
mikeb 3:ecfa4fa0b5a7 40 i=0;
mikeb 3:ecfa4fa0b5a7 41 // puts key number out to LEDs for demo
mikeb 3:ecfa4fa0b5a7 42 for (i=0; i<12; i++) {
mikeb 3:ecfa4fa0b5a7 43 if (((value>>i)&0x01)==1) key_code=i+1;
mikeb 3:ecfa4fa0b5a7 44 }
mikeb 3:ecfa4fa0b5a7 45 led4=key_code & 0x01;
mikeb 3:ecfa4fa0b5a7 46 led3=(key_code>>1) & 0x01;
mikeb 3:ecfa4fa0b5a7 47 led2=(key_code>>2) & 0x01;
mikeb 3:ecfa4fa0b5a7 48 led1=(key_code>>3) & 0x01;
mikeb 3:ecfa4fa0b5a7 49 }
mikeb 3:ecfa4fa0b5a7 50
mikeb 0:8f5b2af5e1d5 51 // Create an SPI bus
mikeb 0:8f5b2af5e1d5 52 SPI spi(p5, p6, p7);
mikeb 0:8f5b2af5e1d5 53
mikeb 0:8f5b2af5e1d5 54 // Device opcode, as defined in datasheet
mikeb 0:8f5b2af5e1d5 55
mikeb 0:8f5b2af5e1d5 56
mikeb 0:8f5b2af5e1d5 57 // Read switch state and show state on LED
mikeb 0:8f5b2af5e1d5 58 // Assumption: Switch connected to GPB0 (MCP23S17 pin 1)
mikeb 0:8f5b2af5e1d5 59 // LED connected to GPA0 (MCP23S17 pin 21)
mikeb 0:8f5b2af5e1d5 60
mikeb 0:8f5b2af5e1d5 61 Watchdog wdt;
mikeb 0:8f5b2af5e1d5 62 BusIn _pins(p28, p29, p30);
mikeb 0:8f5b2af5e1d5 63
mikeb 0:8f5b2af5e1d5 64 int main()
mikeb 0:8f5b2af5e1d5 65 {
mikeb 0:8f5b2af5e1d5 66
mikeb 0:8f5b2af5e1d5 67 _pins.mode(PullUp);
mikeb 2:029b0a855b82 68 Nav_Switch myNav( p18, p15, p16, p19, p17);
mikeb 3:ecfa4fa0b5a7 69 interrupt.fall(&fallInterrupt);
mikeb 3:ecfa4fa0b5a7 70 interrupt.mode(PullUp);
mikeb 0:8f5b2af5e1d5 71 char Opcode = 0x40;
mikeb 0:8f5b2af5e1d5 72
mikeb 0:8f5b2af5e1d5 73 MCP23S17 chip(spi, p20, Opcode);
mikeb 0:8f5b2af5e1d5 74
mikeb 0:8f5b2af5e1d5 75 // Set PORT_A pins to be output pins
mikeb 0:8f5b2af5e1d5 76 chip.direction(PORT_A, 0x00);
mikeb 0:8f5b2af5e1d5 77
mikeb 0:8f5b2af5e1d5 78 // Set PORT_B pins to be input pins
mikeb 0:8f5b2af5e1d5 79 chip.direction(PORT_B, 0xFF);
mikeb 0:8f5b2af5e1d5 80 wdt.kick(5.0);
mikeb 0:8f5b2af5e1d5 81 int switch_1 = 0;
mikeb 0:8f5b2af5e1d5 82 int switch_2 = 0;
mikeb 0:8f5b2af5e1d5 83 int switch_3 = 0;
mikeb 0:8f5b2af5e1d5 84
mikeb 0:8f5b2af5e1d5 85 // int switch_4 = 0;
mikeb 0:8f5b2af5e1d5 86 short lockUpCount = 0;
mikeb 0:8f5b2af5e1d5 87
mikeb 0:8f5b2af5e1d5 88 switch3.mode(PullUp);
mikeb 0:8f5b2af5e1d5 89 switch4.mode(PullUp);
mikeb 2:029b0a855b82 90
mikeb 2:029b0a855b82 91 wait(.45);
mikeb 2:029b0a855b82 92
mikeb 0:8f5b2af5e1d5 93
mikeb 0:8f5b2af5e1d5 94 while(1){
mikeb 0:8f5b2af5e1d5 95
mikeb 0:8f5b2af5e1d5 96
mikeb 0:8f5b2af5e1d5 97 wdt.kick();
mikeb 0:8f5b2af5e1d5 98
mikeb 0:8f5b2af5e1d5 99 if(!switch4){
mikeb 0:8f5b2af5e1d5 100 lockUpCount++;
mikeb 0:8f5b2af5e1d5 101 if (lockUpCount > 100)
mikeb 0:8f5b2af5e1d5 102 while(1) {}
mikeb 0:8f5b2af5e1d5 103 }
mikeb 1:c5bc18044085 104
mikeb 1:c5bc18044085 105
mikeb 0:8f5b2af5e1d5 106 switch (_pins){
mikeb 1:c5bc18044085 107 case 0x7:
mikeb 0:8f5b2af5e1d5 108 if(!switch1)
mikeb 1:c5bc18044085 109 LED_1 = 1;
mikeb 1:c5bc18044085 110 else
mikeb 1:c5bc18044085 111 LED_1 = 0;
mikeb 0:8f5b2af5e1d5 112
mikeb 1:c5bc18044085 113 switch_1 = switch1.read();
mikeb 1:c5bc18044085 114 switch_2 = switch2.read();
mikeb 1:c5bc18044085 115 switch_3 = switch3.read();
mikeb 0:8f5b2af5e1d5 116
mikeb 1:c5bc18044085 117 LED_2 = !switch_1*0.25 + !switch_2 * 0.40 + !switch_3*0.35;
mikeb 1:c5bc18044085 118 break;
mikeb 0:8f5b2af5e1d5 119
mikeb 1:c5bc18044085 120 case 0x6:
mikeb 1:c5bc18044085 121 chip.write(PORT_A, chip.read(PORT_B) & 0x01);
mikeb 1:c5bc18044085 122 break;
mikeb 1:c5bc18044085 123
mikeb 1:c5bc18044085 124 case 0x5:
mikeb 2:029b0a855b82 125 mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
mikeb 2:029b0a855b82 126 if(myNav.fire()) mbedleds = 0x0F;
mikeb 1:c5bc18044085 127 break;
mikeb 0:8f5b2af5e1d5 128
mikeb 0:8f5b2af5e1d5 129 }
mikeb 0:8f5b2af5e1d5 130
mikeb 2:029b0a855b82 131 wait(0.05);
mikeb 0:8f5b2af5e1d5 132 }
mikeb 0:8f5b2af5e1d5 133
mikeb 0:8f5b2af5e1d5 134 }
mikeb 0:8f5b2af5e1d5 135