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 William Marsh

Committer:
natgovor
Date:
Fri Feb 09 16:35:47 2018 +0000
Revision:
3:ac3412621bf9
Parent:
2:e27fd3b65155
Digital to analog conversion. Reads analog signal (voltage) and depending on it, sets the frequency to produce the analog output for sine wave.

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
natgovor 3:ac3412621bf9 14 Ticker adcTick ; // Creates periodic interrupt
WilliamMarshQMUL 0:5307f49cd305 15 AnalogOut ao(PTE30) ; // Analog output
natgovor 3:ac3412621bf9 16 AnalogIn ain(A0) ; // Analog input
natgovor 3:ac3412621bf9 17
natgovor 3:ac3412621bf9 18 Serial pc(USBTX, USBRX); // tx, rx, for debugging
natgovor 3:ac3412621bf9 19
natgovor 3:ac3412621bf9 20 // Message type
natgovor 3:ac3412621bf9 21 typedef struct {
natgovor 3:ac3412621bf9 22 uint16_t analog; /* Analog input value */
natgovor 3:ac3412621bf9 23 } message_t;
natgovor 3:ac3412621bf9 24
natgovor 3:ac3412621bf9 25 // Mail box
natgovor 3:ac3412621bf9 26 Mail<message_t, 2> mailbox;
natgovor 3:ac3412621bf9 27
natgovor 3:ac3412621bf9 28 // Function called every 10ms to read ADC
natgovor 3:ac3412621bf9 29 // Low pass filter
natgovor 3:ac3412621bf9 30 // Every 10th value is sent to mailbox
natgovor 3:ac3412621bf9 31 volatile int samples = 0 ;
natgovor 3:ac3412621bf9 32 volatile uint16_t smoothed = 0 ;
natgovor 3:ac3412621bf9 33 void readA0() {
natgovor 3:ac3412621bf9 34 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
natgovor 3:ac3412621bf9 35 samples++ ;
natgovor 3:ac3412621bf9 36 if (samples == 10) {
natgovor 3:ac3412621bf9 37 // send to thread
natgovor 3:ac3412621bf9 38 message_t *mess = mailbox.alloc() ; // may fail but does not block
natgovor 3:ac3412621bf9 39 if (mess) {
natgovor 3:ac3412621bf9 40 mess->analog = smoothed ;
natgovor 3:ac3412621bf9 41 mailbox.put(mess); // fails but does not block if full
natgovor 3:ac3412621bf9 42 }
natgovor 3:ac3412621bf9 43 samples = 0;
natgovor 3:ac3412621bf9 44 }
natgovor 3:ac3412621bf9 45 }
natgovor 3:ac3412621bf9 46
natgovor 3:ac3412621bf9 47 // Write voltage digits
natgovor 3:ac3412621bf9 48 // v Voltage as scale int, e.g. 3.30 is 330
natgovor 3:ac3412621bf9 49 void vToString(int v, char* s) {
natgovor 3:ac3412621bf9 50 s[3] = '0' + (v % 10) ;
natgovor 3:ac3412621bf9 51 v = v / 10 ;
natgovor 3:ac3412621bf9 52 s[2] = '0' + (v % 10) ;
natgovor 3:ac3412621bf9 53 v = v / 10 ;
natgovor 3:ac3412621bf9 54 s[0] = '0' + (v % 10) ;
natgovor 3:ac3412621bf9 55 }
WilliamMarshQMUL 0:5307f49cd305 56
WilliamMarshQMUL 1:18e0f8aef32f 57 // Function called periodically
WilliamMarshQMUL 1:18e0f8aef32f 58 // Write new value to AnalogOut
WilliamMarshQMUL 2:e27fd3b65155 59 volatile int index = 0 ; // index into array of sin values
WilliamMarshQMUL 0:5307f49cd305 60 void writeAout() {
WilliamMarshQMUL 0:5307f49cd305 61 ao.write_u16(sine[index]) ;
WilliamMarshQMUL 0:5307f49cd305 62 index = (index + 1) % 64 ;
WilliamMarshQMUL 0:5307f49cd305 63 }
WilliamMarshQMUL 0:5307f49cd305 64
WilliamMarshQMUL 0:5307f49cd305 65 // Control the frequency of updates
WilliamMarshQMUL 0:5307f49cd305 66 // Alternative between two frequencies
WilliamMarshQMUL 0:5307f49cd305 67 int main() {
natgovor 3:ac3412621bf9 68 int volts = 0 ;
natgovor 3:ac3412621bf9 69 int counter = 0 ;
natgovor 3:ac3412621bf9 70 char vstring[] = "X.XX\r\n" ;
WilliamMarshQMUL 1:18e0f8aef32f 71 int update_us = 1000 ; // 1ms
natgovor 3:ac3412621bf9 72 int maximum_voltage = 330;
natgovor 3:ac3412621bf9 73
natgovor 3:ac3412621bf9 74 adcTick.attach_us(callback(&readA0), 10000); // ticks every 10ms
natgovor 3:ac3412621bf9 75 int f = 0;
natgovor 3:ac3412621bf9 76 int old_update_us = update_us;
natgovor 3:ac3412621bf9 77 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
natgovor 3:ac3412621bf9 78
WilliamMarshQMUL 0:5307f49cd305 79 while (true) {
natgovor 3:ac3412621bf9 80 osEvent evt = mailbox.get(); // wait for mail
natgovor 3:ac3412621bf9 81 if (evt.status == osEventMail) {
natgovor 3:ac3412621bf9 82 message_t* mess = (message_t*)evt.value.p ;
natgovor 3:ac3412621bf9 83 volts = (mess->analog * 330) / 0xffff ;
natgovor 3:ac3412621bf9 84 mailbox.free(mess) ; // free the message space
natgovor 3:ac3412621bf9 85
natgovor 3:ac3412621bf9 86 // change the frequency
natgovor 3:ac3412621bf9 87 f = 1 + (49 * volts / maximum_voltage);
natgovor 3:ac3412621bf9 88 update_us = 1000000 / (f * 64);
natgovor 3:ac3412621bf9 89
natgovor 3:ac3412621bf9 90 if (update_us != old_update_us) {
natgovor 3:ac3412621bf9 91 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
natgovor 3:ac3412621bf9 92 old_update_us = update_us;
natgovor 3:ac3412621bf9 93 }
natgovor 3:ac3412621bf9 94
natgovor 3:ac3412621bf9 95 vToString(volts, vstring) ;
natgovor 3:ac3412621bf9 96 counter++ ;
natgovor 3:ac3412621bf9 97 if (counter == 10) { // limit bandwidth of serial
natgovor 3:ac3412621bf9 98 pc.printf(vstring) ;
natgovor 3:ac3412621bf9 99 counter = 0 ;
natgovor 3:ac3412621bf9 100 }
natgovor 3:ac3412621bf9 101 }
natgovor 3:ac3412621bf9 102
natgovor 3:ac3412621bf9 103 Thread::wait(200) ;
WilliamMarshQMUL 0:5307f49cd305 104 }
WilliamMarshQMUL 0:5307f49cd305 105 }