Proposed solution to lab 3

Files at this revision

API Documentation at this revision

Comitter:
sebbarpar
Date:
Thu Feb 27 09:47:08 2020 +0000
Parent:
5:1f36332ed463
Commit message:
Lab 3;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sineTable.h Show annotated file Show diff for this revision Revisions of this file
diff -r 1f36332ed463 -r a5841dd9e3b2 main.cpp
--- a/main.cpp	Thu Feb 06 08:11:53 2020 +0000
+++ b/main.cpp	Thu Feb 27 09:47:08 2020 +0000
@@ -1,13 +1,34 @@
 
-// LAB 3 SAMPLE PROGRAM 1
-//   Revised for mbed 5 
-//   Revised to replace Ticker with event queue and thread
+// LAB 3 
+//After pressing the button, maximum voltage is set.
+//Divided by 5(5 LEDs) and each LED assigned a threshold.
+//LED is turned on when threshold is surpassed.
 
 #include "mbed.h"
+#include "sineTable.h"
 
+class AnalogOut_unsafe : public AnalogOut {
+  public:
+    AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {} 
+  protected: 
+    virtual void lock() {}
+    virtual void unlock() {}
+};
+// --------------------------
+
+Ticker tick ;          // Creates periodic interrupt
+AnalogOut_unsafe ao(PTE30) ;  // Analog output
 
 AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+//Connected LEDs
+DigitalOut led1(PTE1);     
+DigitalOut led2(PTE0);
+DigitalOut led3(PTD7);
+DigitalOut led4(PTD6);
+DigitalOut led5(PTE31);
+//Button to set maximum voltage
+InterruptIn button(PTD5); //Button 1
+
 EventQueue queue;  // creates an event queue, to call read ADC
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
@@ -28,6 +49,12 @@
 // Every 10th value is sent to mailbox
 volatile int samples = 0 ;
 volatile uint16_t smoothed = 0 ; 
+volatile int buttonEvent=0;
+
+//Interrupt to detect button switch
+void buttonCallback(){
+    buttonEvent=1;  
+}
 
 void readA0() {
     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
@@ -53,36 +80,78 @@
     s[0] = '0' + (v % 10) ;
 }
 
+
+
+
+// Function called periodically
+// Write new value to AnalogOut 
+volatile int index = 0 ; // index into array of sin values
+void writeAout() {
+    ao.write_u16(sine[index]) ;
+    index = (index + 1) % 64 ;   
+}
+
+volatile int t=312;
+volatile int freq=1;
+
 // Main program
 //   Initialise variables
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
-    led1 = 1 ; // turn off 
+    led1=0 ; // turn off 
+    led2=0;
+    led3=0;
+    led4=0;
+    led5=0;
     int volts = 0 ;
-    const int threshold = 100 ;
+    int i=0;
+    int threshold[5]={400,400,400,400,400};
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
+    char max[] = "X.XX\r\n" ;//For debugging
+    int maxvalue=1;
+    button.fall(&buttonCallback) ; 
     
     // Start the event queue
     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
-
     // call the readA0 function every 10ms 
     queue.call_every(10, readA0) ; 
-
+    
     while (true) {
         osEvent evt = mailbox.get(); // wait for mail 
+        tick.attach_us(callback(&writeAout), t); // setup ticker to write to AnalogOut
+        //pc.printf("%d",t); //For debugging
         if (evt.status == osEventMail) {
             message_t* mess = (message_t*)evt.value.p ;
+             if (buttonEvent==1){//When button is pressed establish maximum value and thresholds
+                buttonEvent=0;
+                maxvalue=(mess->analog * 330) / 0xffff;
+                for (i=1;i<=5;i++){
+                    threshold[i-1]=maxvalue*i/5;//Set values for thresholds
+                }
+            }
             volts = (mess->analog * 330) / 0xffff ;
+            //Calculate frequency to call the function to write the DAC
+            freq=1+(49*volts/maxvalue);
+            if (freq>50) freq=50;
+            t=(1000000/(freq*64));
             mailbox.free(mess) ;  // free the message space
-            if (volts > threshold) led1 = 0 ; else led1 = 1 ;
+            //Turn on LEDs depending on the range of voltage
+            if (volts < threshold[0]) led1 = 0 ; else led1 = 1 ;
+            if (volts < threshold[1]) led2 = 0 ; else led2 = 1 ;
+            if (volts < threshold[2]) led3 = 0 ; else led3 = 1 ;
+            if (volts < threshold[3]) led4 = 0 ; else led4 = 1 ;
+            if (volts < threshold[4]) led5 = 0 ; else led5 = 1 ;
             vToString(volts, vstring) ;
+            vToString(maxvalue, max) ;
             counter++ ;
-            if (counter == 10) {  // limit bandwidth of serial
-                pc.printf(vstring) ;
+            /*if (counter == 10) {  // limit bandwidth of serial
+                //pc.printf(vstring) ;
+                pc.printf("%d \n",freq);
+                pc.printf("%d \n",t);
                 counter = 0 ;
-            }
+            }*/ //For debugging
         }
     }
 }
diff -r 1f36332ed463 -r a5841dd9e3b2 sineTable.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sineTable.h	Thu Feb 27 09:47:08 2020 +0000
@@ -0,0 +1,15 @@
+// Look up table for a sine wave as 16 bits
+// Note that it is possible to construct the full period from 
+//   the first quarter, but the code is more complex
+//
+// These number were calcuated using a spreadsheet 
+const uint16_t sine[] = {
+    32768, 35980, 39161, 42280, 45308, 48215, 50973, 53556, 
+    55938, 58098, 60014, 61667, 63042, 64125, 64906, 65378, 
+    65535, 65378, 64906, 64125, 63042, 61667, 60014, 58098,
+    55938, 53556, 50973, 48215, 45308, 42280, 39161, 35980,
+    32768, 29556, 26375, 23256, 20228, 17321, 14563, 11980,
+     9598,  7438,  5522,  3869,  2494,  1411,   630,   158, 
+        0,   158,   630,  1411,  2494,  3869,  5522,  7438,
+     9598, 11980, 14563, 17321, 20228, 23256, 26375, 29556
+    } ;
\ No newline at end of file