Task 5.2.3 Solution
main.cpp@2:212bbdc6a892, 2019-09-20 (annotated)
- Committer:
- noutram
- Date:
- Fri Sep 20 14:04:09 2019 +0000
- Revision:
- 2:212bbdc6a892
- Parent:
- 0:c616f46e44a5
2019;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:c616f46e44a5 | 1 | #include "mbed.h" |
| noutram | 0:c616f46e44a5 | 2 | |
| noutram | 0:c616f46e44a5 | 3 | #define Fs 1000 |
| noutram | 0:c616f46e44a5 | 4 | #define T (1.0/Fs) |
| noutram | 0:c616f46e44a5 | 5 | #define PI 3.1415926541 |
| noutram | 0:c616f46e44a5 | 6 | |
| noutram | 0:c616f46e44a5 | 7 | |
| noutram | 0:c616f46e44a5 | 8 | //This GPIO is used for Chip Select |
| noutram | 0:c616f46e44a5 | 9 | DigitalOut DAC_CS(D10); |
| noutram | 0:c616f46e44a5 | 10 | |
| noutram | 2:212bbdc6a892 | 11 | //SPI Object (temporarily remove JP6 when running this) |
| noutram | 0:c616f46e44a5 | 12 | SPI spi(D11, D12, D13); |
| noutram | 0:c616f46e44a5 | 13 | |
| noutram | 0:c616f46e44a5 | 14 | //Ticker for setting the output sampling rate |
| noutram | 0:c616f46e44a5 | 15 | Ticker t; |
| noutram | 0:c616f46e44a5 | 16 | |
| noutram | 0:c616f46e44a5 | 17 | //Discrete time |
| noutram | 0:c616f46e44a5 | 18 | unsigned int n=0; |
| noutram | 0:c616f46e44a5 | 19 | |
| noutram | 0:c616f46e44a5 | 20 | //Frequency f |
| noutram | 0:c616f46e44a5 | 21 | double f = 10.0; |
| noutram | 0:c616f46e44a5 | 22 | |
| noutram | 0:c616f46e44a5 | 23 | //Prototype for the ticker ISR |
| noutram | 0:c616f46e44a5 | 24 | void writeSample(); |
| noutram | 0:c616f46e44a5 | 25 | |
| noutram | 0:c616f46e44a5 | 26 | int main() { |
| noutram | 0:c616f46e44a5 | 27 | |
| noutram | 0:c616f46e44a5 | 28 | //Set speed of the SPI interface |
| noutram | 0:c616f46e44a5 | 29 | spi.frequency(20000); |
| noutram | 0:c616f46e44a5 | 30 | |
| noutram | 0:c616f46e44a5 | 31 | //16 bit words, mode 0 clock |
| noutram | 0:c616f46e44a5 | 32 | spi.format(16,0); |
| noutram | 0:c616f46e44a5 | 33 | |
| noutram | 0:c616f46e44a5 | 34 | //Write at Fs Hz |
| noutram | 0:c616f46e44a5 | 35 | t.attach(writeSample, T); |
| noutram | 0:c616f46e44a5 | 36 | |
| noutram | 0:c616f46e44a5 | 37 | while(1) { |
| noutram | 0:c616f46e44a5 | 38 | sleep(); |
| noutram | 0:c616f46e44a5 | 39 | } |
| noutram | 0:c616f46e44a5 | 40 | } |
| noutram | 0:c616f46e44a5 | 41 | |
| noutram | 0:c616f46e44a5 | 42 | //ISR for ticker |
| noutram | 0:c616f46e44a5 | 43 | void writeSample() |
| noutram | 0:c616f46e44a5 | 44 | { |
| noutram | 0:c616f46e44a5 | 45 | //Enable the selected slave device |
| noutram | 0:c616f46e44a5 | 46 | DAC_CS = 0; |
| noutram | 0:c616f46e44a5 | 47 | |
| noutram | 0:c616f46e44a5 | 48 | //Write a header (top 4 bits) and value (bottom 12 bits) |
| noutram | 0:c616f46e44a5 | 49 | double y = cos(2.0*PI*n*f*T); |
| noutram | 0:c616f46e44a5 | 50 | unsigned int val = (unsigned int)(2048.0 * (y + 1.0)); |
| noutram | 0:c616f46e44a5 | 51 | spi.write(0x7000 | val); |
| noutram | 0:c616f46e44a5 | 52 | |
| noutram | 0:c616f46e44a5 | 53 | //Disable the selected slave device (and update the output) |
| noutram | 0:c616f46e44a5 | 54 | DAC_CS = 1; |
| noutram | 0:c616f46e44a5 | 55 | |
| noutram | 0:c616f46e44a5 | 56 | n++; |
| noutram | 0:c616f46e44a5 | 57 | } |