University of Plymouth - Stages 1, 2 and 3 / Mbed OS Task531
Committer:
noutram
Date:
Fri Sep 20 13:52:32 2019 +0000
Revision:
2:302b3ed98b81
Parent:
0:ee386c3c3a80
Child:
3:3e849d835d69
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:ee386c3c3a80 1 #include "mbed.h"
noutram 0:ee386c3c3a80 2
noutram 0:ee386c3c3a80 3 //This GPIO is used for Chip Select
noutram 0:ee386c3c3a80 4 DigitalOut DAC_CS(D10);
noutram 0:ee386c3c3a80 5
noutram 2:302b3ed98b81 6 //Status LED
noutram 2:302b3ed98b81 7 DigitalOut led(LED1,1);
noutram 2:302b3ed98b81 8
noutram 0:ee386c3c3a80 9 //SPI Object
noutram 0:ee386c3c3a80 10 SPI spi(D11, D12, D13);
noutram 0:ee386c3c3a80 11
noutram 0:ee386c3c3a80 12 //Ticker for setting the output sampling rate
noutram 0:ee386c3c3a80 13 Ticker t;
noutram 0:ee386c3c3a80 14
noutram 0:ee386c3c3a80 15 //Prototype for the ticker ISR
noutram 0:ee386c3c3a80 16 void writeSample();
noutram 0:ee386c3c3a80 17
noutram 0:ee386c3c3a80 18 int main() {
noutram 0:ee386c3c3a80 19
noutram 0:ee386c3c3a80 20 //Set speed of the SPI interface
noutram 0:ee386c3c3a80 21 spi.frequency(20000);
noutram 0:ee386c3c3a80 22
noutram 0:ee386c3c3a80 23 //16 bit words, mode 0 clock
noutram 0:ee386c3c3a80 24 spi.format(16,0);
noutram 0:ee386c3c3a80 25
noutram 0:ee386c3c3a80 26 //Write at 1000Hz
noutram 0:ee386c3c3a80 27 t.attach(writeSample, 0.001);
noutram 0:ee386c3c3a80 28
noutram 0:ee386c3c3a80 29 while(1) {
noutram 0:ee386c3c3a80 30 sleep();
noutram 0:ee386c3c3a80 31 }
noutram 0:ee386c3c3a80 32 }
noutram 0:ee386c3c3a80 33
noutram 0:ee386c3c3a80 34 //ISR for ticker
noutram 0:ee386c3c3a80 35 void writeSample()
noutram 0:ee386c3c3a80 36 {
noutram 0:ee386c3c3a80 37 //Enable the selected slave device
noutram 0:ee386c3c3a80 38 DAC_CS = 0;
noutram 0:ee386c3c3a80 39
noutram 0:ee386c3c3a80 40 //Write a header (top 4 bits) and value (bottom 12 bits)
noutram 0:ee386c3c3a80 41 unsigned int val = 2048;
noutram 0:ee386c3c3a80 42 spi.write(0x7000 | val);
noutram 0:ee386c3c3a80 43
noutram 0:ee386c3c3a80 44 //Disable the selected slave device (and update the output)
noutram 0:ee386c3c3a80 45 DAC_CS = 1;
noutram 2:302b3ed98b81 46
noutram 2:302b3ed98b81 47 //Status
noutram 2:302b3ed98b81 48 led = !led;
noutram 0:ee386c3c3a80 49 }