Haozun Sun
/
ADC_HaozunSun_180773877
ADC
main.cpp@6:ef4c3d706a30, 2020-02-27 (annotated)
- Committer:
- hzsun
- Date:
- Thu Feb 27 19:57:56 2020 +0000
- Revision:
- 6:ef4c3d706a30
- Parent:
- 4:ebd00f94455a
180773877 Haozun Sun
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:ad3cdc525b99 | 1 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 2 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 3 | #include "mbed.h" |
WilliamMarshQMUL | 1:126dd2f5fc2d | 4 | |
hzsun | 6:ef4c3d706a30 | 5 | InterruptIn button(PTD0); //buttton function |
WilliamMarshQMUL | 0:ad3cdc525b99 | 6 | AnalogIn ain(A0) ; // Analog input |
WilliamMarshQMUL | 4:ebd00f94455a | 7 | EventQueue queue; // creates an event queue, to call read ADC |
WilliamMarshQMUL | 0:ad3cdc525b99 | 8 | |
hzsun | 6:ef4c3d706a30 | 9 | DigitalOut firstLED(D3); // 1st lED |
hzsun | 6:ef4c3d706a30 | 10 | DigitalOut secondLED(D4); // 2nd lED |
hzsun | 6:ef4c3d706a30 | 11 | DigitalOut thirdLED(D5); // 3rd lED |
hzsun | 6:ef4c3d706a30 | 12 | DigitalOut fourthLED(D6); // 4th lED |
hzsun | 6:ef4c3d706a30 | 13 | DigitalOut fifthLED(D7); // 5th lED |
hzsun | 6:ef4c3d706a30 | 14 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 15 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
WilliamMarshQMUL | 0:ad3cdc525b99 | 16 | |
WilliamMarshQMUL | 4:ebd00f94455a | 17 | // This thread runs the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 18 | Thread eventThread ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 19 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 20 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 21 | typedef struct { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 22 | uint16_t analog; /* Analog input value */ |
WilliamMarshQMUL | 0:ad3cdc525b99 | 23 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 24 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 25 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 26 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 27 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 28 | // Function called every 10ms to read ADC |
WilliamMarshQMUL | 4:ebd00f94455a | 29 | // Average using a low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 30 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 4:ebd00f94455a | 31 | volatile int samples = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 32 | volatile uint16_t smoothed = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 33 | |
hzsun | 6:ef4c3d706a30 | 34 | volatile int pressEvent = 0 ; //pressEvent as comunication flag |
hzsun | 6:ef4c3d706a30 | 35 | void buttonCallback(){ //button call back function |
hzsun | 6:ef4c3d706a30 | 36 | pressEvent=!pressEvent; |
hzsun | 6:ef4c3d706a30 | 37 | } |
hzsun | 6:ef4c3d706a30 | 38 | |
hzsun | 6:ef4c3d706a30 | 39 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 40 | void readA0() { |
WilliamMarshQMUL | 4:ebd00f94455a | 41 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 4:ebd00f94455a | 42 | samples++ ; |
WilliamMarshQMUL | 4:ebd00f94455a | 43 | if (samples == 10) { |
WilliamMarshQMUL | 4:ebd00f94455a | 44 | // send to thread |
WilliamMarshQMUL | 4:ebd00f94455a | 45 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 4:ebd00f94455a | 46 | if (mess) { |
WilliamMarshQMUL | 4:ebd00f94455a | 47 | mess->analog = smoothed ; |
WilliamMarshQMUL | 4:ebd00f94455a | 48 | mailbox.put(mess); // fails but does not block if full |
WilliamMarshQMUL | 0:ad3cdc525b99 | 49 | } |
WilliamMarshQMUL | 4:ebd00f94455a | 50 | samples = 0; |
WilliamMarshQMUL | 4:ebd00f94455a | 51 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 52 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 53 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 54 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 55 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 56 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 57 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 58 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 59 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 60 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 61 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 62 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 63 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 64 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 65 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 66 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 67 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 68 | int main() { |
hzsun | 6:ef4c3d706a30 | 69 | |
hzsun | 6:ef4c3d706a30 | 70 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 71 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 72 | char vstring[] = "X.XX\r\n" ; |
hzsun | 6:ef4c3d706a30 | 73 | firstLED=0; // 1st lED |
hzsun | 6:ef4c3d706a30 | 74 | secondLED=0; // 2nd lED |
hzsun | 6:ef4c3d706a30 | 75 | thirdLED=0; // 3rd lED |
hzsun | 6:ef4c3d706a30 | 76 | fourthLED=0; // 4th lED |
hzsun | 6:ef4c3d706a30 | 77 | fifthLED=0; // 5th LED |
hzsun | 6:ef4c3d706a30 | 78 | |
hzsun | 6:ef4c3d706a30 | 79 | int volts=0; |
hzsun | 6:ef4c3d706a30 | 80 | int MaxVolt =330; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 81 | |
WilliamMarshQMUL | 4:ebd00f94455a | 82 | // Start the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 83 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
hzsun | 6:ef4c3d706a30 | 84 | button.mode(PullUp); |
hzsun | 6:ef4c3d706a30 | 85 | button.fall(&buttonCallback); |
WilliamMarshQMUL | 4:ebd00f94455a | 86 | // call the readA0 function every 10ms |
WilliamMarshQMUL | 4:ebd00f94455a | 87 | queue.call_every(10, readA0) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 88 | |
hzsun | 6:ef4c3d706a30 | 89 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 90 | while (true) { |
hzsun | 6:ef4c3d706a30 | 91 | osEvent evt = mailbox.get(); // wait for mail |
hzsun | 6:ef4c3d706a30 | 92 | |
hzsun | 6:ef4c3d706a30 | 93 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 94 | message_t* mess = (message_t*)evt.value.p ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 95 | volts = (mess->analog * 330) / 0xffff ; |
hzsun | 6:ef4c3d706a30 | 96 | if(pressEvent) { //when button is pressed |
hzsun | 6:ef4c3d706a30 | 97 | MaxVolt=volts ; // get the present value as the max voltage |
hzsun | 6:ef4c3d706a30 | 98 | pressEvent =0; //set the button to up position |
hzsun | 6:ef4c3d706a30 | 99 | } |
hzsun | 6:ef4c3d706a30 | 100 | mailbox.free (mess); |
hzsun | 6:ef4c3d706a30 | 101 | |
hzsun | 6:ef4c3d706a30 | 102 | |
hzsun | 6:ef4c3d706a30 | 103 | if (volts==0) { |
hzsun | 6:ef4c3d706a30 | 104 | firstLED=0; // 1st lED |
hzsun | 6:ef4c3d706a30 | 105 | secondLED=0; // 2nd lED |
hzsun | 6:ef4c3d706a30 | 106 | thirdLED=0; // 3rd lED |
hzsun | 6:ef4c3d706a30 | 107 | fourthLED=0; // 4th lED |
hzsun | 6:ef4c3d706a30 | 108 | fifthLED=0; // 5th LED |
hzsun | 6:ef4c3d706a30 | 109 | } |
hzsun | 6:ef4c3d706a30 | 110 | |
hzsun | 6:ef4c3d706a30 | 111 | if (volts>MaxVolt/6) { //when voltage is bigger than 1/6 of the max volatge |
hzsun | 6:ef4c3d706a30 | 112 | firstLED=1; |
hzsun | 6:ef4c3d706a30 | 113 | secondLED=0; |
hzsun | 6:ef4c3d706a30 | 114 | thirdLED=0; |
hzsun | 6:ef4c3d706a30 | 115 | fourthLED=0; |
hzsun | 6:ef4c3d706a30 | 116 | fifthLED=0; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 117 | } |
hzsun | 6:ef4c3d706a30 | 118 | |
hzsun | 6:ef4c3d706a30 | 119 | if (volts>2*MaxVolt/6) { //when voltage is bigger than 2/6 of the max volatge |
hzsun | 6:ef4c3d706a30 | 120 | secondLED=1; |
hzsun | 6:ef4c3d706a30 | 121 | thirdLED=0; |
hzsun | 6:ef4c3d706a30 | 122 | fourthLED=0; |
hzsun | 6:ef4c3d706a30 | 123 | fifthLED=0; |
hzsun | 6:ef4c3d706a30 | 124 | } |
hzsun | 6:ef4c3d706a30 | 125 | |
hzsun | 6:ef4c3d706a30 | 126 | if (volts>3*MaxVolt/6) { //when voltage is bigger than 3/6 of the max volatge |
hzsun | 6:ef4c3d706a30 | 127 | thirdLED=1; |
hzsun | 6:ef4c3d706a30 | 128 | fourthLED=0; |
hzsun | 6:ef4c3d706a30 | 129 | fifthLED=0; |
hzsun | 6:ef4c3d706a30 | 130 | } |
hzsun | 6:ef4c3d706a30 | 131 | |
hzsun | 6:ef4c3d706a30 | 132 | if (volts>4*MaxVolt/6) { //when voltage is bigger than 4/6 of the max volatge |
hzsun | 6:ef4c3d706a30 | 133 | fourthLED=1; |
hzsun | 6:ef4c3d706a30 | 134 | fifthLED=0; |
hzsun | 6:ef4c3d706a30 | 135 | } |
hzsun | 6:ef4c3d706a30 | 136 | |
hzsun | 6:ef4c3d706a30 | 137 | if (volts>5*MaxVolt/6) { //when voltage is bigger than 5/6 of the max volatge |
hzsun | 6:ef4c3d706a30 | 138 | fifthLED=1; |
hzsun | 6:ef4c3d706a30 | 139 | } |
hzsun | 6:ef4c3d706a30 | 140 | vToString(volts, vstring) ; // function to show the value on the computer |
hzsun | 6:ef4c3d706a30 | 141 | counter++; |
hzsun | 6:ef4c3d706a30 | 142 | if (counter==10) { |
hzsun | 6:ef4c3d706a30 | 143 | pc.printf(vstring); |
hzsun | 6:ef4c3d706a30 | 144 | counter=0; |
hzsun | 6:ef4c3d706a30 | 145 | } |
hzsun | 6:ef4c3d706a30 | 146 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 147 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 148 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 149 | } |
hzsun | 6:ef4c3d706a30 | 150 |