Kenneth Swanson / Mbed 2 deprecated 4180-lab1-basic-io

Dependencies:   MCP23S17 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // code for part 1, 2, & 3
00002 #include "mbed.h"
00003 #include "MCP23S17.h"
00004 
00005 // 1
00006 DigitalIn dip_switch3(p30);
00007 DigitalOut red_led(p17);
00008 // 2
00009 DigitalIn dip_switch1(p28);
00010 DigitalIn dip_switch2(p29);
00011 PwmOut green_led(p21);
00012 // 3
00013 SPI spi(p5, p6, p7); // SPI bus
00014 char Opcode = 0x40; // 8-bit address for writes
00015 MCP23S17 chip = MCP23S17(spi, p20, Opcode); // create an MCP23S17
00016 int switch_state;
00017 // RGB led control
00018 AnalogIn control_pot(p18);
00019 PwmOut rgb_b(p22);
00020 PwmOut rgb_r(p23);
00021 PwmOut rgb_g(p24);
00022 // WATCHDOG
00023 DigitalOut myled1(LED1); //in main loop part 1
00024 DigitalOut myled2(LED2); //in main loop part 2 (where fault occurs)
00025 DigitalOut myled3(LED3); //The pushbutton or power on caused a reset
00026 DigitalOut myled4(LED4); //The watchdog timer caused a reset
00027  
00028 // Simon's Watchdog code from
00029 // http://mbed.org/forum/mbed/topic/508/
00030 class Watchdog {
00031 public:
00032 // Load timeout value in watchdog timer and enable
00033     void kick(float s) {
00034         LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
00035         uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
00036         LPC_WDT->WDTC = s * (float)clk;
00037         LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
00038         kick();
00039     }
00040 // "kick" or "feed" the dog - reset the watchdog timer
00041 // by writing this required bit pattern
00042     void kick() {
00043         LPC_WDT->WDFEED = 0xAA;
00044         LPC_WDT->WDFEED = 0x55;
00045     }
00046 };
00047 Watchdog wdt;
00048 
00049 int main()
00050 {
00051     int count = 0;
00052 // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
00053     if ((LPC_WDT->WDMOD >> 2) & 1)
00054         myled4 = 1; else myled3 = 1;
00055         
00056 // setup a 10 second timeout on watchdog timer hardware
00057 // needs to be longer than worst case main loop exection time
00058     wdt.kick(5.0);  
00059     // 2
00060     dip_switch1.mode(PullUp); // configure internal pull up resistor
00061     dip_switch2.mode(PullUp);
00062     
00063     green_led.period(0.016f); // ~ 60 Hz
00064     green_led.write(0.50f); // 50 % duty cycle
00065     
00066     // 3
00067     chip.direction(PORT_A, 0x00); // set all Port A bits to output
00068     chip.direction(PORT_B, 0xFF); // set all B to input
00069     switch_state = 0;
00070     
00071     while(1) {
00072         // 1
00073         red_led = dip_switch3;
00074         
00075         // 2
00076         if (!dip_switch1) {
00077             green_led.write(0.25f); // half start brightness, half duty cycle
00078         }
00079         else if (!dip_switch2) {
00080             green_led.write(0.125f); // 1/4 starting brightness
00081         } else {
00082             green_led.write(0.50f); // initial
00083         }
00084         
00085         // 3
00086         // get the state of the switch (0 or 1) and flip it, 0 when off
00087         switch_state = !chip.read(PORT_B) & 0x01;
00088         // write the saved value to MCP23S17 Port A
00089         chip.write(PORT_A, switch_state);
00090         wait(.2);
00091         // yellow led should change opposite the green led
00092         if (count == 12) while (1) {};
00093         // LED 2 will stay on during the fault
00094         count ++;
00095         // End of main loop so "kick" to reset watchdog timer and avoid a reset
00096         wdt.kick();
00097         // RGB led control
00098         rgb_r = control_pot;
00099         rgb_g = 0.5f - control_pot;
00100         rgb_b = 1.0f - control_pot;
00101     }
00102 }