Nicholas Outram
/
Task531
Task 5.3.1
main.cpp@1:60594d016407, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:56:45 2017 +0000
- Revision:
- 1:60594d016407
- Parent:
- 0:ee386c3c3a80
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:ee386c3c3a80 | 1 | #include "mbed.h" |
noutram | 0:ee386c3c3a80 | 2 | //#include "spi.h" |
noutram | 0:ee386c3c3a80 | 3 | |
noutram | 0:ee386c3c3a80 | 4 | PwmOut mypwm(PWM_OUT); |
noutram | 0:ee386c3c3a80 | 5 | |
noutram | 0:ee386c3c3a80 | 6 | //This GPIO is used for Chip Select |
noutram | 0:ee386c3c3a80 | 7 | DigitalOut DAC_CS(D10); |
noutram | 0:ee386c3c3a80 | 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 | 0:ee386c3c3a80 | 46 | } |