Nicholas Outram / Mbed OS Task531
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //#include "spi.h"
00003 
00004 PwmOut mypwm(PWM_OUT);
00005 
00006 //This GPIO is used for Chip Select
00007 DigitalOut DAC_CS(D10);
00008 
00009 //SPI Object
00010 SPI spi(D11, D12, D13);
00011 
00012 //Ticker for setting the output sampling rate
00013 Ticker t;
00014 
00015 //Prototype for the ticker ISR
00016 void writeSample();
00017 
00018 int main() {
00019 
00020     //Set speed of the SPI interface
00021     spi.frequency(20000);
00022     
00023     //16 bit words, mode 0 clock
00024     spi.format(16,0);
00025     
00026     //Write at 1000Hz    
00027     t.attach(writeSample, 0.001);
00028     
00029     while(1) {
00030         sleep();
00031     }
00032 }
00033 
00034 //ISR for ticker
00035 void writeSample()
00036 {
00037     //Enable the selected slave device
00038     DAC_CS = 0;
00039     
00040     //Write a header (top 4 bits) and value (bottom 12 bits) 
00041     unsigned int val = 2048;
00042     spi.write(0x7000 | val);
00043     
00044     //Disable the selected slave device (and update the output)
00045     DAC_CS = 1;
00046 }