AnalougeToDigital Converter
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(PTA12); 00011 DigitalOut led2(PTA4); 00012 DigitalOut led3(PTA5); 00013 DigitalOut led4(PTC8); 00014 DigitalOut led5(PTC9); 00015 Serial pc(USBTX, USBRX); // tx, rx, for debugging 00016 00017 DigitalIn b1(PTD0, PullUp); 00018 00019 volatile int pressEvent = 0 ; // Variabe set by the polling thread 00020 00021 enum buttonPos { up, down, bounce }; // Button positions 00022 void polling() 00023 { 00024 buttonPos pos = up ; 00025 int bcounter = 0 ; 00026 00027 switch (pos) { 00028 case up : 00029 if (!b1.read()) { // now down 00030 pressEvent = 1 ; // transition occurred 00031 pos = down ; 00032 } 00033 break ; 00034 case down : 00035 if (b1 == 1) { // no longer down 00036 bcounter = 3 ; // wait four cycles 00037 pos = bounce ; 00038 } 00039 break ; 00040 case bounce : 00041 if (b1 == 0) { // down again - button has bounced 00042 pos = down ; // no event 00043 } else if (bcounter == 0) { 00044 pos = up ; // delay passed - reset to up 00045 } else { 00046 bcounter-- ; // continue waiting 00047 } 00048 break ; 00049 00050 } 00051 } 00052 00053 // Message type 00054 typedef struct { 00055 uint16_t analog; /* Analog input value */ 00056 } message_t; 00057 00058 // Mail box 00059 Mail<message_t, 2> mailbox; 00060 00061 // Function called every 10ms to read ADC 00062 // Low pass filter 00063 // Every 10th value is sent to mailbox 00064 volatile int samples = 0 ; 00065 volatile uint16_t smoothed = 0 ; 00066 void readA0() 00067 { 00068 polling(); 00069 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; // divided by 2 - reduce the signal noise 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 { 00086 s[3] = '0' + (v % 10) ; 00087 v = v / 10 ; 00088 s[2] = '0' + (v % 10) ; 00089 v = v / 10 ; 00090 s[0] = '0' + (v % 10) ; 00091 } 00092 00093 // Main program 00094 // Initialise variables 00095 // Attach ISR for ticker 00096 // Procss messages from mailbox 00097 int main() 00098 { 00099 led1 = 1 ; // turn off 00100 int volts = 0 ; 00101 int threshold = 100 ; // 1 vol 00102 int counter = 0 ; 00103 char vstring[] = "X.XX\r\n" ; 00104 00105 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms -> 10000 micro second 00106 00107 00108 while (true) { 00109 osEvent evt = mailbox.get(); // wait for mail 00110 00111 00112 00113 // every 100 ms this loop operates 00114 if (evt.status == osEventMail) { 00115 message_t* mess = (message_t*)evt.value.p ; 00116 volts = (mess->analog * 330) / 0xffff ; // 2 ^ 16 00117 00118 if (pressEvent) { 00119 pressEvent = 0 ; // clear the event variable 00120 threshold = volts; 00121 00122 } 00123 00124 mailbox.free(mess) ; // free the message space 00125 if(volts < (threshold / 6)) { 00126 led1 = 0; 00127 led2 = 0; 00128 led3 = 0; 00129 led4 = 0; 00130 led5 = 0; 00131 } 00132 if((volts > (threshold * 1/ 6))&&(volts < (threshold * 2/ 6))) { 00133 led1 = 1; 00134 led2 = 0; 00135 led3 = 0; 00136 led4 = 0; 00137 led5 = 0; 00138 } 00139 if((volts > (threshold * 2/ 6))&&(volts < (threshold * 3/ 6))) { 00140 led1 = 1; 00141 led2 = 1; 00142 led3 = 0; 00143 led4 = 0; 00144 led5 = 0; 00145 } 00146 if((volts > (threshold * 3/ 6))&&(volts < (threshold * 4/ 6))) { 00147 led1 = 1; 00148 led2 = 1; 00149 led3 = 1; 00150 led4 = 0; 00151 led5 = 0; 00152 } 00153 if((volts > (threshold * 4/ 6))&&(volts < (threshold * 5/ 6))) { 00154 led1 = 1; 00155 led2 = 1; 00156 led3 = 1; 00157 led4 = 1; 00158 led5 = 0; 00159 } 00160 if(volts > (threshold * 5/ 6)) { 00161 led1 = 1; 00162 led2 = 1; 00163 led3 = 1; 00164 led4 = 1; 00165 led5 = 1; 00166 } 00167 vToString(volts, vstring) ; 00168 counter++ ; 00169 00170 // every 1 s this loop will operate 00171 if (counter == 10) { // limit bandwidth of serial 00172 pc.printf(vstring) ; 00173 counter = 0 ; 00174 } 00175 } 00176 00177 } 00178 }
Generated on Thu Jul 14 2022 13:00:28 by
1.7.2
