![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LAB 3 DAC
Fork of DACandticker_sample by
name.cpp@4:dc6e0fdcf6b4, 2018-02-15 (annotated)
- Committer:
- Bossman
- Date:
- Thu Feb 15 17:19:16 2018 +0000
- Revision:
- 4:dc6e0fdcf6b4
Version 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bossman | 4:dc6e0fdcf6b4 | 1 | // Revised for mbed 5 |
Bossman | 4:dc6e0fdcf6b4 | 2 | |
Bossman | 4:dc6e0fdcf6b4 | 3 | #include "mbed.h" |
Bossman | 4:dc6e0fdcf6b4 | 4 | #include "sineTable.h" |
Bossman | 4:dc6e0fdcf6b4 | 5 | |
Bossman | 4:dc6e0fdcf6b4 | 6 | Ticker tickout ; // Creates periodic interrupt |
Bossman | 4:dc6e0fdcf6b4 | 7 | AnalogOut ao(PTE30) ; // Analog output |
Bossman | 4:dc6e0fdcf6b4 | 8 | |
Bossman | 4:dc6e0fdcf6b4 | 9 | Ticker tick; // Ticker for reading analog |
Bossman | 4:dc6e0fdcf6b4 | 10 | AnalogIn ain(A0) ; // Analog input |
Bossman | 4:dc6e0fdcf6b4 | 11 | DigitalOut led1(LED_RED); |
Bossman | 4:dc6e0fdcf6b4 | 12 | |
Bossman | 4:dc6e0fdcf6b4 | 13 | |
Bossman | 4:dc6e0fdcf6b4 | 14 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
Bossman | 4:dc6e0fdcf6b4 | 15 | |
Bossman | 4:dc6e0fdcf6b4 | 16 | // Message type |
Bossman | 4:dc6e0fdcf6b4 | 17 | typedef struct { |
Bossman | 4:dc6e0fdcf6b4 | 18 | uint16_t analog; /* Analog input value */ |
Bossman | 4:dc6e0fdcf6b4 | 19 | } message_t; |
Bossman | 4:dc6e0fdcf6b4 | 20 | |
Bossman | 4:dc6e0fdcf6b4 | 21 | // Mail box |
Bossman | 4:dc6e0fdcf6b4 | 22 | Mail<message_t, 2> mailbox; |
Bossman | 4:dc6e0fdcf6b4 | 23 | |
Bossman | 4:dc6e0fdcf6b4 | 24 | // Function called every 10ms to read ADC |
Bossman | 4:dc6e0fdcf6b4 | 25 | // Low pass filter |
Bossman | 4:dc6e0fdcf6b4 | 26 | // Every 10th value is sent to mailbox |
Bossman | 4:dc6e0fdcf6b4 | 27 | volatile int samples = 0 ; |
Bossman | 4:dc6e0fdcf6b4 | 28 | volatile uint16_t smoothed = 0 ; |
Bossman | 4:dc6e0fdcf6b4 | 29 | void readA0() { |
Bossman | 4:dc6e0fdcf6b4 | 30 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
Bossman | 4:dc6e0fdcf6b4 | 31 | samples++ ; |
Bossman | 4:dc6e0fdcf6b4 | 32 | if (samples == 10) { |
Bossman | 4:dc6e0fdcf6b4 | 33 | // send to thread |
Bossman | 4:dc6e0fdcf6b4 | 34 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
Bossman | 4:dc6e0fdcf6b4 | 35 | if (mess) { |
Bossman | 4:dc6e0fdcf6b4 | 36 | mess->analog = smoothed ; |
Bossman | 4:dc6e0fdcf6b4 | 37 | mailbox.put(mess); // fails but does not block if full |
Bossman | 4:dc6e0fdcf6b4 | 38 | } |
Bossman | 4:dc6e0fdcf6b4 | 39 | samples = 0; |
Bossman | 4:dc6e0fdcf6b4 | 40 | } |
Bossman | 4:dc6e0fdcf6b4 | 41 | } |
Bossman | 4:dc6e0fdcf6b4 | 42 | |
Bossman | 4:dc6e0fdcf6b4 | 43 | // Write voltage digits |
Bossman | 4:dc6e0fdcf6b4 | 44 | // v Voltage as scale int, e.g. 3.30 is 330 |
Bossman | 4:dc6e0fdcf6b4 | 45 | void vToString(int v, char* s) { |
Bossman | 4:dc6e0fdcf6b4 | 46 | s[3] = '0' + (v % 10) ; |
Bossman | 4:dc6e0fdcf6b4 | 47 | v = v / 10 ; |
Bossman | 4:dc6e0fdcf6b4 | 48 | s[2] = '0' + (v % 10) ; |
Bossman | 4:dc6e0fdcf6b4 | 49 | v = v / 10 ; |
Bossman | 4:dc6e0fdcf6b4 | 50 | s[0] = '0' + (v % 10) ; |
Bossman | 4:dc6e0fdcf6b4 | 51 | } |
Bossman | 4:dc6e0fdcf6b4 | 52 | |
Bossman | 4:dc6e0fdcf6b4 | 53 | volatile int index = 0 ; // index into array of sin values |
Bossman | 4:dc6e0fdcf6b4 | 54 | void writeAout() { |
Bossman | 4:dc6e0fdcf6b4 | 55 | ao.write_u16(sine[index]) ; |
Bossman | 4:dc6e0fdcf6b4 | 56 | index = (index + 1) % 64 ; |
Bossman | 4:dc6e0fdcf6b4 | 57 | } |
Bossman | 4:dc6e0fdcf6b4 | 58 | |
Bossman | 4:dc6e0fdcf6b4 | 59 | // Main program |
Bossman | 4:dc6e0fdcf6b4 | 60 | // Initialise variables |
Bossman | 4:dc6e0fdcf6b4 | 61 | // Attach ISR for ticker |
Bossman | 4:dc6e0fdcf6b4 | 62 | // Procss messages from mailbox |
Bossman | 4:dc6e0fdcf6b4 | 63 | int main() { |
Bossman | 4:dc6e0fdcf6b4 | 64 | |
Bossman | 4:dc6e0fdcf6b4 | 65 | int update_us = 1000 ; |
Bossman | 4:dc6e0fdcf6b4 | 66 | int volts = 0; |
Bossman | 4:dc6e0fdcf6b4 | 67 | led1 = 1 ; // turn off |
Bossman | 4:dc6e0fdcf6b4 | 68 | // char vstring[] = "X.XX\r\n" ; |
Bossman | 4:dc6e0fdcf6b4 | 69 | |
Bossman | 4:dc6e0fdcf6b4 | 70 | tick.attach_us(callback(&readA0), 10000); // ticks every 10ms |
Bossman | 4:dc6e0fdcf6b4 | 71 | |
Bossman | 4:dc6e0fdcf6b4 | 72 | |
Bossman | 4:dc6e0fdcf6b4 | 73 | while (true) |
Bossman | 4:dc6e0fdcf6b4 | 74 | { |
Bossman | 4:dc6e0fdcf6b4 | 75 | osEvent evt = mailbox.get(); // wait for mail |
Bossman | 4:dc6e0fdcf6b4 | 76 | if (evt.status == osEventMail) |
Bossman | 4:dc6e0fdcf6b4 | 77 | { |
Bossman | 4:dc6e0fdcf6b4 | 78 | message_t* mess = (message_t*)evt.value.p ; |
Bossman | 4:dc6e0fdcf6b4 | 79 | volts = (mess->analog * 330) / 0xffff ; |
Bossman | 4:dc6e0fdcf6b4 | 80 | mailbox.free(mess) ; // free the message space |
Bossman | 4:dc6e0fdcf6b4 | 81 | |
Bossman | 4:dc6e0fdcf6b4 | 82 | update_us = 1000000/((1+(49*volts/330))*64) ; |
Bossman | 4:dc6e0fdcf6b4 | 83 | tickout.detach() ; |
Bossman | 4:dc6e0fdcf6b4 | 84 | tickout.attach_us(callback(&writeAout), update_us); |
Bossman | 4:dc6e0fdcf6b4 | 85 | } |
Bossman | 4:dc6e0fdcf6b4 | 86 | |
Bossman | 4:dc6e0fdcf6b4 | 87 | } |
Bossman | 4:dc6e0fdcf6b4 | 88 | } |