4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Tue Jan 21 20:02:09 2020 +0000
Parent:
6:e346d2a3010f
Child:
8:a5689a2b6af3
Commit message:
parts 7-9

Changed in this revision

MCP23S17.lib Show annotated file Show diff for this revision Revisions of this file
assembly_ec.s Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
part4.h Show annotated file Show diff for this revision Revisions of this file
part7.h Show annotated file Show diff for this revision Revisions of this file
part8.h Show annotated file Show diff for this revision Revisions of this file
part9.h Show annotated file Show diff for this revision Revisions of this file
watchdog_ec.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.lib	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/romilly/code/MCP23S17/#068b1e8909bb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/assembly_ec.s	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,30 @@
+//    AREA asm_func, CODE, READONLY
+//; Export my_asm function location so that C compiler can find it and link
+//    EXPORT assembly_ec
+//assembly_ec
+//;
+//; ARM Assembly language function to set LED1 bit to a value passed from C   
+//; Pushbutton is on GPIO port 2.4 (connected to p22)
+//; Led is on GPIO port 2.0 (connected to p26)
+//; See Chapter 9 in the LPC1768 User Manual
+//; for all of the GPIO register info and addresses
+//; Pinnames.h has the mbed modules pin port and bit connections
+//;
+//
+//; Load GPIO Port 2 base address in register R1
+//    LDR     R1, =0x2009C040 ; 0x2009C040 = GPIO port 2 base address
+//; Load value from pushbutton into register R0
+//    LDR     R0, R1
+//; Move bit mask in register R2 for bit 4 only
+//    MOV.W   R2, #0x000010   ; 0x000010 = 1<<4 all "0"s with a "1" in bit 4
+//; See if pushbutton is pressed
+//    CMP     R0, #0
+//; (If-Then-Else) on next two instructions using equal cond from the zero flag
+//    ITE EQ
+//; STORE if EQ - clear led 1 port bit using GPIO FIOCLR register and mask
+//    STREQ   R2, [R1,#0x58]  ; if==0, set LED1 bit
+//; STORE if NE - set led 1 port bit using GPIO FIOSET register and mask
+//    STRNE   R2, [R1,#0x5C]  ; if==1, clear LED1 bit
+//; Return to C using link register (Branch indirect using LR - a return)
+//    BX      LR
+//    END
\ No newline at end of file
--- a/main.cpp	Wed Jan 15 20:06:00 2020 +0000
+++ b/main.cpp	Tue Jan 21 20:02:09 2020 +0000
@@ -1,58 +1,63 @@
 #include "mbed.h"
 #include "RGBLed.h"
 #include "PinDetect.h"
-#include "part4.h"
+//#include "part4.h"
+//#include "part6.h"
+#include "part7.h"
+//#include "part8.h"
+//#include "part9.h"
+//#include "watchdog_ec.h"
 
-DigitalOut myled(p26);
-PwmOut builtinLED(LED1);
-DigitalIn pb(p22);
+//DigitalOut myled(p26);
+//PwmOut builtinLED(LED1);
+//DigitalIn pb(p22);
 //DigitalIn pb2(p21);
 
 // p20 is most significant bit
-PinDetect redSelect(p18);
-PinDetect blueSelect(p18);
-PinDetect greenSelect(p20);
-
-PinName redPin = p24;
-PinName greenPin = p25;
-PinName bluePin = p23;
-
-PinDetect pb1(p21);
-PinDetect pb2(p22);
+//PinDetect redSelect(p18);
+//PinDetect blueSelect(p19);
+//PinDetect greenSelect(p20);
+//
+//PinName redPin = p24;
+//PinName greenPin = p25;
+//PinName bluePin = p23;
+//
+//PinDetect pb1(p21);
+//PinDetect pb2(p22);
 
 float volatile p = 1.0f;
 
 void pb1_hit_callback() {
     if (p > 0) {
-        p -= 0.1;
+        p -= 0.1f;
     }
 }
 
 void pb2_hit_callback() {
     if (p < 1) {
-        p += 0.1;
+        p += 0.1f;
     }
 }
 
