LAB 3 DAC

Fork of DACandticker_sample by William Marsh

Committer:
WilliamMarshQMUL
Date:
Wed Jan 24 22:56:54 2018 +0000
Revision:
2:e27fd3b65155
Parent:
1:18e0f8aef32f
Child:
3:1df612e3c838
Update for mbed 5

Who changed what in which revision?

UserRevisionLine numberNew 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
WilliamMarshQMUL 0:5307f49cd305 9
WilliamMarshQMUL 0:5307f49cd305 10 #include "mbed.h"
WilliamMarshQMUL 0:5307f49cd305 11 #include "sineTable.h"
WilliamMarshQMUL 0:5307f49cd305 12
WilliamMarshQMUL 0:5307f49cd305 13 Ticker tick ; // Creates periodic interrupt
WilliamMarshQMUL 0:5307f49cd305 14 AnalogOut ao(PTE30) ; // Analog output
WilliamMarshQMUL 0:5307f49cd305 15
WilliamMarshQMUL 1:18e0f8aef32f 16 // Function called periodically
WilliamMarshQMUL 1:18e0f8aef32f 17 // Write new value to AnalogOut
WilliamMarshQMUL 2:e27fd3b65155 18 volatile int index = 0 ; // index into array of sin values
WilliamMarshQMUL 0:5307f49cd305 19 void writeAout() {
WilliamMarshQMUL 0:5307f49cd305 20 ao.write_u16(sine[index]) ;
WilliamMarshQMUL 0:5307f49cd305 21 index = (index + 1) % 64 ;
WilliamMarshQMUL 0:5307f49cd305 22 }
WilliamMarshQMUL 0:5307f49cd305 23
WilliamMarshQMUL 0:5307f49cd305 24 // Control the frequency of updates
WilliamMarshQMUL 0:5307f49cd305 25 // Alternative between two frequencies
WilliamMarshQMUL 0:5307f49cd305 26 int main() {
WilliamMarshQMUL 1:18e0f8aef32f 27 int update_us = 1000 ; // 1ms
WilliamMarshQMUL 0:5307f49cd305 28 while (true) {
WilliamMarshQMUL 0:5307f49cd305 29 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 30 Thread::wait(30000) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 1:18e0f8aef32f 31 update_us = 2000 ; // 2ms
WilliamMarshQMUL 0:5307f49cd305 32 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 33 Thread::wait(30000) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 1:18e0f8aef32f 34 update_us = 1000 ; // 1ms
WilliamMarshQMUL 0:5307f49cd305 35 }
WilliamMarshQMUL 0:5307f49cd305 36 }