Analog to digital and digital to analog conversion at 8 kHz rate on PA0 (A/D) and PA4 (D/A): use of timers and sleep() (to save energy). On PC8 there's a rising and falling signal to measure the time required by the A/D and D/A interrupt service routine.

Dependencies:   mbed-src mbed

Revision:
1:a71233fe1e1c
Parent:
0:57e0a19da71e
diff -r 57e0a19da71e -r a71233fe1e1c main.cpp
--- a/main.cpp	Tue Dec 16 13:17:50 2014 +0000
+++ b/main.cpp	Wed May 04 14:50:04 2016 +0000
@@ -1,17 +1,37 @@
 #include "mbed.h"
 
 Ticker toggle_led_ticker;
+Ticker sampler;
 
+
+// I/Os
+AnalogIn analog_sample(A0);      // Analog IN A0
+AnalogIn threshold_vlaue(A1);    // Threshold A1
+AnalogOut my_output(PA_4);       // Analog OUT A2
 DigitalOut led1(LED1);
+DigitalOut do0(PC_8);
+DigitalOut do1(PC_9);
+
+float analog_value;
+//uint16_t analog_value;
 
 void toggle_led() {
     led1 = !led1;
 }
 
+void ADCisr() {
+    do0 = 1;
+    analog_value = analog_sample.read(); // Converts and read the analog input value (value from 0x0000 to 0xFFFF)
+    my_output.write(analog_value);
+}
+
 int main() {
     // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
-    toggle_led_ticker.attach(&toggle_led, 0.1);
+    toggle_led_ticker.attach(&toggle_led, 0.5);
+    sampler.attach(&ADCisr, 0.000125); // the address of the function to be attached (&..) and the interval (8 Khz)
     while (true) {
         // Do other things...
+        do0 = 0;
+    sleep();        // Enter in sleep
     }
 }
\ No newline at end of file