![](/media/cache/profiles/8631e5b91354a846f00dbbf16ad94259.jpg.50x50_q85.jpg)
Lab 3 example 2, no debugging
Fork of DACandticker_sample_with_debug by
DAC Ticker
- Sample code for part 2 of lab 3 using the DAC, called from a Ticker.
- Note that to use AnalogOut from the Ticker (an ISR), we create a version without locking.
main.cpp@5:5161dd5f273e, 2020-02-06 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Thu Feb 06 09:03:54 2020 +0000
- Revision:
- 5:5161dd5f273e
- Parent:
- 4:75ad475aff41
Update mbed. Repace wait with sleep for
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 | 4:75ad475aff41 | 5 | // |
WilliamMarshQMUL | 4:75ad475aff41 | 6 | // This code uses the Ticker API to create a periodic interrupt |
WilliamMarshQMUL | 4:75ad475aff41 | 7 | // Devices used in this way must not call locks; a variant of |
WilliamMarshQMUL | 4:75ad475aff41 | 8 | // the AnalogOut device w/o locking is created for this. |
WilliamMarshQMUL | 0:5307f49cd305 | 9 | // |
WilliamMarshQMUL | 2:e27fd3b65155 | 10 | // Updated for mbed 5 |
WilliamMarshQMUL | 2:e27fd3b65155 | 11 | |
WilliamMarshQMUL | 1:18e0f8aef32f | 12 | // THIS VERSION HAS NO DEBUGGING CODE |
WilliamMarshQMUL | 0:5307f49cd305 | 13 | |
WilliamMarshQMUL | 0:5307f49cd305 | 14 | #include "mbed.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 15 | #include "sineTable.h" |
WilliamMarshQMUL | 0:5307f49cd305 | 16 | |
WilliamMarshQMUL | 4:75ad475aff41 | 17 | // -------------------------- |
WilliamMarshQMUL | 4:75ad475aff41 | 18 | // This declaration introduces a derived version of the mbed AnalogOut |
WilliamMarshQMUL | 4:75ad475aff41 | 19 | // class with the locking removed. |
WilliamMarshQMUL | 4:75ad475aff41 | 20 | // You do NOT NEED TO UNDERSTAND this |
WilliamMarshQMUL | 4:75ad475aff41 | 21 | class AnalogOut_unsafe : public AnalogOut { |
WilliamMarshQMUL | 4:75ad475aff41 | 22 | public: |
WilliamMarshQMUL | 4:75ad475aff41 | 23 | AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {} |
WilliamMarshQMUL | 4:75ad475aff41 | 24 | protected: |
WilliamMarshQMUL | 4:75ad475aff41 | 25 | virtual void lock() {} |
WilliamMarshQMUL | 4:75ad475aff41 | 26 | virtual void unlock() {} |
WilliamMarshQMUL | 4:75ad475aff41 | 27 | }; |
WilliamMarshQMUL | 4:75ad475aff41 | 28 | // -------------------------- |
WilliamMarshQMUL | 4:75ad475aff41 | 29 | |
WilliamMarshQMUL | 0:5307f49cd305 | 30 | Ticker tick ; // Creates periodic interrupt |
WilliamMarshQMUL | 4:75ad475aff41 | 31 | AnalogOut_unsafe ao(PTE30) ; // Analog output |
WilliamMarshQMUL | 0:5307f49cd305 | 32 | |
WilliamMarshQMUL | 1:18e0f8aef32f | 33 | // Function called periodically |
WilliamMarshQMUL | 1:18e0f8aef32f | 34 | // Write new value to AnalogOut |
WilliamMarshQMUL | 2:e27fd3b65155 | 35 | volatile int index = 0 ; // index into array of sin values |
WilliamMarshQMUL | 0:5307f49cd305 | 36 | void writeAout() { |
WilliamMarshQMUL | 0:5307f49cd305 | 37 | ao.write_u16(sine[index]) ; |
WilliamMarshQMUL | 0:5307f49cd305 | 38 | index = (index + 1) % 64 ; |
WilliamMarshQMUL | 0:5307f49cd305 | 39 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 40 | |
WilliamMarshQMUL | 0:5307f49cd305 | 41 | // Control the frequency of updates |
WilliamMarshQMUL | 0:5307f49cd305 | 42 | // Alternative between two frequencies |
WilliamMarshQMUL | 0:5307f49cd305 | 43 | int main() { |
WilliamMarshQMUL | 1:18e0f8aef32f | 44 | int update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 45 | while (true) { |
WilliamMarshQMUL | 0:5307f49cd305 | 46 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 5:5161dd5f273e | 47 | ThisThread::sleep_for(30000) ; // wait 30 sec |
WilliamMarshQMUL | 1:18e0f8aef32f | 48 | update_us = 2000 ; // 2ms |
WilliamMarshQMUL | 0:5307f49cd305 | 49 | tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut |
WilliamMarshQMUL | 5:5161dd5f273e | 50 | ThisThread::sleep_for(30000) ; // wait 30 sec |
WilliamMarshQMUL | 1:18e0f8aef32f | 51 | update_us = 1000 ; // 1ms |
WilliamMarshQMUL | 0:5307f49cd305 | 52 | } |
WilliamMarshQMUL | 0:5307f49cd305 | 53 | } |