10kHz ADC which reads from 17,18,19,20 pins and send the result to remote server (multicast now) via UDP

Dependencies:   EthernetNetIf mbed

Revision:
0:7744cef9f8af
Child:
1:beae6624e569
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 13 14:57:54 2012 +0000
@@ -0,0 +1,90 @@
+
+#define SAMPLE_RATE    1500000
+#include "mbed.h"
+#include "adc.h"
+#include "EthernetNetIf.h"
+#include "UDPSocket.h"
+
+EthernetNetIf eth;
+UDPSocket udp;
+
+DigitalOut int_led(LED1);
+Serial pc(USBTX,USBRX);
+Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL);
+ADC adc(SAMPLE_RATE, 1);
+Ticker flipper;
+unsigned short samples1,samples2,samples3,samples4;
+int k=0;
+Timer tmr;
+char str[50];
+
+
+void onUDPSocketEvent(UDPSocketEvent e) {  //îáðàáîòêà ïðèõîäÿùåãî ïàêåòà UDP
+    switch (e) {
+        case UDPSOCKET_READABLE: //The only event for now
+            char buf[64] = {0};
+            Host host;
+            while ( int len = udp.recvfrom( buf, 63, &host ) ) {
+                if ( len <= 0 )
+                    break;
+                printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf);
+            }
+            break;
+    }
+}
+
+void flip() {                       // èçìåðåíèå ïî ïðåðûâàíèþ è îòïðàâêà â UDP
+    int_led = !int_led;
+/*    samples1=adc.read(p20);
+    samples2=adc.read(p19);
+    samples3=adc.read(p18);
+    samples4=adc.read(p17);*/
+    k++;                             //ñ÷åò÷èê êîëè÷åñòâà öèêëîâ
+    sprintf(str, "1=%04u 2=%04u 3=%04u 4=%04u",adc.read(p17),adc.read(p18),adc.read(p19),adc.read(p20));
+    udp.sendto( str, strlen(str), &multicast );
+}
+
+
+int main() {
+    pc.baud(57600);
+    //óñòàíîâêà Ethernet
+    printf("Setting up...\n");
+    EthernetErr ethErr = eth.setup();
+    if (ethErr) {
+        printf("Error %d in setup.\n", ethErr);
+        return -1;
+    }
+    printf("Setup OK\n");
+ //   Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000
+    udp.setOnEvent(&onUDPSocketEvent);
+    udp.bind(multicast);
+    
+    Net::poll();
+    
+    tmr.start();
+
+    //âêëþ÷àåì  ADC íà ïèíàõ 17,18,19,20
+    adc.startmode(0,0);
+    adc.burst(1);
+    adc.setup(p20,1);
+    adc.setup(p19,1);
+    adc.setup(p18,1);
+    adc.setup(p17,1);
+
+    printf("start\n");
+
+    flipper.attach_us(&flip, 100); // âêëþ÷àåì ïðåðûâàíèå
+    wait(10);                      // ìåðÿåì 10 ñåêóíä
+    flipper.detach();
+
+    adc.burst(0);                 //âûêëþ÷àåì ADC
+    adc.setup(p20,0);
+    adc.setup(p19,0);
+    adc.setup(p18,0);
+    adc.setup(p17,0);
+
+
+    printf("number of cycles= %d\n",k);
+    printf("End: %f\n",tmr.read());
+
+}
\ No newline at end of file