![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Digital to analog conversion. Reads analog signal (voltage) and depending on it, sets the frequency to produce the analog output for sine wave.
Fork of DACandticker_sample by
main.cpp@1:18e0f8aef32f, 2017-02-01 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Wed Feb 01 13:53:26 2017 +0000
- Revision:
- 1:18e0f8aef32f
- Parent:
- 0:5307f49cd305
- Child:
- 2:e27fd3b65155
Version without debug
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 | 1:18e0f8aef32f | 6 | // THIS VERSION HAS NO DEBUGGING CODE |
WilliamMarshQMUL | 0:5307f49cd305 | 7 | |
WilliamMarshQMUL | 0:5307f49cd305 | 8 | #include "mbed.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 9 | #include "rtos.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 10 | #include "sineTable.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 11 | |
WilliamMarshQMUL | 0:5307f49cd305 | 12 | Ticker tick ; // Creates periodic interrupt |
WilliamMarshQMUL | 0:5307f49cd305 | 13 | AnalogOut ao(PTE30) ; // Analog output |
WilliamMarshQMUL | 0:5307f49cd305 | 14 | |
WilliamMarshQMUL | 1:18e0f8aef32f | 15 | // Function called periodically |
WilliamMarshQMUL | 1:18e0f8aef32f | 16 | // Write new value to AnalogOut |
WilliamMarshQMUL | 0:5307f49cd305 | 17 | volatile int index = 0 ; // this variable is not just for debugging!! |
WilliamMarshQMUL | 0:5307f49cd305 | 18 | void writeAout() { |
WilliamMarshQMUL | 0:5307f49cd305 | 19 | ao.write_u16(sine[index]) ; |
WilliamMarshQMUL | 0:5307f49cd305 | 20 | index = (index + 1) % 64 ; |
WilliamMarshQMUL | 0:5307f49cd305 | 21 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 22 | |
WilliamMarshQMUL | 0:5307f49cd305 | 23 | // Control the frequency of updates |
WilliamMarshQMUL | 0:5307f49cd305 | 24 | // Alternative between two frequencies |
WilliamMarshQMUL | 0:5307f49cd305 | 25 | int main() { |
WilliamMarshQMUL | 1:18e0f8aef32f | 26 | int update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 27 | while (true) { |
WilliamMarshQMUL | 0:5307f49cd305 | 28 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 0:5307f49cd305 | 29 | Thread::wait(30000) ; // wait 30 sec - 30000ms |
WilliamMarshQMUL | 1:18e0f8aef32f | 30 | update_us = 2000 ; // 2ms |
WilliamMarshQMUL | 0:5307f49cd305 | 31 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 0:5307f49cd305 | 32 | Thread::wait(30000) ; // wait 30 sec - 30000ms |
WilliamMarshQMUL | 1:18e0f8aef32f | 33 | update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 34 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 35 | } |