![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Analog to digital conversion. Reads voltage and prints it in the terminal. Switches on/off the leads depending on the voltage threashold. Uses button for voltage callibration.
Fork of ADCandticker_sample by
main.cpp@2:26893e7c0068, 2018-02-09 (annotated)
- Committer:
- natgovor
- Date:
- Fri Feb 09 16:31:44 2018 +0000
- Revision:
- 2:26893e7c0068
- Parent:
- 1:126dd2f5fc2d
Analog to digital conversion. Reads voltage and displays it in the terminal.
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 |
natgovor | 2:26893e7c0068 | 10 | |
natgovor | 2:26893e7c0068 | 11 | const int states_number = 6; // number of leds + 1 |
natgovor | 2:26893e7c0068 | 12 | DigitalOut leds[5] = { PTA13, PTD5, PTD0, PTD2, PTD3 }; |
natgovor | 2:26893e7c0068 | 13 | |
natgovor | 2:26893e7c0068 | 14 | // button setup |
natgovor | 2:26893e7c0068 | 15 | DigitalIn button(PTE1, PullUp); |
natgovor | 2:26893e7c0068 | 16 | enum buttonPos { up, down, bounce }; // Button positions |
natgovor | 2:26893e7c0068 | 17 | buttonPos pos = up ; |
natgovor | 2:26893e7c0068 | 18 | int bcounter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 19 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 20 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
WilliamMarshQMUL | 0:ad3cdc525b99 | 21 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 22 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 23 | typedef struct { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 24 | uint16_t analog; /* Analog input value */ |
natgovor | 2:26893e7c0068 | 25 | bool buttonPressed; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 26 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 27 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 28 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 29 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 30 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 31 | // Function called every 10ms to read ADC |
WilliamMarshQMUL | 0:ad3cdc525b99 | 32 | // Low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 33 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 34 | volatile int samples = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 35 | volatile uint16_t smoothed = 0 ; |
natgovor | 2:26893e7c0068 | 36 | volatile bool pressEvent = false; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 37 | void readA0() { |
natgovor | 2:26893e7c0068 | 38 | // Check button state |
natgovor | 2:26893e7c0068 | 39 | switch (pos) { |
natgovor | 2:26893e7c0068 | 40 | case up : |
natgovor | 2:26893e7c0068 | 41 | if (!button.read()) { // now down |
natgovor | 2:26893e7c0068 | 42 | pressEvent = true; |
natgovor | 2:26893e7c0068 | 43 | pos = down ; |
natgovor | 2:26893e7c0068 | 44 | } |
natgovor | 2:26893e7c0068 | 45 | break ; |
natgovor | 2:26893e7c0068 | 46 | case down : |
natgovor | 2:26893e7c0068 | 47 | if (button == 1) { // no longer down |
natgovor | 2:26893e7c0068 | 48 | bcounter = 3 ; // wait four cycles |
natgovor | 2:26893e7c0068 | 49 | pos = bounce ; |
natgovor | 2:26893e7c0068 | 50 | } |
natgovor | 2:26893e7c0068 | 51 | break ; |
natgovor | 2:26893e7c0068 | 52 | case bounce : |
natgovor | 2:26893e7c0068 | 53 | if (button == 0) { // down again - button has bounced |
natgovor | 2:26893e7c0068 | 54 | pos = down ; // no event |
natgovor | 2:26893e7c0068 | 55 | } else if (bcounter == 0) { |
natgovor | 2:26893e7c0068 | 56 | pos = up ; // delay passed - reset to up |
natgovor | 2:26893e7c0068 | 57 | } else { |
natgovor | 2:26893e7c0068 | 58 | bcounter-- ; // continue waiting |
natgovor | 2:26893e7c0068 | 59 | } |
natgovor | 2:26893e7c0068 | 60 | break ; |
natgovor | 2:26893e7c0068 | 61 | } |
natgovor | 2:26893e7c0068 | 62 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | samples++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 65 | if (samples == 10) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 66 | // send to thread |
WilliamMarshQMUL | 0:ad3cdc525b99 | 67 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 0:ad3cdc525b99 | 68 | if (mess) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 69 | mess->analog = smoothed ; |
natgovor | 2:26893e7c0068 | 70 | mess->buttonPressed = pressEvent; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 71 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 0:ad3cdc525b99 | 72 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 73 | samples = 0; |
natgovor | 2:26893e7c0068 | 74 | pressEvent = false; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 75 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 76 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 77 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 78 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 79 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 80 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 81 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 82 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 83 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 84 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 85 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 86 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 87 | |
natgovor | 2:26893e7c0068 | 88 | void lightLeds(int threshold_index) { |
natgovor | 2:26893e7c0068 | 89 | int leds_number = states_number - 1; |
natgovor | 2:26893e7c0068 | 90 | for (int i=0; i<leds_number; i++) { |
natgovor | 2:26893e7c0068 | 91 | if (i <= threshold_index - 1) { |
natgovor | 2:26893e7c0068 | 92 | leds[i] = 1; |
natgovor | 2:26893e7c0068 | 93 | } else { |
natgovor | 2:26893e7c0068 | 94 | leds[i] = 0; |
natgovor | 2:26893e7c0068 | 95 | } |
natgovor | 2:26893e7c0068 | 96 | } |
natgovor | 2:26893e7c0068 | 97 | } |
natgovor | 2:26893e7c0068 | 98 | |
natgovor | 2:26893e7c0068 | 99 | void calculate_thresholds(int max_voltage, int* threshold) { |
natgovor | 2:26893e7c0068 | 100 | int interval = max_voltage / states_number; |
natgovor | 2:26893e7c0068 | 101 | for (int i = 0; i < states_number; i++) { |
natgovor | 2:26893e7c0068 | 102 | if (i == 0) { |
natgovor | 2:26893e7c0068 | 103 | threshold[i] = interval; |
natgovor | 2:26893e7c0068 | 104 | } else { |
natgovor | 2:26893e7c0068 | 105 | threshold[i] = threshold[i-1] + interval; |
natgovor | 2:26893e7c0068 | 106 | } |
natgovor | 2:26893e7c0068 | 107 | } |
natgovor | 2:26893e7c0068 | 108 | } |
natgovor | 2:26893e7c0068 | 109 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 110 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 111 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 112 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 113 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 114 | int main() { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 115 | int volts = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 116 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 117 | char vstring[] = "X.XX\r\n" ; |
natgovor | 2:26893e7c0068 | 118 | |
natgovor | 2:26893e7c0068 | 119 | //int max_voltage = 330; |
natgovor | 2:26893e7c0068 | 120 | int threshold[states_number]; |
natgovor | 2:26893e7c0068 | 121 | |
natgovor | 2:26893e7c0068 | 122 | calculate_thresholds(330, threshold); |
WilliamMarshQMUL | 0:ad3cdc525b99 | 123 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 124 | tick.attach_us(callback(&readA0), 10000); // ticks every 10ms |
WilliamMarshQMUL | 0:ad3cdc525b99 | 125 | while (true) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 126 | osEvent evt = mailbox.get(); // wait for mail |
WilliamMarshQMUL | 0:ad3cdc525b99 | 127 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 128 | message_t* mess = (message_t*)evt.value.p ; |
natgovor | 2:26893e7c0068 | 129 | bool pressEvent = mess->buttonPressed; |
natgovor | 2:26893e7c0068 | 130 | if (pressEvent) { |
natgovor | 2:26893e7c0068 | 131 | // recalculate treshold |
natgovor | 2:26893e7c0068 | 132 | calculate_thresholds(volts, threshold); |
natgovor | 2:26893e7c0068 | 133 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 134 | volts = (mess->analog * 330) / 0xffff ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 135 | mailbox.free(mess) ; // free the message space |
natgovor | 2:26893e7c0068 | 136 | |
natgovor | 2:26893e7c0068 | 137 | if (volts < threshold[0]) { |
natgovor | 2:26893e7c0068 | 138 | lightLeds(0); |
natgovor | 2:26893e7c0068 | 139 | } else if (volts > threshold[0] && volts < threshold[1]) { |
natgovor | 2:26893e7c0068 | 140 | lightLeds(1); |
natgovor | 2:26893e7c0068 | 141 | } else if (volts > threshold[1] && volts < threshold[2]) { |
natgovor | 2:26893e7c0068 | 142 | lightLeds(2); |
natgovor | 2:26893e7c0068 | 143 | } else if (volts > threshold[2] && volts < threshold[3]) { |
natgovor | 2:26893e7c0068 | 144 | lightLeds(3); |
natgovor | 2:26893e7c0068 | 145 | } else if (volts > threshold[3] && volts < threshold[4]) { |
natgovor | 2:26893e7c0068 | 146 | lightLeds(4); |
natgovor | 2:26893e7c0068 | 147 | } else if (volts > threshold[4]) { |
natgovor | 2:26893e7c0068 | 148 | lightLeds(5); |
natgovor | 2:26893e7c0068 | 149 | } |
natgovor | 2:26893e7c0068 | 150 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 151 | vToString(volts, vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 152 | counter++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 153 | if (counter == 10) { // limit bandwidth of serial |
WilliamMarshQMUL | 0:ad3cdc525b99 | 154 | pc.printf(vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 155 | counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 156 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 157 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 158 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 159 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 160 | } |