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: EthernetNetIf mbed
main.cpp
00001 00002 #define SAMPLE_RATE 150000 00003 #include "mbed.h" 00004 #include "adc.h" 00005 #include "EthernetNetIf.h" 00006 #include "UDPSocket.h" 00007 00008 EthernetNetIf eth( IpAddr(192,168,1,5), //IP Address 00009 IpAddr(255,255,255,0), //Network Mask 00010 IpAddr(192,168,1,254), //Gateway 00011 IpAddr(192,168,1,254) //DNS 00012 ); 00013 //EthernetNetIf eth; 00014 UDPSocket udp; 00015 00016 DigitalOut int_led(LED1); 00017 DigitalOut P15(p15); 00018 Serial pc(USBTX,USBRX); 00019 Host multicast(IpAddr(192,168,1,10),27200, NULL); //Target IP and port 00020 ADC adc(SAMPLE_RATE, 1); 00021 Ticker flipper; 00022 unsigned short s1,s2,s3; 00023 unsigned short i1,i2,i3; 00024 unsigned short a1,a2,a3; 00025 unsigned short sm1, sm2, sm3; 00026 unsigned short im1, im2, im3; 00027 unsigned short am1; 00028 unsigned short smag, imag; 00029 float s0; 00030 //int k=0; 00031 //Timer tmr; 00032 Timeout timeout; 00033 char str[50]; 00034 unsigned char buf[10]; 00035 int count_puls; 00036 int led1_on = 0; 00037 void Timer_Handler() 00038 { 00039 if(count_puls) { 00040 printf("cp %d\n", count_puls); 00041 count_puls--; 00042 led1_on = !led1_on; 00043 int_led = led1_on; 00044 P15 = !led1_on; 00045 timeout.attach_us(Timer_Handler, 100); 00046 } else 00047 timeout.detach(); 00048 } 00049 00050 void onUDPSocketEvent(UDPSocketEvent e) { //Incoming UDP packet processing 00051 switch (e) { 00052 case UDPSOCKET_READABLE: //The only event for now 00053 char buf[64] = {0}; 00054 Host host; 00055 while ( int len = udp.recvfrom( buf, 63, &host ) ) { 00056 if ( len <= 0 ) 00057 break; 00058 printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf); 00059 } 00060 switch(buf[0]) { 00061 case '1': 00062 count_puls = 8; 00063 break; 00064 case '2': 00065 count_puls = 4; 00066 break; 00067 case '3': 00068 count_puls = 6; 00069 break; 00070 case '4': 00071 count_puls = 2; 00072 break; 00073 case '5': 00074 count_puls = 10; 00075 break; 00076 default: 00077 count_puls = 0; 00078 } 00079 timeout.attach_us(Timer_Handler, 100); 00080 break; 00081 } 00082 } 00083 /* 00084 void adc_comlete(uint32_t value) { 00085 samples1 = adc.read(p20); 00086 Net::poll(); //Do network stuff 00087 sprintf(str, "1=%4u\0", samples1); 00088 printf("%d\n", samples1); 00089 udp.sendto( str, strlen(str)+1, &multicast ); 00090 00091 } 00092 */ 00093 unsigned short Major(unsigned short dadc2, unsigned short dadc0, unsigned short dadc1) 00094 { 00095 //printf("%d %d %d\n", dadc2, dadc0, dadc1); 00096 unsigned short rez; 00097 int dif01, dif02, dif12; 00098 dif01 = dadc0 - dadc1; 00099 dif02 = dadc0 - dadc2; 00100 dif12 = dadc1 - dadc2; 00101 if (dif01 < 0) dif01 = -dif01; 00102 if (dif02 < 0) dif02 = -dif02; 00103 if (dif12 < 0) dif12 = -dif12; 00104 if (dif01 < dif02) { 00105 if (dif12 < dif01) 00106 dadc0 = dadc2; 00107 //else // �������� 2 00108 } else { 00109 if (dif12 < dif02) 00110 dadc0 = dadc2; // �������� 0 00111 else 00112 dadc1 = dadc2; // �������� 1 00113 } 00114 rez = dadc0 + dadc1; 00115 rez >>= 1; 00116 return rez; 00117 } 00118 int delta; 00119 float err; 00120 static int number = 0; 00121 00122 void flip() { // interrupt function: measurement and sending a UDP packet 00123 switch(number) { 00124 case 0: 00125 s1 = adc.read(p20); 00126 number++; 00127 break; 00128 case 3: 00129 s2 = adc.read(p20); 00130 number++; 00131 break; 00132 case 6: 00133 s3 = adc.read(p20); 00134 number++; 00135 sm1 = Major(s1, s2, s3); 00136 // sprintf(str, "1=%4u\0",sm1); 00137 // Net::poll(); //Do network stuff 00138 // udp.sendto( str, strlen(str)+1, &multicast ); 00139 break; 00140 case 1: 00141 number++; 00142 i1 = adc.read(p19); 00143 break; 00144 case 4: 00145 number++; 00146 i2 = adc.read(p19); 00147 break; 00148 case 7: 00149 number++; 00150 i3 = adc.read(p19); 00151 im1 = Major(i1, i2, i3); 00152 // sprintf(str, "2=%4u %4u\0",sm1, im1); 00153 // Net::poll(); //Do network stuff 00154 // udp.sendto( str, strlen(str)+1, &multicast ); 00155 break; 00156 case 2: 00157 number++; 00158 a1 = adc.read(p17); 00159 break; 00160 case 5: 00161 number++; 00162 a2 = adc.read(p17); 00163 break; 00164 case 8: 00165 number=0; 00166 a3 = adc.read(p17); 00167 am1 = Major(a1, a2, a3); 00168 sprintf(str, "2=%4u %4u %4u\0",sm1, im1, am1); 00169 Net::poll(); //Do network stuff 00170 udp.sendto( str, strlen(str)+1, &multicast ); 00171 break; 00172 } 00173 } 00174 /* 00175 void flip() { // interrupt function: measurement and sending a UDP packet 00176 // int_led = !int_led; 00177 // adc.select(p19); 00178 // adc.burst(1); 00179 // adc.start(); 00180 // wait_us(500); 00181 //if(adc.done(p20)) { 00182 switch(number) { 00183 case 0: 00184 number++; 00185 s1 = adc.read(p20); 00186 break; 00187 case 1: 00188 number++; 00189 s2 = adc.read(p20); 00190 break; 00191 case 2: 00192 number++; 00193 s3 = adc.read(p20); 00194 m1 = Major(s1, s2, s3); 00195 break; 00196 case 3: 00197 number++; 00198 s1 = adc.read(p20); 00199 break; 00200 case 4: 00201 number++; 00202 s2 = adc.read(p20); 00203 break; 00204 case 5: 00205 number++; 00206 s3 = adc.read(p20); 00207 m2 = Major(s1, s2, s3); 00208 break; 00209 case 6: 00210 number++; 00211 s1 = adc.read(p20); 00212 break; 00213 case 7: 00214 number++; 00215 s2 = adc.read(p20); 00216 break; 00217 case 8: 00218 number=0; 00219 s3 = adc.read(p20); 00220 m3 = Major(s1, s2, s3); 00221 mag = Major(m1, m2, m3); 00222 Net::poll(); //Do network stuff 00223 sprintf(str, "1=%4u\0", mag); 00224 // printf("%d\n", samples4); 00225 udp.sendto( str, strlen(str)+1, &multicast ); 00226 break; 00227 } 00228 } 00229 */ 00230 #define MASK 0xC700000C 00231 //#define MASK 0x00000000 00232 00233 int main() { 00234 pc.baud(57600); 00235 //Ethernet initializing, Serial Interface - 57600 kbit/s 00236 printf("Setting up...\n"); 00237 P15 = 1; 00238 EthernetErr ethErr = eth.setup(); 00239 if (ethErr) { 00240 // printf("Error %d in setup.\n", ethErr); 00241 return -1; 00242 } 00243 // printf("Setup OK\n"); 00244 // Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000 00245 AnalogIn in0(p20); 00246 AnalogIn in1(p19); 00247 // AnalogIn in2(p18); 00248 AnalogIn in3(p17); 00249 // AnalogIn in4(p16); 00250 // AnalogIn in5(p15); 00251 // PortOut(Port0, MASK); 00252 // DigitalOut P19(p19); 00253 AnalogOut tri(p18); 00254 // DigitalOut P17(p17); 00255 DigitalOut P16(p16); 00256 P16 = 0; // P17 = 0;// = P19 = 0; 00257 00258 udp.setOnEvent(&onUDPSocketEvent); 00259 udp.bind(multicast); 00260 00261 Net::poll(); 00262 00263 // tmr.start(); 00264 00265 //Turning ON Burst-mode ADC on pins 17,18,19,20 note: connect not used ADC ping to ground 00266 // adc.startmode(0,0); 00267 adc.burst(1); 00268 adc.setup(p20,1); 00269 adc.setup(p19,1); 00270 // adc.select(p20); 00271 // adc.start(); 00272 // adc.setup(p18,1); 00273 adc.setup(p17,1); 00274 // printf("start\n"); 00275 // printf("%d\n", adc.channel_to_pin_number(0)); 00276 00277 flipper.attach_us(&flip, 83); // Interrupt Turn ON (100 - 10kHz; 1000 - 1 kHz) 00278 tri = 0; 00279 while(1) { 00280 tri = tri + 0.001; 00281 wait_us(100); 00282 if(tri >= 0.06) { 00283 tri = 0; 00284 } 00285 } 00286 }
Generated on Fri Jul 29 2022 23:19:46 by
1.7.2