Andrew Mkpanam
/
ADCandticker
final
main.cpp@3:f5d7fddeef39, 2019-01-31 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Thu Jan 31 11:10:30 2019 +0000
- Revision:
- 3:f5d7fddeef39
- Parent:
- 1:126dd2f5fc2d
- Child:
- 4:ebd00f94455a
Version with thread instead of ticker
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 | 0:ad3cdc525b99 | 4 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 5 | #include "mbed.h" |
WilliamMarshQMUL | 1:126dd2f5fc2d | 6 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 7 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 8 | Ticker tick; // Ticker for reading analog |
WilliamMarshQMUL | 0:ad3cdc525b99 | 9 | AnalogIn ain(A0) ; // Analog input |
WilliamMarshQMUL | 0:ad3cdc525b99 | 10 | DigitalOut led1(LED_RED); // Red LED |
WilliamMarshQMUL | 3:f5d7fddeef39 | 11 | DigitalOut led2(LED_BLUE); // BLUE LED |
WilliamMarshQMUL | 0:ad3cdc525b99 | 12 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 13 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
WilliamMarshQMUL | 0:ad3cdc525b99 | 14 | |
WilliamMarshQMUL | 3:f5d7fddeef39 | 15 | // Variable for a second thread |
WilliamMarshQMUL | 3:f5d7fddeef39 | 16 | Thread ADCthread; |
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 | 0:ad3cdc525b99 | 27 | // Low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 28 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 29 | void readA0() { |
WilliamMarshQMUL | 3:f5d7fddeef39 | 30 | int samples = 0 ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 31 | uint16_t smoothed = 0 ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 32 | while (true) { |
WilliamMarshQMUL | 3:f5d7fddeef39 | 33 | wait(0.01) ; // every 10 ms |
WilliamMarshQMUL | 3:f5d7fddeef39 | 34 | led2 = 1 ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 35 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 36 | samples++ ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 37 | if (samples == 10) { |
WilliamMarshQMUL | 3:f5d7fddeef39 | 38 | // send to thread |
WilliamMarshQMUL | 3:f5d7fddeef39 | 39 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 3:f5d7fddeef39 | 40 | if (mess) { |
WilliamMarshQMUL | 3:f5d7fddeef39 | 41 | mess->analog = smoothed ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 42 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 3:f5d7fddeef39 | 43 | } |
WilliamMarshQMUL | 3:f5d7fddeef39 | 44 | samples = 0; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 45 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 46 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 47 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 48 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 49 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 50 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 51 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 52 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 53 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 54 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 55 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 56 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 57 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 58 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 59 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 60 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 61 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 62 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | int main() { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | led1 = 1 ; // turn off |
WilliamMarshQMUL | 0:ad3cdc525b99 | 65 | int volts = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 66 | const int threshold = 100 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 67 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 68 | char vstring[] = "X.XX\r\n" ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 69 | |
WilliamMarshQMUL | 3:f5d7fddeef39 | 70 | // start the ADC reading thread |
WilliamMarshQMUL | 3:f5d7fddeef39 | 71 | ADCthread.start(callback(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 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 89 | } |