Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ADCandticker_sample by
main.cpp
00001 00002 // LAB 3 SAMPLE PROGRAM 1 00003 // Revised for mbed 5 00004 00005 #include "mbed.h" 00006 00007 00008 Ticker tick; // Ticker for reading analog 00009 AnalogIn ain(A0) ; // Analog input 00010 DigitalOut led1(D8); // LED 1 00011 DigitalOut led2(D9); //LED 2 00012 DigitalOut led3(D10); //LED 3 00013 DigitalOut led4(D11); //LED 4 00014 DigitalOut led5(D12); //LED 5 00015 00016 DigitalIn b1(PTE1, PullUp); //button 00017 00018 Thread pollT ; // thread to poll 00019 volatile int pressEvent = 0 ; // Variabe set by the polling thread 00020 00021 enum buttonPos { up, down, bounce }; // Button positions 00022 void polling() { 00023 buttonPos pos = up ; 00024 int bcounter = 0 ; 00025 while (true) { 00026 switch (pos) { 00027 case up : 00028 if (!b1.read()) { // now down 00029 pressEvent = 1 ; // transition occurred 00030 pos = down ; 00031 } 00032 break ; 00033 case down : 00034 if (b1 == 1) { // no longer down 00035 bcounter = 3 ; // wait four cycles 00036 pos = bounce ; 00037 } 00038 break ; 00039 case bounce : 00040 if (b1 == 0) { // down again - button has bounced 00041 pos = down ; // no event 00042 } else if (bcounter == 0) { 00043 pos = up ; // delay passed - reset to up 00044 } else { 00045 bcounter-- ; // continue waiting 00046 } 00047 break ; 00048 } 00049 Thread::wait(30); 00050 } 00051 } 00052 00053 Serial pc(USBTX, USBRX); // tx, rx, for debugging 00054 00055 // Message type 00056 typedef struct { 00057 uint16_t analog; /* Analog input value */ 00058 } message_t; 00059 00060 // Mail box 00061 Mail<message_t, 2> mailbox; 00062 00063 // Function called every 10ms to read ADC 00064 // Low pass filter 00065 // Every 10th value is sent to mailbox 00066 volatile int samples = 0 ; 00067 volatile uint16_t smoothed = 0 ; 00068 void readA0() { 00069 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; 00070 samples++ ; 00071 if (samples == 10) { 00072 // send to thread 00073 message_t *mess = mailbox.alloc() ; // may fail but does not block 00074 if (mess) { 00075 mess->analog = smoothed ; 00076 mailbox.put(mess); // fails but does not block if full 00077 } 00078 samples = 0; 00079 } 00080 } 00081 00082 // Write voltage digits 00083 // v Voltage as scale int, e.g. 3.30 is 330 00084 void vToString(int v, char* s) { 00085 s[3] = '0' + (v % 10) ; 00086 v = v / 10 ; 00087 s[2] = '0' + (v % 10) ; 00088 v = v / 10 ; 00089 s[0] = '0' + (v % 10) ; 00090 } 00091 00092 // Main program 00093 // Initialise variables 00094 // Attach ISR for ticker 00095 // Procss messages from mailbox 00096 int main() { 00097 00098 00099 pollT.start(callback(polling)); 00100 int firstled; 00101 int secondled; 00102 int thirdled; 00103 int fourthled; 00104 int fifthled; 00105 volatile int max = 330; //initial max voltage level required to light up the last LED 00106 00107 int volts = 0 ; 00108 00109 int counter = 0 ; 00110 char vstring[] = "X.XX\r\n" ; 00111 00112 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms 00113 while (true) { 00114 osEvent evt = mailbox.get(); // wait for mail 00115 if (evt.status == osEventMail) { 00116 message_t* mess = (message_t*)evt.value.p ; 00117 volts = (mess->analog * 330) / 0xffff ; 00118 if (pressEvent) { 00119 pressEvent = 0 ; // clear the event variable 00120 max=volts ; //set the present voltage as the new maximum voltage to light up the last LED 00121 } 00122 mailbox.free(mess) ; // free the message space 00123 00124 firstled = max*2/6; //voltage required to light up the first led 00125 secondled = max*3/6; //voltage required to light up the second led 00126 thirdled = max*4/6; //voltage required to light up the third led 00127 fourthled = max*5/6; //voltage required to light up the fourth led 00128 fifthled = max; //voltage required to light up the fifth led 00129 00130 if (volts >= firstled) led1 = 1 ; else led1 = 0 ; 00131 if (volts >= secondled) led2 = 1; else led2 = 0; 00132 if (volts >=thirdled) led3 = 1; else led3 = 0; 00133 if (volts >= fourthled) led4 = 1; else led4 = 0; 00134 if (volts >= fifthled) led5 = 1; else led5 = 0; 00135 00136 vToString(volts, vstring) ; 00137 counter++ ; 00138 if (counter == 10) { // limit bandwidth of serial 00139 pc.printf(vstring) ; 00140 counter = 0 ; 00141 } 00142 } 00143 00144 } 00145 }
Generated on Thu Jul 14 2022 23:18:17 by
1.7.2