-RGBLed myRGBLed = RGBLed(redPin, greenPin, bluePin);
+//RGBLed myRGBLed = RGBLed(redPin, greenPin, bluePin);
 
 int main() {
     
-    pb1.mode(PullUp);
-    pb2.mode(PullUp);
-    redSelect.mode(PullUp);
-    greenSelect.mode(PullUp);
-    blueSelect.mode(PullUp);
-    wait(0.1);
-    
-    pb1.attach_asserted(&pb1_hit_callback);
-    pb2.attach_asserted(&pb2_hit_callback);
-    pb1.setSampleFrequency();
-    pb2.setSampleFrequency();
-    
-    float redVal = 0.0f;
-    float greenVal = 0.0f;
-    float blueVal = 0.0f;
+//    pb1.mode(PullUp);
+//    pb2.mode(PullUp);
+//    redSelect.mode(PullUp);
+//    greenSelect.mode(PullUp);
+//    blueSelect.mode(PullUp);
+//    wait(0.1);
+//    
+//    pb1.attach_asserted(&pb1_hit_callback);
+//    pb2.attach_asserted(&pb2_hit_callback);
+//    pb1.setSampleFrequency();
+//    pb2.setSampleFrequency();
+//    
+//    float redVal = 0.0f;
+//    float greenVal = 0.0f;
+//    float blueVal = 0.0f;
     
     while(1) {
         // Part 1
@@ -63,26 +68,43 @@
 //        wait(0.5);
         
         // Part 3
+//        if (!redSelect) {
+//            redVal = 1.0f * p;
+//        } else {
+//            redVal = 0.0f;
+//        }
+//        if (!greenSelect) {
+//            greenVal = 1.0f * p;
+//        } else {
+//            greenVal = 0.0f;
+//        }
+//        if (!blueSelect) {
+//            blueVal = 1.0f * p;
+//        } else {
+//            blueVal = 0.0f;
+//        }
+//        myRGBLed.write(redVal, greenVal, blueVal);
+//        wait(0.5);
         
-        if (!redSelect) {
-            redVal = 1.0 * p;
-        } else {
-            redVal = 0.0f;
-        }
-        if (!greenSelect) {
-            greenVal = 1.0 * p;
-        } else {
-            greenVal = 0.0f;
-        }
-        if (!blueSelect) {
-            blueVal = 1.0 * p;
-        } else {
-            blueVal = 0.0f;
-        }
-        myRGBLed.write(redVal, greenVal, blueVal);
-        wait(0.5);
+        // Part 4
+//        run_part4();
+        
+        // Part 5 in separate project
+        
+        // Part 6 in separate project
         
-        run();
+        // Part 7
+        run_part7();
+
+        // Part 8
+//        run_part8();
 
+        // Part 9
+//        run_part9();
+
+        // Part 10 in separate project
+        
+        // Watchdog Extra Credit
+        
     }
 }
--- a/part4.h	Wed Jan 15 20:06:00 2020 +0000
+++ b/part4.h	Tue Jan 21 20:02:09 2020 +0000
@@ -61,7 +61,7 @@
  
 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
  
-int run()
+int run_part4()
 {
     while(1) {
         //with pullups a button hit is a "0" - "~" inverts data to leds
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part7.h	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,61 @@
+// A simple IO demo using the MCP23S17 library
+//
+// MCP23S17 Library Copyright (c) 2010 Romilly Cocking
+// Released under the MIT License: http://mbed.org/license/mit
+//
+// See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
+//
+//
+// MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
+// uses MCP23S17 library version 0.4
+
+#include "mbed.h"
+#include "MCP23S17.h"
+// Create SPI bus
+SPI spi(p5, p6, p7);
+// 
+// Wiring Connections:
+// mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
+// mbed p20 to MCP23S17 CS
+// MCP23S17 reset pin pulled high
+// MCP23S17 GPA0 connected to GPB0 for loopback test
+// A0, A1, A2 of the MCP23S17  are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
+// This is referred to as the opcode in the device datasheet
+char Opcode = 0x40;
+
+// Next create a MCP23S17
+// mbed p20 is connected to ~chipSelect on the MCP23S17
+MCP23S17 chip = MCP23S17(spi, p20, Opcode);
+
+// Optional software reset - mbed p14 to MCP23S17 reset pin
+// DigitalOut reset(p14);
+
+DigitalOut myled(p26);
+DigitalIn pb(p22);
+
+int run_part7() {
+//  The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
+//  The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
+//  But just leave it pulled high for this simple demo code.
+//  After a power on reset, both IO ports default to input mode
+//
+//  Here is the optional code for a software reset
+//  reset = 0;
+//  wait_us(1);
+//  reset = 1;
+//
+//  Set all 8 Port A bits to output direction
+    chip.direction(PORT_A, 0x00);
+//  Set all 8 Port B bits to input direction
+    chip.direction(PORT_B, 0xFF);
+    myled=0;
+//  Start Loopback test sending out and reading back values
+//  loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
+    while (1) {
+        // write value of pushbutton to Port A
+        chip.write(PORT_A, !pb);
+        wait(.5);
+        // read back value from MCP23S17 Port B and display B0 on myled
+        myled = chip.read(PORT_B)& 0x01;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part8.h	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+PwmOut myled(LED1);
+AnalogIn mypotentiometer(p20);
+
+int run_part8()
+{
+    while(1) {
+        myled = mypotentiometer;
+        wait(0.01);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part9.h	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+
+#define PI 3.14159265
+#define N_SAMPLES 20
+
+AnalogOut aOut(pin);
+
+int run_part9() {
+    while (1) {
+        for (double i = 0; i < 2*PI; i += (2*PI/N_SAMPLES) {
+            aOut = (sin(i) + 1.0) * (3.3 / 2.0);
+            wait(0.1);
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/watchdog_ec.h	Tue Jan 21 20:02:09 2020 +0000
@@ -0,0 +1,49 @@
+#include "mbed.h"
+
+class Watchdog {
+public:
+// Load timeout value in watchdog timer and enable
+    void kick(float s) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = s * (float)clk;
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        kick();
+    }
+// "kick" or "feed" the dog - reset the watchdog timer
+// by writing this required bit pattern
+    void kick() {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
+ 
+// Setup the watchdog timer
+Watchdog wdt;
+
+DigitalIn pb(p22);
+DigitalOut myled(p26);
+
+int prev;
+int count = 0;
+
+int run_watchdogEC() {
+    wdt.kick(10.0);
+    
+    prev = pb;
+    
+    while(1) {
+        if (prev != pb) {
+            count++;
+        }
+        myled = !pb;
+        
+        if (count == 2) {
+            while (1) {
+                ;
+            }
+        }
+        
+        wdt.kick();
+    }
+}
\ No newline at end of file