the code for Lab3
Fork of ADCandticker_sample by
Embed:
(wiki syntax)
Show/hide line numbers
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); // Red LED 00011 DigitalOut led2(D9); 00012 DigitalOut led3(D10); 00013 DigitalOut led4(D11); 00014 DigitalOut led5(D12); 00015 00016 Serial pc(USBTX, USBRX); // tx, rx, for debugging 00017 DigitalIn b1(PTE1,PullUp); 00018 00019 Thread pollT ; // thread to poll 00020 volatile int pressEvent = 0 ; // Variabe set by the polling thread 00021 volatile int maxvolt = 330; 00022 00023 /// implement the botton here ///////////////////////////////////// 00024 00025 00026 enum buttonPos { up, down, bounce }; // Button positions 00027 void polling() { 00028 buttonPos pos = up ; 00029 int bcounter = 0 ; 00030 while (true) { 00031 switch (pos) { 00032 case up : 00033 if (!b1.read()) { // now down 00034 pressEvent = 1 ; // transition occurred 00035 pos = down ; 00036 } 00037 break ; 00038 case down : 00039 if (b1 == 1) { // no longer down 00040 bcounter = 3 ; // wait four cycles 00041 pos = bounce ; 00042 } 00043 break ; 00044 case bounce : 00045 if (b1 == 0) { // down again - button has bounced 00046 pos = down ; // no event 00047 } else if (bcounter == 0) { 00048 pos = up ; // delay passed - reset to up 00049 } else { 00050 bcounter-- ; // continue waiting 00051 } 00052 break ; 00053 } 00054 Thread::wait(30); 00055 } 00056 } 00057 // end the button ////////////////////////////////////////////////// 00058 00059 // Message type 00060 typedef struct { 00061 uint16_t analog; /* Analog input value */ 00062 } message_t; 00063 00064 // Mail box 00065 Mail<message_t, 2> mailbox; 00066 00067 // Function called every 10ms to read ADC 00068 // Low pass filter 00069 // Every 10th value is sent to mailbox 00070 volatile int samples = 0 ; 00071 volatile uint16_t smoothed = 0 ; 00072 void readA0() { 00073 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; 00074 samples++ ; 00075 if (samples == 10) { 00076 // send to thread 00077 message_t *mess = mailbox.alloc() ; // may fail but does not block 00078 if (mess) { 00079 mess->analog = smoothed ; 00080 mailbox.put(mess); // fails but does not block if full 00081 } 00082 samples = 0; 00083 } 00084 } 00085 00086 // Write voltage digits 00087 // v Voltage as scale int, e.g. 3.30 is 330 00088 void vToString(int v, char* s) { 00089 s[3] = '0' + (v % 10) ; 00090 v = v / 10 ; 00091 s[2] = '0' + (v % 10) ; 00092 v = v / 10 ; 00093 s[0] = '0' + (v % 10) ; 00094 } 00095 00096 // Main program 00097 // Initialise variables 00098 // Attach ISR for ticker 00099 // Procss messages from mailbox 00100 int main() { 00101 led1 = 1 ; // turn off 00102 led2 = 1; 00103 led3 = 1; 00104 led4 = 1; 00105 led5 = 1; 00106 int volts = 0 ; 00107 //const int threshold; 00108 const int threshold1 = 55 ; 00109 const int threshold2 = 110 ; 00110 const int threshold3 = 165 ; 00111 const int threshold4 = 220 ; 00112 const int threshold5 = 275 ; 00113 int counter = 0 ; 00114 char vstring[] = "X.XX\r\n" ; 00115 //threshold1 = maxvolt/6; 00116 00117 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms 00118 while (true) { 00119 osEvent evt = mailbox.get(); // wait for mail 00120 if (evt.status == osEventMail) { 00121 message_t* mess = (message_t*)evt.value.p ; 00122 volts = (mess->analog * 330) / 0xffff ; 00123 mailbox.free(mess) ; // free the message space 00124 00125 ////press event //////////////////////////////////// 00126 00127 if(pressEvent) 00128 { 00129 pressEvent =0; 00130 maxvolt = volts; 00131 /// update the threshold here 00132 //threshold1 = maxvolt / 6; 00133 led1 = 1; 00134 led2 = 1; 00135 led3 = 1; 00136 led4 = 1; 00137 led5 = 1; 00138 } 00139 00140 // End of the pressEvent //////////////////////////// 00141 00142 if (volts > threshold1) led1 = 1 ; else led1 = 0 ; 00143 if (volts > threshold2) led2 = 1 ; else led2 = 0 ; 00144 if (volts > threshold3) led3 = 1 ; else led3 = 0 ; 00145 if (volts > threshold4) led4 = 1 ; else led4 = 0 ; 00146 if (volts > threshold5) led5 = 1 ; else led5 = 0 ; 00147 vToString(volts, vstring) ; 00148 counter++ ; 00149 if (counter == 10) { // limit bandwidth of serial 00150 pc.printf(vstring) ; 00151 counter = 0 ; 00152 } 00153 } 00154 00155 } 00156 }
Generated on Sun Jul 17 2022 13:35:18 by
1.7.2
