Jiajun Zhang
/
DACandticker
LAB 3 DAC
Fork of DACandticker_sample by
Revision 4:dc6e0fdcf6b4, committed 2018-02-15
- Comitter:
- Bossman
- Date:
- Thu Feb 15 17:19:16 2018 +0000
- Parent:
- 3:1df612e3c838
- Commit message:
- Version 2
Changed in this revision
main.cpp | Show diff for this revision Revisions of this file |
name.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 1df612e3c838 -r dc6e0fdcf6b4 main.cpp --- a/main.cpp Fri Feb 09 21:13:03 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -// Lab 3 Example Program 2 -// ----------------------- -// Periodically write to the AnalogOut to create a sine wave -// Alternate between two fixed frequencies every 5 sec -// -// Updated for mbed 5 - -// THIS VERSION HAS NO DEBUGGING CODE - -#include "mbed.h" -#include "sineTable.h" - -Ticker tick ; // Creates periodic interrupt -AnalogOut ao(PTE30) ; // Analog output - - -// Function called periodically -// Write new value to AnalogOut -volatile int index = 0 ; // index into array of sin values -void writeAout() { - ao.write_u16(sine[index]) ; - index = (index + 1) % 64 ; -} - -// Control the frequency of updates -// Alternative between two frequencies -int main() { - int update_us = 1000 ; // 1ms - while (true) { - tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut - Thread::wait(30000) ; // wait 30 sec - 30000ms - update_us = 2000 ; // 2ms - tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut - Thread::wait(30000) ; // wait 30 sec - 30000ms - update_us = 1000 ; // 1ms - } -}
diff -r 1df612e3c838 -r dc6e0fdcf6b4 name.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/name.cpp Thu Feb 15 17:19:16 2018 +0000 @@ -0,0 +1,88 @@ +// Revised for mbed 5 + +#include "mbed.h" +#include "sineTable.h" + +Ticker tickout ; // Creates periodic interrupt +AnalogOut ao(PTE30) ; // Analog output + +Ticker tick; // Ticker for reading analog +AnalogIn ain(A0) ; // Analog input +DigitalOut led1(LED_RED); + + +Serial pc(USBTX, USBRX); // tx, rx, for debugging + +// Message type +typedef struct { + uint16_t analog; /* Analog input value */ +} message_t; + +// Mail box +Mail<message_t, 2> mailbox; + +// Function called every 10ms to read ADC +// Low pass filter +// Every 10th value is sent to mailbox +volatile int samples = 0 ; +volatile uint16_t smoothed = 0 ; +void readA0() { + smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; + samples++ ; + if (samples == 10) { + // send to thread + message_t *mess = mailbox.alloc() ; // may fail but does not block + if (mess) { + mess->analog = smoothed ; + mailbox.put(mess); // fails but does not block if full + } + samples = 0; + } +} + +// Write voltage digits +// v Voltage as scale int, e.g. 3.30 is 330 +void vToString(int v, char* s) { + s[3] = '0' + (v % 10) ; + v = v / 10 ; + s[2] = '0' + (v % 10) ; + v = v / 10 ; + s[0] = '0' + (v % 10) ; +} + +volatile int index = 0 ; // index into array of sin values +void writeAout() { + ao.write_u16(sine[index]) ; + index = (index + 1) % 64 ; +} + +// Main program +// Initialise variables +// Attach ISR for ticker +// Procss messages from mailbox +int main() { + + int update_us = 1000 ; + int volts = 0; + led1 = 1 ; // turn off + // char vstring[] = "X.XX\r\n" ; + + tick.attach_us(callback(&readA0), 10000); // ticks every 10ms + + + while (true) + { + osEvent evt = mailbox.get(); // wait for mail + if (evt.status == osEventMail) + { + message_t* mess = (message_t*)evt.value.p ; + volts = (mess->analog * 330) / 0xffff ; + mailbox.free(mess) ; // free the message space + + update_us = 1000000/((1+(49*volts/330))*64) ; + tickout.detach() ; + tickout.attach_us(callback(&writeAout), update_us); + } + + } +} \ No newline at end of file