Manuel Campos
/
DACandticker_samplecvmanuel
DAC manuel
Fork of DACandticker_sample by
main.cpp@3:b9a275ca5ba0, 2018-02-09 (annotated)
- Committer:
- cvmanuel
- Date:
- Fri Feb 09 23:39:20 2018 +0000
- Revision:
- 3:b9a275ca5ba0
- Parent:
- 2:e27fd3b65155
initial version, no changes made
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:5307f49cd305 | 1 | // Lab 3 Example Program 2 |
WilliamMarshQMUL | 0:5307f49cd305 | 2 | // ----------------------- |
WilliamMarshQMUL | 0:5307f49cd305 | 3 | // Periodically write to the AnalogOut to create a sine wave |
WilliamMarshQMUL | 0:5307f49cd305 | 4 | // Alternate between two fixed frequencies every 5 sec |
WilliamMarshQMUL | 0:5307f49cd305 | 5 | // |
WilliamMarshQMUL | 2:e27fd3b65155 | 6 | // Updated for mbed 5 |
WilliamMarshQMUL | 2:e27fd3b65155 | 7 | |
WilliamMarshQMUL | 1:18e0f8aef32f | 8 | // THIS VERSION HAS NO DEBUGGING CODE |
cvmanuel | 3:b9a275ca5ba0 | 9 | //TEST |
WilliamMarshQMUL | 0:5307f49cd305 | 10 | |
WilliamMarshQMUL | 0:5307f49cd305 | 11 | #include "mbed.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 12 | #include "sineTable.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 13 | |
WilliamMarshQMUL | 0:5307f49cd305 | 14 | Ticker tick ; // Creates periodic interrupt |
WilliamMarshQMUL | 0:5307f49cd305 | 15 | AnalogOut ao(PTE30) ; // Analog output |
WilliamMarshQMUL | 0:5307f49cd305 | 16 | |
WilliamMarshQMUL | 1:18e0f8aef32f | 17 | // Function called periodically |
WilliamMarshQMUL | 1:18e0f8aef32f | 18 | // Write new value to AnalogOut |
WilliamMarshQMUL | 2:e27fd3b65155 | 19 | volatile int index = 0 ; // index into array of sin values |
WilliamMarshQMUL | 0:5307f49cd305 | 20 | void writeAout() { |
WilliamMarshQMUL | 0:5307f49cd305 | 21 | ao.write_u16(sine[index]) ; |
WilliamMarshQMUL | 0:5307f49cd305 | 22 | index = (index + 1) % 64 ; |
WilliamMarshQMUL | 0:5307f49cd305 | 23 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 24 | |
WilliamMarshQMUL | 0:5307f49cd305 | 25 | // Control the frequency of updates |
WilliamMarshQMUL | 0:5307f49cd305 | 26 | // Alternative between two frequencies |
WilliamMarshQMUL | 0:5307f49cd305 | 27 | int main() { |
WilliamMarshQMUL | 1:18e0f8aef32f | 28 | int update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 29 | while (true) { |
WilliamMarshQMUL | 0:5307f49cd305 | 30 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 0:5307f49cd305 | 31 | Thread::wait(30000) ; // wait 30 sec - 30000ms |
WilliamMarshQMUL | 1:18e0f8aef32f | 32 | update_us = 2000 ; // 2ms |
WilliamMarshQMUL | 0:5307f49cd305 | 33 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 0:5307f49cd305 | 34 | Thread::wait(30000) ; // wait 30 sec - 30000ms |
WilliamMarshQMUL | 1:18e0f8aef32f | 35 | update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 36 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 37 | } |