noop din
/
ADCandticker_sample
the code for Lab3
Fork of ADCandticker_sample by
main.cpp@4:8edcd6d3c39f, 2018-02-09 (annotated)
- Committer:
- toh2018
- Date:
- Fri Feb 09 21:07:44 2018 +0000
- Revision:
- 4:8edcd6d3c39f
- Parent:
- 3:2db0cd79282b
- Child:
- 5:3bd67c8bb0ee
add threshold
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 | 1:126dd2f5fc2d | 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 |
toh2018 | 2:f7c24790b1ad | 10 | DigitalOut led1(D8); // Red LED |
toh2018 | 2:f7c24790b1ad | 11 | DigitalOut led2(D9); |
toh2018 | 2:f7c24790b1ad | 12 | DigitalOut led3(D10); |
toh2018 | 2:f7c24790b1ad | 13 | DigitalOut led4(D11); |
toh2018 | 2:f7c24790b1ad | 14 | DigitalOut led5(D12); |
WilliamMarshQMUL | 0:ad3cdc525b99 | 15 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 16 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
toh2018 | 2:f7c24790b1ad | 17 | DigitalIn b1(PTE1,PullUp); |
WilliamMarshQMUL | 0:ad3cdc525b99 | 18 | |
toh2018 | 2:f7c24790b1ad | 19 | Thread pollT ; // thread to poll |
toh2018 | 2:f7c24790b1ad | 20 | volatile int pressEvent = 0 ; // Variabe set by the polling thread |
toh2018 | 2:f7c24790b1ad | 21 | volatile int maxvolt = 330; |
toh2018 | 2:f7c24790b1ad | 22 | |
toh2018 | 3:2db0cd79282b | 23 | /// implement the botton here ///////////////////////////////////// |
toh2018 | 2:f7c24790b1ad | 24 | |
toh2018 | 2:f7c24790b1ad | 25 | |
toh2018 | 2:f7c24790b1ad | 26 | enum buttonPos { up, down, bounce }; // Button positions |
toh2018 | 2:f7c24790b1ad | 27 | void polling() { |
toh2018 | 2:f7c24790b1ad | 28 | buttonPos pos = up ; |
toh2018 | 2:f7c24790b1ad | 29 | int bcounter = 0 ; |
toh2018 | 2:f7c24790b1ad | 30 | while (true) { |
toh2018 | 2:f7c24790b1ad | 31 | switch (pos) { |
toh2018 | 2:f7c24790b1ad | 32 | case up : |
toh2018 | 2:f7c24790b1ad | 33 | if (!b1.read()) { // now down |
toh2018 | 2:f7c24790b1ad | 34 | pressEvent = 1 ; // transition occurred |
toh2018 | 2:f7c24790b1ad | 35 | pos = down ; |
toh2018 | 2:f7c24790b1ad | 36 | } |
toh2018 | 2:f7c24790b1ad | 37 | break ; |
toh2018 | 2:f7c24790b1ad | 38 | case down : |
toh2018 | 2:f7c24790b1ad | 39 | if (b1 == 1) { // no longer down |
toh2018 | 2:f7c24790b1ad | 40 | bcounter = 3 ; // wait four cycles |
toh2018 | 2:f7c24790b1ad | 41 | pos = bounce ; |
toh2018 | 2:f7c24790b1ad | 42 | } |
toh2018 | 2:f7c24790b1ad | 43 | break ; |
toh2018 | 2:f7c24790b1ad | 44 | case bounce : |
toh2018 | 2:f7c24790b1ad | 45 | if (b1 == 0) { // down again - button has bounced |
toh2018 | 2:f7c24790b1ad | 46 | pos = down ; // no event |
toh2018 | 2:f7c24790b1ad | 47 | } else if (bcounter == 0) { |
toh2018 | 2:f7c24790b1ad | 48 | pos = up ; // delay passed - reset to up |
toh2018 | 2:f7c24790b1ad | 49 | } else { |
toh2018 | 2:f7c24790b1ad | 50 | bcounter-- ; // continue waiting |
toh2018 | 2:f7c24790b1ad | 51 | } |
toh2018 | 2:f7c24790b1ad | 52 | break ; |
toh2018 | 2:f7c24790b1ad | 53 | } |
toh2018 | 2:f7c24790b1ad | 54 | Thread::wait(30); |
toh2018 | 2:f7c24790b1ad | 55 | } |
toh2018 | 2:f7c24790b1ad | 56 | } |
toh2018 | 3:2db0cd79282b | 57 | // end the button ////////////////////////////////////////////////// |
toh2018 | 2:f7c24790b1ad | 58 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 59 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 60 | typedef struct { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 61 | uint16_t analog; /* Analog input value */ |
WilliamMarshQMUL | 0:ad3cdc525b99 | 62 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 65 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 66 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 67 | // Function called every 10ms to read ADC |
WilliamMarshQMUL | 0:ad3cdc525b99 | 68 | // Low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 69 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 70 | volatile int samples = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 71 | volatile uint16_t smoothed = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 72 | void readA0() { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 73 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 74 | samples++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 75 | if (samples == 10) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 76 | // send to thread |
WilliamMarshQMUL | 0:ad3cdc525b99 | 77 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 0:ad3cdc525b99 | 78 | if (mess) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 79 | mess->analog = smoothed ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 80 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 0:ad3cdc525b99 | 81 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 82 | samples = 0; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 83 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 84 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 85 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 86 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 87 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 88 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 89 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 90 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 91 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 92 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 93 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 94 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 95 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 96 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 97 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 98 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 99 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 100 | int main() { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 101 | led1 = 1 ; // turn off |
toh2018 | 2:f7c24790b1ad | 102 | led2 = 1; |
toh2018 | 2:f7c24790b1ad | 103 | led3 = 1; |
toh2018 | 2:f7c24790b1ad | 104 | led4 = 1; |
toh2018 | 2:f7c24790b1ad | 105 | led5 = 1; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 106 | int volts = 0 ; |
toh2018 | 3:2db0cd79282b | 107 | //const int threshold = 100 ; |
toh2018 | 2:f7c24790b1ad | 108 | const int threshold1 = 55 ; |
toh2018 | 2:f7c24790b1ad | 109 | const int threshold2 = 110 ; |
toh2018 | 2:f7c24790b1ad | 110 | const int threshold3 = 165 ; |
toh2018 | 2:f7c24790b1ad | 111 | const int threshold4 = 220 ; |
toh2018 | 2:f7c24790b1ad | 112 | const int threshold5 = 275 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 113 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 114 | char vstring[] = "X.XX\r\n" ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 115 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 116 | tick.attach_us(callback(&readA0), 10000); // ticks every 10ms |
WilliamMarshQMUL | 0:ad3cdc525b99 | 117 | while (true) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 118 | osEvent evt = mailbox.get(); // wait for mail |
WilliamMarshQMUL | 0:ad3cdc525b99 | 119 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 120 | message_t* mess = (message_t*)evt.value.p ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 121 | volts = (mess->analog * 330) / 0xffff ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 122 | mailbox.free(mess) ; // free the message space |
toh2018 | 2:f7c24790b1ad | 123 | |
toh2018 | 3:2db0cd79282b | 124 | ////press event //////////////////////////////////// |
toh2018 | 2:f7c24790b1ad | 125 | |
toh2018 | 2:f7c24790b1ad | 126 | if(pressEvent) |
toh2018 | 2:f7c24790b1ad | 127 | { |
toh2018 | 2:f7c24790b1ad | 128 | pressEvent =0; |
toh2018 | 2:f7c24790b1ad | 129 | maxvolt = volts; |
toh2018 | 4:8edcd6d3c39f | 130 | /// update the threshold here |
toh2018 | 2:f7c24790b1ad | 131 | } |
toh2018 | 2:f7c24790b1ad | 132 | |
toh2018 | 3:2db0cd79282b | 133 | // End of the pressEvent //////////////////////////// |
toh2018 | 2:f7c24790b1ad | 134 | |
toh2018 | 2:f7c24790b1ad | 135 | if (volts > threshold1) led1 = 1 ; else led1 = 0 ; |
toh2018 | 2:f7c24790b1ad | 136 | if (volts > threshold2) led2 = 1 ; else led2 = 0 ; |
toh2018 | 2:f7c24790b1ad | 137 | if (volts > threshold3) led3 = 1 ; else led3 = 0 ; |
toh2018 | 2:f7c24790b1ad | 138 | if (volts > threshold4) led4 = 1 ; else led4 = 0 ; |
toh2018 | 2:f7c24790b1ad | 139 | if (volts > threshold5) led5 = 1 ; else led5 = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 140 | vToString(volts, vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 141 | counter++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 142 | if (counter == 10) { // limit bandwidth of serial |
WilliamMarshQMUL | 0:ad3cdc525b99 | 143 | pc.printf(vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 144 | counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 145 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 146 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 147 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 148 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 149 | } |