![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Complete
main.cpp@2:029b0a855b82, 2016-01-30 (annotated)
- Committer:
- mikeb
- Date:
- Sat Jan 30 02:34:55 2016 +0000
- Revision:
- 2:029b0a855b82
- Parent:
- 1:c5bc18044085
- Child:
- 3:ecfa4fa0b5a7
Functional on Parts 1-4
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:8f5b2af5e1d5 | 12 | |
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 | 0:8f5b2af5e1d5 | 22 | // Create an SPI bus |
mikeb | 0:8f5b2af5e1d5 | 23 | SPI spi(p5, p6, p7); |
mikeb | 0:8f5b2af5e1d5 | 24 | |
mikeb | 0:8f5b2af5e1d5 | 25 | // Device opcode, as defined in datasheet |
mikeb | 0:8f5b2af5e1d5 | 26 | |
mikeb | 0:8f5b2af5e1d5 | 27 | |
mikeb | 0:8f5b2af5e1d5 | 28 | // Read switch state and show state on LED |
mikeb | 0:8f5b2af5e1d5 | 29 | // Assumption: Switch connected to GPB0 (MCP23S17 pin 1) |
mikeb | 0:8f5b2af5e1d5 | 30 | // LED connected to GPA0 (MCP23S17 pin 21) |
mikeb | 0:8f5b2af5e1d5 | 31 | |
mikeb | 0:8f5b2af5e1d5 | 32 | Watchdog wdt; |
mikeb | 0:8f5b2af5e1d5 | 33 | BusIn _pins(p28, p29, p30); |
mikeb | 0:8f5b2af5e1d5 | 34 | |
mikeb | 0:8f5b2af5e1d5 | 35 | int main() |
mikeb | 0:8f5b2af5e1d5 | 36 | { |
mikeb | 0:8f5b2af5e1d5 | 37 | |
mikeb | 0:8f5b2af5e1d5 | 38 | _pins.mode(PullUp); |
mikeb | 2:029b0a855b82 | 39 | Nav_Switch myNav( p18, p15, p16, p19, p17); |
mikeb | 0:8f5b2af5e1d5 | 40 | |
mikeb | 0:8f5b2af5e1d5 | 41 | char Opcode = 0x40; |
mikeb | 0:8f5b2af5e1d5 | 42 | |
mikeb | 0:8f5b2af5e1d5 | 43 | MCP23S17 chip(spi, p20, Opcode); |
mikeb | 0:8f5b2af5e1d5 | 44 | |
mikeb | 0:8f5b2af5e1d5 | 45 | // Set PORT_A pins to be output pins |
mikeb | 0:8f5b2af5e1d5 | 46 | chip.direction(PORT_A, 0x00); |
mikeb | 0:8f5b2af5e1d5 | 47 | |
mikeb | 0:8f5b2af5e1d5 | 48 | // Set PORT_B pins to be input pins |
mikeb | 0:8f5b2af5e1d5 | 49 | chip.direction(PORT_B, 0xFF); |
mikeb | 0:8f5b2af5e1d5 | 50 | wdt.kick(5.0); |
mikeb | 0:8f5b2af5e1d5 | 51 | int switch_1 = 0; |
mikeb | 0:8f5b2af5e1d5 | 52 | int switch_2 = 0; |
mikeb | 0:8f5b2af5e1d5 | 53 | int switch_3 = 0; |
mikeb | 0:8f5b2af5e1d5 | 54 | |
mikeb | 0:8f5b2af5e1d5 | 55 | // int switch_4 = 0; |
mikeb | 0:8f5b2af5e1d5 | 56 | short lockUpCount = 0; |
mikeb | 0:8f5b2af5e1d5 | 57 | |
mikeb | 0:8f5b2af5e1d5 | 58 | switch3.mode(PullUp); |
mikeb | 0:8f5b2af5e1d5 | 59 | switch4.mode(PullUp); |
mikeb | 2:029b0a855b82 | 60 | |
mikeb | 2:029b0a855b82 | 61 | wait(.45); |
mikeb | 2:029b0a855b82 | 62 | |
mikeb | 0:8f5b2af5e1d5 | 63 | |
mikeb | 0:8f5b2af5e1d5 | 64 | while(1){ |
mikeb | 0:8f5b2af5e1d5 | 65 | |
mikeb | 0:8f5b2af5e1d5 | 66 | |
mikeb | 0:8f5b2af5e1d5 | 67 | wdt.kick(); |
mikeb | 0:8f5b2af5e1d5 | 68 | |
mikeb | 0:8f5b2af5e1d5 | 69 | if(!switch4){ |
mikeb | 0:8f5b2af5e1d5 | 70 | lockUpCount++; |
mikeb | 0:8f5b2af5e1d5 | 71 | if (lockUpCount > 100) |
mikeb | 0:8f5b2af5e1d5 | 72 | while(1) {} |
mikeb | 0:8f5b2af5e1d5 | 73 | } |
mikeb | 1:c5bc18044085 | 74 | |
mikeb | 1:c5bc18044085 | 75 | |
mikeb | 0:8f5b2af5e1d5 | 76 | switch (_pins){ |
mikeb | 1:c5bc18044085 | 77 | case 0x7: |
mikeb | 0:8f5b2af5e1d5 | 78 | if(!switch1) |
mikeb | 1:c5bc18044085 | 79 | LED_1 = 1; |
mikeb | 1:c5bc18044085 | 80 | else |
mikeb | 1:c5bc18044085 | 81 | LED_1 = 0; |
mikeb | 0:8f5b2af5e1d5 | 82 | |
mikeb | 1:c5bc18044085 | 83 | switch_1 = switch1.read(); |
mikeb | 1:c5bc18044085 | 84 | switch_2 = switch2.read(); |
mikeb | 1:c5bc18044085 | 85 | switch_3 = switch3.read(); |
mikeb | 0:8f5b2af5e1d5 | 86 | |
mikeb | 1:c5bc18044085 | 87 | LED_2 = !switch_1*0.25 + !switch_2 * 0.40 + !switch_3*0.35; |
mikeb | 1:c5bc18044085 | 88 | break; |
mikeb | 0:8f5b2af5e1d5 | 89 | |
mikeb | 1:c5bc18044085 | 90 | case 0x6: |
mikeb | 1:c5bc18044085 | 91 | chip.write(PORT_A, chip.read(PORT_B) & 0x01); |
mikeb | 1:c5bc18044085 | 92 | break; |
mikeb | 1:c5bc18044085 | 93 | |
mikeb | 1:c5bc18044085 | 94 | case 0x5: |
mikeb | 2:029b0a855b82 | 95 | mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs |
mikeb | 2:029b0a855b82 | 96 | if(myNav.fire()) mbedleds = 0x0F; |
mikeb | 1:c5bc18044085 | 97 | break; |
mikeb | 0:8f5b2af5e1d5 | 98 | |
mikeb | 0:8f5b2af5e1d5 | 99 | } |
mikeb | 0:8f5b2af5e1d5 | 100 | |
mikeb | 2:029b0a855b82 | 101 | wait(0.05); |
mikeb | 0:8f5b2af5e1d5 | 102 | } |
mikeb | 0:8f5b2af5e1d5 | 103 | |
mikeb | 0:8f5b2af5e1d5 | 104 | } |
mikeb | 0:8f5b2af5e1d5 | 105 |