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

Dependencies:   MCP23S17 mbed

Revision:
0:f58f38896cfa
Child:
1:cd5247f3a499
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Sep 09 22:14:06 2016 +0000
@@ -0,0 +1,55 @@
+// code for part 1, 2, & 3
+#include "mbed.h"
+#include "MCP23S17.h"
+
+// 1
+DigitalIn dip_switch8(p21);
+DigitalOut green_led(p22);
+// 2
+DigitalIn dip_switch1(p26);
+DigitalIn dip_switch2(p25);
+PwmOut blue_led(p23);
+// 3
+SPI spi(p5, p6, p7); // SPI bus
+char Opcode = 0x40; // 8-bit address for writes
+MCP23S17 chip = MCP23S17(spi, p20, Opcode); // create an MCP23S17
+
+int switch_state;
+
+int main()
+{
+    // 2
+    dip_switch1.mode(PullUp); // configure internal pull up resistor
+    dip_switch2.mode(PullUp);
+    
+    blue_led.period(0.016f); // ~ 60 Hz
+    blue_led.write(0.50f); // 50 % duty cycle
+    
+    // 3
+    chip.direction(PORT_A, 0x00); // set all Port A bits to output
+    chip.direction(PORT_B, 0xFF); // set all B to input
+    switch_state = 0;
+    
+    while(1) {
+        // 1
+        green_led = dip_switch8;
+        
+        // 2
+        if (!dip_switch1) {
+            blue_led.write(0.25f); // half start brightness, half duty cycle
+        }
+        else if (!dip_switch2) {
+            blue_led.write(0.125f); // 1/4 starting brightness
+        } else {
+            blue_led.write(0.50f); // initial
+        }
+        
+        // 3
+        // get the state of the switch (0 or 1) and flip it, 0 when off
+        switch_state = !chip.read(PORT_B) & 0x01;
+        // write the saved value to MCP23S17 Port A
+        chip.write(PORT_A, switch_state);
+        wait(.2);
+        // yellow led should change opposite the green led
+    }
+}