Lab3-task2

Fork of ADCandticker_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
Peilingyi
Date:
Fri Feb 09 23:18:53 2018 +0000
Parent:
1:126dd2f5fc2d
Commit message:
Lab3-2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
sinTable.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jan 24 21:55:43 2018 +0000
+++ b/main.cpp	Fri Feb 09 23:18:53 2018 +0000
@@ -3,11 +3,15 @@
 //   Revised for mbed 5
 
 #include "mbed.h"
+#include "sinTable.h"
 
+Ticker tickout ;          // Creates periodic interrupt
+AnalogOut ao(PTE30) ;  // Analog output
 
 Ticker tick;                // Ticker for reading analog
 AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+DigitalOut led1(LED_RED);
+  
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
 
@@ -48,31 +52,50 @@
     s[0] = '0' + (v % 10) ;
 }
 
+volatile int index = 0 ; // index into array of sin values
+void writeAout() {
+    ao.write_u16(sine[index]) ;
+    index = (index + 1) % 64 ;   
+}
+
 // Main program
 //   Initialise variables
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
+    
+    int update_us = 1000 ;
+    float tempupdate = 0.0;
+    int volts = 0;
     led1 = 1 ; // turn off 
-    int volts = 0 ;
-    const int threshold = 100 ;
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
-
+ 
     tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
-    while (true) {
+   
+
+    while (true) 
+    {  
         osEvent evt = mailbox.get(); // wait for mail 
-        if (evt.status == osEventMail) {
+        if (evt.status == osEventMail) 
+        {
             message_t* mess = (message_t*)evt.value.p ;
             volts = (mess->analog * 330) / 0xffff ;
             mailbox.free(mess) ;  // free the message space
-            if (volts > threshold) led1 = 0 ; else led1 = 1 ;
-            vToString(volts, vstring) ;
-            counter++ ;
-            if (counter == 10) {  // limit bandwidth of serial
-                pc.printf(vstring) ;
+            
+           update_us = 1000000/((1+(49*volts/330))*64) ;
+           tickout.detach() ;
+           tickout.attach_us(callback(&writeAout), update_us);
+
+           // vToString(volts, vstring) ;
+          
+ /*           counter++ ;
+            if (counter == 10) {  // limit bandwidth of 
+                pc.printf(vstring);
+                pc.printf("%d\n",update_us);
                 counter = 0 ;
             }
+            */
         }
 
     }
--- a/mbed-os.lib	Wed Jan 24 21:55:43 2018 +0000
+++ b/mbed-os.lib	Fri Feb 09 23:18:53 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#96d9a00d0a1d25095b330095fa81c40f7741777c
+https://github.com/ARMmbed/mbed-os/#caeaa49d68c67ee00275cece10cd88e0ed0f6ed3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sinTable.h	Fri Feb 09 23:18:53 2018 +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