Haozun Sun
/
ADC_HaozunSun_180773877
ADC
main.cpp@4:ebd00f94455a, 2019-01-31 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Thu Jan 31 13:53:15 2019 +0000
- Revision:
- 4:ebd00f94455a
- Parent:
- 3:f5d7fddeef39
- Child:
- 6:ef4c3d706a30
Replace Ticker with event queue and thread
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:ad3cdc525b99 | 1 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 2 | // LAB 3 SAMPLE PROGRAM 1 |
WilliamMarshQMUL | 3:f5d7fddeef39 | 3 | // Revised for mbed 5 |
WilliamMarshQMUL | 4:ebd00f94455a | 4 | // Revised to replace Ticker with event queue and thread |
WilliamMarshQMUL | 0:ad3cdc525b99 | 5 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 6 | #include "mbed.h" |
WilliamMarshQMUL | 1:126dd2f5fc2d | 7 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 8 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 9 | AnalogIn ain(A0) ; // Analog input |
WilliamMarshQMUL | 0:ad3cdc525b99 | 10 | DigitalOut led1(LED_RED); // Red LED |
WilliamMarshQMUL | 4:ebd00f94455a | 11 | EventQueue queue; // creates an event queue, to call read ADC |
WilliamMarshQMUL | 0:ad3cdc525b99 | 12 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 13 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
WilliamMarshQMUL | 0:ad3cdc525b99 | 14 | |
WilliamMarshQMUL | 4:ebd00f94455a | 15 | // This thread runs the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 16 | Thread eventThread ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 17 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 18 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 19 | typedef struct { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 20 | uint16_t analog; /* Analog input value */ |
WilliamMarshQMUL | 0:ad3cdc525b99 | 21 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 22 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 23 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 24 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 25 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 26 | // Function called every 10ms to read ADC |
WilliamMarshQMUL | 4:ebd00f94455a | 27 | // Average using a low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 28 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 4:ebd00f94455a | 29 | volatile int samples = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 30 | volatile uint16_t smoothed = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 31 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 32 | void readA0() { |
WilliamMarshQMUL | 4:ebd00f94455a | 33 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 4:ebd00f94455a | 34 | samples++ ; |
WilliamMarshQMUL | 4:ebd00f94455a | 35 | if (samples == 10) { |
WilliamMarshQMUL | 4:ebd00f94455a | 36 | // send to thread |
WilliamMarshQMUL | 4:ebd00f94455a | 37 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 4:ebd00f94455a | 38 | if (mess) { |
WilliamMarshQMUL | 4:ebd00f94455a | 39 | mess->analog = smoothed ; |
WilliamMarshQMUL | 4:ebd00f94455a | 40 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 0:ad3cdc525b99 | 41 | } |
WilliamMarshQMUL | 4:ebd00f94455a | 42 | samples = 0; |
WilliamMarshQMUL | 4:ebd00f94455a | 43 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 44 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 45 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 46 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 47 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 48 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 49 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 50 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 51 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 52 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 53 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 54 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 55 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 56 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 57 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 58 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 59 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 60 | int main() { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 61 | led1 = 1 ; // turn off |
WilliamMarshQMUL | 0:ad3cdc525b99 | 62 | int volts = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | const int threshold = 100 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 65 | char vstring[] = "X.XX\r\n" ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 66 | |
WilliamMarshQMUL | 4:ebd00f94455a | 67 | // Start the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 68 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
WilliamMarshQMUL | 4:ebd00f94455a | 69 | |
WilliamMarshQMUL | 4:ebd00f94455a | 70 | // call the readA0 function every 10ms |
WilliamMarshQMUL | 4:ebd00f94455a | 71 | queue.call_every(10, readA0) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 72 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 73 | while (true) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 74 | osEvent evt = mailbox.get(); // wait for mail |
WilliamMarshQMUL | 0:ad3cdc525b99 | 75 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 76 | message_t* mess = (message_t*)evt.value.p ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 77 | volts = (mess->analog * 330) / 0xffff ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 78 | mailbox.free(mess) ; // free the message space |
WilliamMarshQMUL | 0:ad3cdc525b99 | 79 | if (volts > threshold) led1 = 0 ; else led1 = 1 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 80 | vToString(volts, vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 81 | counter++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 82 | if (counter == 10) { // limit bandwidth of serial |
WilliamMarshQMUL | 0:ad3cdc525b99 | 83 | pc.printf(vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 84 | counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 85 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 86 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 87 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 88 | } |