Nicholas Outram
/
Task532Solution
Task 5.2.3 Solution
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 #define Fs 1000 00004 #define T (1.0/Fs) 00005 #define PI 3.1415926541 00006 00007 PwmOut mypwm(PWM_OUT); 00008 00009 //This GPIO is used for Chip Select 00010 DigitalOut DAC_CS(D10); 00011 00012 //SPI Object 00013 SPI spi(D11, D12, D13); 00014 00015 //Ticker for setting the output sampling rate 00016 Ticker t; 00017 00018 //Discrete time 00019 unsigned int n=0; 00020 00021 //Frequency f 00022 double f = 10.0; 00023 00024 //Prototype for the ticker ISR 00025 void writeSample(); 00026 00027 int main() { 00028 00029 //Set speed of the SPI interface 00030 spi.frequency(20000); 00031 00032 //16 bit words, mode 0 clock 00033 spi.format(16,0); 00034 00035 //Write at Fs Hz 00036 t.attach(writeSample, T); 00037 00038 while(1) { 00039 sleep(); 00040 } 00041 } 00042 00043 //ISR for ticker 00044 void writeSample() 00045 { 00046 //Enable the selected slave device 00047 DAC_CS = 0; 00048 00049 //Write a header (top 4 bits) and value (bottom 12 bits) 00050 double y = cos(2.0*PI*n*f*T); 00051 unsigned int val = (unsigned int)(2048.0 * (y + 1.0)); 00052 spi.write(0x7000 | val); 00053 00054 //Disable the selected slave device (and update the output) 00055 DAC_CS = 1; 00056 00057 n++; 00058 }
Generated on Fri Jul 15 2022 14:45:28 by 1.7.2