Lab 3 example 2, no debugging

Fork of DACandticker_sample_with_debug by William Marsh

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.
Committer:
WilliamMarshQMUL
Date:
Wed Feb 01 13:38:02 2017 +0000
Revision:
0:5307f49cd305
Child:
1:18e0f8aef32f
First version

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 0:5307f49cd305 6 // THIS VERSION HAS DEBUGGING CODE USING THE SERIAL PORT
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 0:5307f49cd305 15 // --- following code for debugging ---
WilliamMarshQMUL 0:5307f49cd305 16 Thread debugT ;
WilliamMarshQMUL 0:5307f49cd305 17 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
WilliamMarshQMUL 0:5307f49cd305 18
WilliamMarshQMUL 0:5307f49cd305 19 // Put a simple reprsentation of the sine wave
WilliamMarshQMUL 0:5307f49cd305 20 // to the serial output. ONLY at low frequency
WilliamMarshQMUL 0:5307f49cd305 21 void debug(int index) {
WilliamMarshQMUL 0:5307f49cd305 22 int sine4 = sine[index] >> 11 ; // get top 5 bits
WilliamMarshQMUL 0:5307f49cd305 23 pc.putc('*') ;
WilliamMarshQMUL 0:5307f49cd305 24 while (sine4--) pc.putc('*') ;
WilliamMarshQMUL 0:5307f49cd305 25 pc.putc('\n') ;
WilliamMarshQMUL 0:5307f49cd305 26 //pc.putc('\r') ;
WilliamMarshQMUL 0:5307f49cd305 27 }
WilliamMarshQMUL 0:5307f49cd305 28
WilliamMarshQMUL 0:5307f49cd305 29 volatile int index = 0 ; // this variable is not just for debugging!!
WilliamMarshQMUL 0:5307f49cd305 30
WilliamMarshQMUL 0:5307f49cd305 31 void debugCallback() {
WilliamMarshQMUL 0:5307f49cd305 32 while (true) {
WilliamMarshQMUL 0:5307f49cd305 33 Thread::signal_wait(0x1) ;
WilliamMarshQMUL 0:5307f49cd305 34 debug(index) ; // there is a race condition here
WilliamMarshQMUL 0:5307f49cd305 35 }
WilliamMarshQMUL 0:5307f49cd305 36 }
WilliamMarshQMUL 0:5307f49cd305 37 // ---- end of debugging code ---------
WilliamMarshQMUL 0:5307f49cd305 38
WilliamMarshQMUL 0:5307f49cd305 39 // Function called every periodically
WilliamMarshQMUL 0:5307f49cd305 40 // Write new value to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 41 void writeAout() {
WilliamMarshQMUL 0:5307f49cd305 42 ao.write_u16(sine[index]) ;
WilliamMarshQMUL 0:5307f49cd305 43 debugT.signal_set(0x1) ; // DEBUGGING low frequency only
WilliamMarshQMUL 0:5307f49cd305 44 index = (index + 1) % 64 ;
WilliamMarshQMUL 0:5307f49cd305 45 }
WilliamMarshQMUL 0:5307f49cd305 46
WilliamMarshQMUL 0:5307f49cd305 47 // Control the frequency of updates
WilliamMarshQMUL 0:5307f49cd305 48 // Alternative between two frequencies
WilliamMarshQMUL 0:5307f49cd305 49 int main() {
WilliamMarshQMUL 0:5307f49cd305 50 int update_us = 100000 ; // 100ms
WilliamMarshQMUL 0:5307f49cd305 51 debugT.start(&debugCallback) ;
WilliamMarshQMUL 0:5307f49cd305 52 while (true) {
WilliamMarshQMUL 0:5307f49cd305 53 pc.printf("Update at 64 x 100ms giving about 0.15Hz\n");
WilliamMarshQMUL 0:5307f49cd305 54 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 55 Thread::wait(30000) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 0:5307f49cd305 56 update_us = 150000 ; // 150ms
WilliamMarshQMUL 0:5307f49cd305 57 pc.printf("Update at 64 x 150ms giving about 0.1Hz\n");
WilliamMarshQMUL 0:5307f49cd305 58 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
WilliamMarshQMUL 0:5307f49cd305 59 Thread::wait(30000) ; // wait 30 sec - 30000ms
WilliamMarshQMUL 0:5307f49cd305 60 update_us = 100000 ; // 100ms
WilliamMarshQMUL 0:5307f49cd305 61 }
WilliamMarshQMUL 0:5307f49cd305 62 }