![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADC
Revision 6:ef4c3d706a30, committed 2020-02-27
- Comitter:
- hzsun
- Date:
- Thu Feb 27 19:57:56 2020 +0000
- Parent:
- 5:1f36332ed463
- Commit message:
- 180773877 Haozun Sun
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Feb 06 08:11:53 2020 +0000 +++ b/main.cpp Thu Feb 27 19:57:56 2020 +0000 @@ -1,15 +1,17 @@ -// LAB 3 SAMPLE PROGRAM 1 -// Revised for mbed 5 -// Revised to replace Ticker with event queue and thread #include "mbed.h" - +InterruptIn button(PTD0); //buttton function AnalogIn ain(A0) ; // Analog input -DigitalOut led1(LED_RED); // Red LED EventQueue queue; // creates an event queue, to call read ADC +DigitalOut firstLED(D3); // 1st lED +DigitalOut secondLED(D4); // 2nd lED +DigitalOut thirdLED(D5); // 3rd lED +DigitalOut fourthLED(D6); // 4th lED +DigitalOut fifthLED(D7); // 5th lED + Serial pc(USBTX, USBRX); // tx, rx, for debugging // This thread runs the event queue @@ -29,6 +31,12 @@ volatile int samples = 0 ; volatile uint16_t smoothed = 0 ; +volatile int pressEvent = 0 ; //pressEvent as comunication flag +void buttonCallback(){ //button call back function + pressEvent=!pressEvent; + } + + void readA0() { smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; samples++ ; @@ -58,31 +66,85 @@ // Attach ISR for ticker // Procss messages from mailbox int main() { - led1 = 1 ; // turn off - int volts = 0 ; - const int threshold = 100 ; + + int counter = 0 ; char vstring[] = "X.XX\r\n" ; + firstLED=0; // 1st lED + secondLED=0; // 2nd lED + thirdLED=0; // 3rd lED + fourthLED=0; // 4th lED + fifthLED=0; // 5th LED + + int volts=0; + int MaxVolt =330; // Start the event queue eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); - + button.mode(PullUp); + button.fall(&buttonCallback); // call the readA0 function every 10ms queue.call_every(10, readA0) ; + while (true) { - osEvent evt = mailbox.get(); // wait for mail - if (evt.status == osEventMail) { + osEvent evt = mailbox.get(); // wait for mail + + if (evt.status == osEventMail) { message_t* mess = (message_t*)evt.value.p ; volts = (mess->analog * 330) / 0xffff ; - mailbox.free(mess) ; // free the message space - if (volts > threshold) led1 = 0 ; else led1 = 1 ; - vToString(volts, vstring) ; - counter++ ; - if (counter == 10) { // limit bandwidth of serial - pc.printf(vstring) ; - counter = 0 ; + if(pressEvent) { //when button is pressed + MaxVolt=volts ; // get the present value as the max voltage + pressEvent =0; //set the button to up position + } + mailbox.free (mess); + + + if (volts==0) { + firstLED=0; // 1st lED + secondLED=0; // 2nd lED + thirdLED=0; // 3rd lED + fourthLED=0; // 4th lED + fifthLED=0; // 5th LED + } + + if (volts>MaxVolt/6) { //when voltage is bigger than 1/6 of the max volatge + firstLED=1; + secondLED=0; + thirdLED=0; + fourthLED=0; + fifthLED=0; } + + if (volts>2*MaxVolt/6) { //when voltage is bigger than 2/6 of the max volatge + secondLED=1; + thirdLED=0; + fourthLED=0; + fifthLED=0; + } + + if (volts>3*MaxVolt/6) { //when voltage is bigger than 3/6 of the max volatge + thirdLED=1; + fourthLED=0; + fifthLED=0; + } + + if (volts>4*MaxVolt/6) { //when voltage is bigger than 4/6 of the max volatge + fourthLED=1; + fifthLED=0; + } + + if (volts>5*MaxVolt/6) { //when voltage is bigger than 5/6 of the max volatge + fifthLED=1; + } + vToString(volts, vstring) ; // function to show the value on the computer + counter++; + if (counter==10) { + pc.printf(vstring); + counter=0; + } + } } } +