Soheil Novinfard
/
ADCandticker_modified_2
AnalougeToDigital Converter
Fork of ADCandticker_sample by
main.cpp@2:24a06c840c25, 2018-02-08 (annotated)
- Committer:
- novinfard
- Date:
- Thu Feb 08 16:16:01 2018 +0000
- Revision:
- 2:24a06c840c25
- Parent:
- 1:126dd2f5fc2d
Project Lab3 (Part 1) finished
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 |
novinfard | 2:24a06c840c25 | 10 | DigitalOut led1(PTA12); |
novinfard | 2:24a06c840c25 | 11 | DigitalOut led2(PTA4); |
novinfard | 2:24a06c840c25 | 12 | DigitalOut led3(PTA5); |
novinfard | 2:24a06c840c25 | 13 | DigitalOut led4(PTC8); |
novinfard | 2:24a06c840c25 | 14 | DigitalOut led5(PTC9); |
novinfard | 2:24a06c840c25 | 15 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
novinfard | 2:24a06c840c25 | 16 | |
novinfard | 2:24a06c840c25 | 17 | DigitalIn b1(PTD0, PullUp); |
novinfard | 2:24a06c840c25 | 18 | |
novinfard | 2:24a06c840c25 | 19 | volatile int pressEvent = 0 ; // Variabe set by the polling thread |
novinfard | 2:24a06c840c25 | 20 | |
novinfard | 2:24a06c840c25 | 21 | enum buttonPos { up, down, bounce }; // Button positions |
novinfard | 2:24a06c840c25 | 22 | void polling() |
novinfard | 2:24a06c840c25 | 23 | { |
novinfard | 2:24a06c840c25 | 24 | buttonPos pos = up ; |
novinfard | 2:24a06c840c25 | 25 | int bcounter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 26 | |
novinfard | 2:24a06c840c25 | 27 | switch (pos) { |
novinfard | 2:24a06c840c25 | 28 | case up : |
novinfard | 2:24a06c840c25 | 29 | if (!b1.read()) { // now down |
novinfard | 2:24a06c840c25 | 30 | pressEvent = 1 ; // transition occurred |
novinfard | 2:24a06c840c25 | 31 | pos = down ; |
novinfard | 2:24a06c840c25 | 32 | } |
novinfard | 2:24a06c840c25 | 33 | break ; |
novinfard | 2:24a06c840c25 | 34 | case down : |
novinfard | 2:24a06c840c25 | 35 | if (b1 == 1) { // no longer down |
novinfard | 2:24a06c840c25 | 36 | bcounter = 3 ; // wait four cycles |
novinfard | 2:24a06c840c25 | 37 | pos = bounce ; |
novinfard | 2:24a06c840c25 | 38 | } |
novinfard | 2:24a06c840c25 | 39 | break ; |
novinfard | 2:24a06c840c25 | 40 | case bounce : |
novinfard | 2:24a06c840c25 | 41 | if (b1 == 0) { // down again - button has bounced |
novinfard | 2:24a06c840c25 | 42 | pos = down ; // no event |
novinfard | 2:24a06c840c25 | 43 | } else if (bcounter == 0) { |
novinfard | 2:24a06c840c25 | 44 | pos = up ; // delay passed - reset to up |
novinfard | 2:24a06c840c25 | 45 | } else { |
novinfard | 2:24a06c840c25 | 46 | bcounter-- ; // continue waiting |
novinfard | 2:24a06c840c25 | 47 | } |
novinfard | 2:24a06c840c25 | 48 | break ; |
novinfard | 2:24a06c840c25 | 49 | |
novinfard | 2:24a06c840c25 | 50 | } |
novinfard | 2:24a06c840c25 | 51 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 52 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 53 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 54 | typedef struct { |
novinfard | 2:24a06c840c25 | 55 | uint16_t analog; /* Analog input value */ |
WilliamMarshQMUL | 0:ad3cdc525b99 | 56 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 57 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 58 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 59 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 60 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 61 | // Function called every 10ms to read ADC |
novinfard | 2:24a06c840c25 | 62 | // Low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | volatile int samples = 0 ; |
novinfard | 2:24a06c840c25 | 65 | volatile uint16_t smoothed = 0 ; |
novinfard | 2:24a06c840c25 | 66 | void readA0() |
novinfard | 2:24a06c840c25 | 67 | { |
novinfard | 2:24a06c840c25 | 68 | polling(); |
novinfard | 2:24a06c840c25 | 69 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; // divided by 2 - reduce the signal noise |
WilliamMarshQMUL | 0:ad3cdc525b99 | 70 | samples++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 71 | if (samples == 10) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 72 | // send to thread |
WilliamMarshQMUL | 0:ad3cdc525b99 | 73 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 0:ad3cdc525b99 | 74 | if (mess) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 75 | mess->analog = smoothed ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 76 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 0:ad3cdc525b99 | 77 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 78 | samples = 0; |
novinfard | 2:24a06c840c25 | 79 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 80 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 81 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 82 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 83 | // v Voltage as scale int, e.g. 3.30 is 330 |
novinfard | 2:24a06c840c25 | 84 | void vToString(int v, char* s) |
novinfard | 2:24a06c840c25 | 85 | { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 86 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 87 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 88 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 89 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 90 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 91 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 92 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 93 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 94 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 95 | // Attach ISR for ticker |
novinfard | 2:24a06c840c25 | 96 | // Procss messages from mailbox |
novinfard | 2:24a06c840c25 | 97 | int main() |
novinfard | 2:24a06c840c25 | 98 | { |
novinfard | 2:24a06c840c25 | 99 | led1 = 1 ; // turn off |
WilliamMarshQMUL | 0:ad3cdc525b99 | 100 | int volts = 0 ; |
novinfard | 2:24a06c840c25 | 101 | int threshold = 100 ; // 1 vol |
WilliamMarshQMUL | 0:ad3cdc525b99 | 102 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 103 | char vstring[] = "X.XX\r\n" ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 104 | |
novinfard | 2:24a06c840c25 | 105 | tick.attach_us(callback(&readA0), 10000); // ticks every 10ms -> 10000 micro second |
novinfard | 2:24a06c840c25 | 106 | |
novinfard | 2:24a06c840c25 | 107 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 108 | while (true) { |
novinfard | 2:24a06c840c25 | 109 | osEvent evt = mailbox.get(); // wait for mail |
novinfard | 2:24a06c840c25 | 110 | |
novinfard | 2:24a06c840c25 | 111 | |
novinfard | 2:24a06c840c25 | 112 | |
novinfard | 2:24a06c840c25 | 113 | // every 100 ms this loop operates |
WilliamMarshQMUL | 0:ad3cdc525b99 | 114 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 115 | message_t* mess = (message_t*)evt.value.p ; |
novinfard | 2:24a06c840c25 | 116 | volts = (mess->analog * 330) / 0xffff ; // 2 ^ 16 |
novinfard | 2:24a06c840c25 | 117 | |
novinfard | 2:24a06c840c25 | 118 | if (pressEvent) { |
novinfard | 2:24a06c840c25 | 119 | pressEvent = 0 ; // clear the event variable |
novinfard | 2:24a06c840c25 | 120 | threshold = volts; |
novinfard | 2:24a06c840c25 | 121 | |
novinfard | 2:24a06c840c25 | 122 | } |
novinfard | 2:24a06c840c25 | 123 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 124 | mailbox.free(mess) ; // free the message space |
novinfard | 2:24a06c840c25 | 125 | if(volts < (threshold / 6)) { |
novinfard | 2:24a06c840c25 | 126 | led1 = 0; |
novinfard | 2:24a06c840c25 | 127 | led2 = 0; |
novinfard | 2:24a06c840c25 | 128 | led3 = 0; |
novinfard | 2:24a06c840c25 | 129 | led4 = 0; |
novinfard | 2:24a06c840c25 | 130 | led5 = 0; |
novinfard | 2:24a06c840c25 | 131 | } |
novinfard | 2:24a06c840c25 | 132 | if((volts > (threshold * 1/ 6))&&(volts < (threshold * 2/ 6))) { |
novinfard | 2:24a06c840c25 | 133 | led1 = 1; |
novinfard | 2:24a06c840c25 | 134 | led2 = 0; |
novinfard | 2:24a06c840c25 | 135 | led3 = 0; |
novinfard | 2:24a06c840c25 | 136 | led4 = 0; |
novinfard | 2:24a06c840c25 | 137 | led5 = 0; |
novinfard | 2:24a06c840c25 | 138 | } |
novinfard | 2:24a06c840c25 | 139 | if((volts > (threshold * 2/ 6))&&(volts < (threshold * 3/ 6))) { |
novinfard | 2:24a06c840c25 | 140 | led1 = 1; |
novinfard | 2:24a06c840c25 | 141 | led2 = 1; |
novinfard | 2:24a06c840c25 | 142 | led3 = 0; |
novinfard | 2:24a06c840c25 | 143 | led4 = 0; |
novinfard | 2:24a06c840c25 | 144 | led5 = 0; |
novinfard | 2:24a06c840c25 | 145 | } |
novinfard | 2:24a06c840c25 | 146 | if((volts > (threshold * 3/ 6))&&(volts < (threshold * 4/ 6))) { |
novinfard | 2:24a06c840c25 | 147 | led1 = 1; |
novinfard | 2:24a06c840c25 | 148 | led2 = 1; |
novinfard | 2:24a06c840c25 | 149 | led3 = 1; |
novinfard | 2:24a06c840c25 | 150 | led4 = 0; |
novinfard | 2:24a06c840c25 | 151 | led5 = 0; |
novinfard | 2:24a06c840c25 | 152 | } |
novinfard | 2:24a06c840c25 | 153 | if((volts > (threshold * 4/ 6))&&(volts < (threshold * 5/ 6))) { |
novinfard | 2:24a06c840c25 | 154 | led1 = 1; |
novinfard | 2:24a06c840c25 | 155 | led2 = 1; |
novinfard | 2:24a06c840c25 | 156 | led3 = 1; |
novinfard | 2:24a06c840c25 | 157 | led4 = 1; |
novinfard | 2:24a06c840c25 | 158 | led5 = 0; |
novinfard | 2:24a06c840c25 | 159 | } |
novinfard | 2:24a06c840c25 | 160 | if(volts > (threshold * 5/ 6)) { |
novinfard | 2:24a06c840c25 | 161 | led1 = 1; |
novinfard | 2:24a06c840c25 | 162 | led2 = 1; |
novinfard | 2:24a06c840c25 | 163 | led3 = 1; |
novinfard | 2:24a06c840c25 | 164 | led4 = 1; |
novinfard | 2:24a06c840c25 | 165 | led5 = 1; |
novinfard | 2:24a06c840c25 | 166 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 167 | vToString(volts, vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 168 | counter++ ; |
novinfard | 2:24a06c840c25 | 169 | |
novinfard | 2:24a06c840c25 | 170 | // every 1 s this loop will operate |
WilliamMarshQMUL | 0:ad3cdc525b99 | 171 | if (counter == 10) { // limit bandwidth of serial |
WilliamMarshQMUL | 0:ad3cdc525b99 | 172 | pc.printf(vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 173 | counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 174 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 175 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 176 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 177 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 178 | } |