Task 5.2.3 Solution

Dependencies:   mbed

Fork of Task532Solution by Nicholas Outram

Files at this revision

API Documentation at this revision

Comitter:
noutram
Date:
Thu Sep 24 12:32:26 2015 +0000
Commit message:
Initial version 24-09-2015

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c616f46e44a5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 24 12:32:26 2015 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+
+#define Fs 1000
+#define T (1.0/Fs)
+#define PI 3.1415926541
+
+PwmOut mypwm(PWM_OUT);
+
+//This GPIO is used for Chip Select
+DigitalOut DAC_CS(D10);
+
+//SPI Object
+SPI spi(D11, D12, D13);
+
+//Ticker for setting the output sampling rate
+Ticker t;
+
+//Discrete time
+unsigned int n=0;
+
+//Frequency f
+double f = 10.0;
+
+//Prototype for the ticker ISR
+void writeSample();
+
+int main() {
+
+    //Set speed of the SPI interface
+    spi.frequency(20000);
+    
+    //16 bit words, mode 0 clock
+    spi.format(16,0);
+    
+    //Write at Fs Hz    
+    t.attach(writeSample, T);
+    
+    while(1) {
+        sleep();
+    }
+}
+
+//ISR for ticker
+void writeSample()
+{
+    //Enable the selected slave device
+    DAC_CS = 0;
+    
+    //Write a header (top 4 bits) and value (bottom 12 bits) 
+    double y = cos(2.0*PI*n*f*T);
+    unsigned int val = (unsigned int)(2048.0 * (y + 1.0));
+    spi.write(0x7000 | val);
+    
+    //Disable the selected slave device (and update the output)
+    DAC_CS = 1;
+    
+    n++;
+}
\ No newline at end of file
diff -r 000000000000 -r c616f46e44a5 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 24 12:32:26 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file