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.
Dependencies: mbed EthernetNetIf
main.cpp
00001 #define SAMPLE_RATE 50000 00002 #include "mbed.h" 00003 #include "adc.h" 00004 #include "EthernetNetIf.h" 00005 #include "UDPSocket.h" 00006 #include "MedianFilter.h" 00007 struct { 00008 char key; 00009 unsigned short v1; 00010 unsigned short v2; 00011 unsigned short v3; 00012 unsigned short v4; 00013 } buff; 00014 00015 00016 EthernetNetIf eth( IpAddr(192,168,1,155), //IP Address 00017 IpAddr(255,255,255,0), //Network Mask 00018 IpAddr(192,168,1,254), //Gateway 00019 IpAddr(192,168,1,254) //DNS 00020 ); 00021 //EthernetNetIf eth; 00022 UDPSocket udp; 00023 00024 DigitalOut int_led1(LED1); 00025 DigitalOut int_led2(LED2); 00026 DigitalOut int_led3(LED3); 00027 DigitalOut int_led4(LED4); 00028 //DigitalOut P21(p21); 00029 //DigitalOut P22(p22); 00030 //DigitalOut P23(p23); 00031 //DigitalOut P24(p24); 00032 //Serial pc(USBTX,USBRX); 00033 Host multicast(IpAddr(192,168,1,51),27200, NULL); //Target IP and port 00034 ADC adc(SAMPLE_RATE, 1); 00035 Ticker flipper; 00036 unsigned short s1,s2,s3; 00037 unsigned short i1,i2,i3; 00038 unsigned short a1,a2,a3; 00039 unsigned short b1,b2,b3; 00040 unsigned short bm1; 00041 unsigned short sm1, sm2, sm3; 00042 unsigned short im1, im2, im3; 00043 unsigned short am1, am2, am3; 00044 unsigned short smag, imag, amag; 00045 float s0; 00046 00047 MedianFilter mf16(9); 00048 MedianFilter mf17(9); 00049 MedianFilter mf19(9); 00050 MedianFilter mf20(9); 00051 void flip(); 00052 ///////////////////////////////////////// 00053 DigitalInOut port_in_out[4] = {DigitalInOut(p21),DigitalInOut(p22),DigitalInOut(p23),DigitalInOut(p24)}; 00054 //DigitalInOut P21(p21); 00055 //DigitalInOut P22(p22); 00056 //DigitalInOut P23(p23); 00057 //DigitalInOut P24(p24); 00058 int count_puls; 00059 int mode_read; 00060 int device_no; 00061 Timeout timeout; 00062 Timeout timeout2; 00063 InterruptIn port_interrupt[4] = {InterruptIn(p21),InterruptIn(p22),InterruptIn(p23),InterruptIn(p24)}; 00064 //InterruptIn impuls21(p21); 00065 //InterruptIn impuls22(p22); 00066 //InterruptIn impuls23(p23); 00067 //InterruptIn impuls24(p24); 00068 int count_imp = 0; 00069 void impuls_fall() {count_imp++;} 00070 ///////////////////////////////////////// 00071 unsigned char send_buf[16]; 00072 00073 void Handler21() { 00074 } 00075 00076 void Timer_Handler21() { 00077 send_buf[0] = '5'; 00078 send_buf[2] = device_no - 1; 00079 switch(count_imp) { 00080 case 0: 00081 send_buf[1] = 0; 00082 break; 00083 case 1: 00084 send_buf[1] = 1; 00085 break; 00086 case 2: 00087 send_buf[1] = 3; 00088 break; 00089 case 3: 00090 send_buf[1] = 4; 00091 break; 00092 case 4: 00093 send_buf[1] = 2; 00094 break; 00095 case 5: 00096 send_buf[1] = 5; 00097 break; 00098 } 00099 Net::poll(); //Do network stuff 00100 udp.sendto((char*)&send_buf, 3, &multicast ); 00101 timeout2.detach(); 00102 // pc.printf("%d %s\n", count_imp, (char*)&send_buf); 00103 } 00104 00105 00106 void Timer_Handler() 00107 { 00108 if(count_puls) { 00109 unsigned char v = count_puls % 2; 00110 count_puls--; 00111 // led1_on = !led1_on; 00112 // int_led1 = led1_on; 00113 port_in_out[device_no-1] = v; 00114 /* switch(device_no) { 00115 case 1: 00116 P21 = v; 00117 break; 00118 case 2: 00119 P22 = v; 00120 break; 00121 case 3: 00122 P23 = v; 00123 break; 00124 case 4: 00125 P24 = v; 00126 break; 00127 default: 00128 timeout.detach(); 00129 return; 00130 } 00131 */ 00132 if(mode_read) { 00133 timeout.attach_us(Timer_Handler, 30000); 00134 mode_read = 0; 00135 int_led1 = 1; 00136 00137 } else { 00138 timeout.attach_us(Timer_Handler, 1000); 00139 int_led1 = 0; 00140 } 00141 } else { 00142 timeout.detach(); 00143 port_in_out[device_no-1].input(); 00144 timeout2.attach_us(Timer_Handler21, 200000); 00145 count_imp = 0; 00146 00147 } 00148 } 00149 bool transmit_enable = false; 00150 void StartTransmit(bool start) { 00151 if(start) { 00152 transmit_enable = true; 00153 // flipper.attach_us(&flip, 200); // Interrupt Turn ON (100 - 10kHz; 1000 - 1 kHz) 00154 } else { 00155 transmit_enable = false; 00156 // flipper.detach(); 00157 // flipper.attach_us(&flip, 200000); // Interrupt Turn ON (100 - 10kHz; 1000 - 1 kHz) 00158 } 00159 } 00160 00161 void onUDPSocketEvent(UDPSocketEvent e) { //Incoming UDP packet processing 00162 int len; 00163 switch (e) { 00164 case UDPSOCKET_READABLE: //The only event for now 00165 char buf[64] = {0}; 00166 Host host; 00167 while ( len = udp.recvfrom( buf, 63, &host ), len ) { 00168 if ( len <= 0 ) 00169 break; 00170 // printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf); 00171 } 00172 switch(buf[0]) { 00173 case 'S': 00174 StartTransmit(true); 00175 return; 00176 case 's': 00177 StartTransmit(false); 00178 return; 00179 case '1': 00180 device_no = 1; 00181 // int_led1 = 1; 00182 break; 00183 case '2': 00184 device_no = 2; 00185 // int_led2 = 1; 00186 break; 00187 case '3': 00188 device_no = 3; 00189 // int_led3 = 1; 00190 break; 00191 case '4': 00192 device_no = 4; 00193 // int_led3 = 1; 00194 break; 00195 default: 00196 device_no = 0; 00197 // int_led4 = 1; 00198 count_puls = 0; 00199 return; 00200 } 00201 int_led1 = 0; 00202 int_led2 = 0; 00203 int_led3 = 0; 00204 int_led4 = 0; 00205 if(buf[2] == 0) { 00206 mode_read = 0; 00207 switch(buf[1]) { 00208 case '1': 00209 count_puls = 2; //8 00210 // int_led1 = 1; 00211 break; 00212 case '2': 00213 count_puls = 8; //4; 00214 // int_led2 = 1; 00215 break; 00216 case '3': 00217 count_puls = 4; //6; 00218 // int_led3 = 1; 00219 break; 00220 case '4': 00221 count_puls = 6; //2;8 00222 // int_led4 = 1; 00223 break; 00224 case '5': 00225 count_puls = 10; 00226 // int_led1 = 1; 00227 // int_led2 = 1; 00228 break; 00229 default: 00230 count_puls = 0; 00231 } 00232 } else { 00233 count_puls = 2; 00234 mode_read = 1; 00235 00236 // led1_on = 1; 00237 } 00238 port_in_out[device_no-1].output(); 00239 timeout.attach_us(Timer_Handler, 1000); 00240 break; 00241 } 00242 } 00243 00244 00245 00246 //static int number20 = 0; 00247 //static int pass20 = 0; 00248 int ccc = 0; 00249 int number = 0; 00250 int complete20 = 1; 00251 int d20 = 0; 00252 void adc_comlete20(uint32_t value) { 00253 // if(++d20 == 10000) 00254 // int_led1 = 1; 00255 // if(d20 == 20000) { 00256 // int_led1 = 0; 00257 // d20 = 0; 00258 // } 00259 s1 = adc.read(p20); 00260 sm1 = mf20.GetMedian(s1); 00261 // adc.interrupt_state(p20, 0); 00262 complete20 = 1; 00263 adc.interrupt_state(p20, 0); 00264 // adc.select(p16); 00265 adc.interrupt_state(p16, 1); 00266 } 00267 00268 00269 int d19 = 0; 00270 //static int number19 = 0; 00271 int complete19 = 1; 00272 //static int pass19 = 0; 00273 void adc_comlete19(uint32_t value) { 00274 // if(++d19 == 10000) 00275 // int_led2 = 1; 00276 // if(d19 == 20000) { 00277 // int_led2 = 0; 00278 // d19 = 0; 00279 // } 00280 i1 = adc.read(p19); 00281 im1 = mf19.GetMedian(i1); 00282 complete19 = 1; 00283 adc.interrupt_state(p19, 0); 00284 // adc.select(p20); 00285 adc.interrupt_state(p20, 1); 00286 } 00287 00288 int complete17 = 1; 00289 int d17 = 0; 00290 void adc_comlete17(uint32_t value) { 00291 // if(++d17 == 10000) 00292 // int_led3 = 1; 00293 // if(d17 == 20000) { 00294 // int_led3 = 0; 00295 // d17 = 0; 00296 // } 00297 b1 = adc.read(p17); 00298 bm1 = mf17.GetMedian(b1); 00299 complete17 = 1; 00300 adc.interrupt_state(p17, 0); 00301 // adc.select(p19); 00302 adc.interrupt_state(p19, 1); 00303 } 00304 00305 int d16 = 0; 00306 //static int number16 = 0; 00307 int complete16 = 1; 00308 //int pass16 = 0; 00309 void adc_comlete16(uint32_t value) { 00310 // if(++d16 == 10000) 00311 // int_led4 = 1; 00312 // if(d16 == 20000) { 00313 // int_led4 = 0; 00314 // d16 = 0; 00315 // } 00316 a1 = adc.read(p16); 00317 am1 = mf16.GetMedian(a1); 00318 complete16 = 1; 00319 adc.interrupt_state(p16, 0); 00320 // adc.select(p17); 00321 adc.interrupt_state(p17, 1); 00322 } 00323 00324 int delta; 00325 float err; 00326 00327 void flip() { 00328 //if(!complete20) 00329 // int_led4 = 1; 00330 // if(complete20 && complete19 && complete16) { 00331 { //complete20 = complete19 = 00332 complete16 = 0; 00333 // int_led4 = 1; 00334 // sprintf(str, "2=%4u %4u %4u\0",a1, im1, a1); 00335 // printf(str); 00336 buff.key = '2'; 00337 buff.v1 = sm1; 00338 buff.v2 = im1; 00339 buff.v3 = am1; 00340 buff.v4 = bm1; 00341 Net::poll(); //Do network stuff 00342 if(transmit_enable) 00343 udp.sendto((char*)&buff, sizeof(buff), &multicast ); 00344 // udp.sendto( str, strlen(str)+1, &multicast ); 00345 // LPC_ADC->ADINTEN = 0; 00346 // adc.interrupt_state(p20, 1); 00347 // adc.interrupt_state(p19, 1); 00348 adc.interrupt_state(p16, 1); 00349 // int_led4 = 0; 00350 // LPC_ADC->ADINTEN &= ~0x100; 00351 // LPC_ADC->ADINTEN |= 1 << chan; 00352 // LPC_ADC->ADINTEN |= 0x32; 00353 /* Enable the ADC Interrupt */ 00354 // NVIC_EnableIRQ(ADC_IRQn); 00355 } 00356 // else { 00357 // int_led4 = 0; 00358 // } 00359 } 00360 00361 #define MASK 0xC700000C 00362 //#define MASK 0x00000000 00363 00364 int main() { 00365 // pc.baud(57600); 00366 //Ethernet initializing, Serial Interface - 57600 kbit/s 00367 // printf("Setting up...\n"); 00368 port_in_out[0].output(); 00369 port_in_out[1].output(); 00370 port_in_out[2].output(); 00371 port_in_out[3].output(); 00372 port_in_out[0]=1; 00373 port_in_out[1]=1; 00374 port_in_out[2]=1; 00375 port_in_out[3]=1; 00376 EthernetErr ethErr = eth.setup(); 00377 // if (ethErr) { 00378 // printf("Error %d in setup.\n", ethErr); 00379 // return -1; 00380 // } 00381 // printf("Setup OK\n"); 00382 // Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000 00383 AnalogIn in0(p20); 00384 AnalogIn in1(p19); 00385 AnalogIn in2(p17); 00386 AnalogIn in3(p16); 00387 // PortOut(Port0, MASK); 00388 // DigitalOut P19(p19); 00389 AnalogOut tri(p18); 00390 // DigitalOut P17(p17); 00391 DigitalOut P15(p15); 00392 // P17 = 0; 00393 P15 = 0;// = P19 = 0; 00394 port_interrupt[0].fall(&impuls_fall); 00395 port_interrupt[1].fall(&impuls_fall); 00396 port_interrupt[2].fall(&impuls_fall); 00397 port_interrupt[3].fall(&impuls_fall); 00398 00399 udp.setOnEvent(&onUDPSocketEvent); 00400 udp.bind(multicast); 00401 00402 Net::poll(); 00403 00404 // tmr.start(); 00405 00406 //Turning ON Burst-mode ADC on pins 17,18,19,20 note: connect not used ADC ping to ground 00407 // adc.startmode(0,0); 00408 adc.burst(1); 00409 adc.setup(p20,1); 00410 adc.setup(p19,1); 00411 // adc.select(p20); 00412 // adc.start(); 00413 adc.setup(p17,1); 00414 adc.setup(p16,1); 00415 // printf("start\n"); 00416 // printf("%d\n", adc.channel_to_pin_number(0)); 00417 //complete20 = complete19 = 00418 complete16 = 0; 00419 adc.append(p20, adc_comlete20); 00420 adc.interrupt_state(p20, 0); 00421 adc.append(p19, adc_comlete19); 00422 adc.interrupt_state(p19, 0); 00423 adc.append(p17, adc_comlete17); 00424 adc.interrupt_state(p17, 0); 00425 adc.append(p16, adc_comlete16); 00426 adc.interrupt_state(p16, 0); 00427 flipper.attach_us(&flip, 200); // Interrupt Turn ON (100 - 10kHz; 1000 - 1 kHz) 00428 tri = 0; 00429 while(1) { 00430 tri = tri + 0.001; 00431 wait_us(100); 00432 if(tri >= 0.06) { 00433 tri = 0; 00434 } 00435 } 00436 }
Generated on Thu Jul 21 2022 09:18:57 by
