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

Committer:
IZ0ABD
Date:
Wed May 04 14:50:04 2016 +0000
Revision:
1:a71233fe1e1c
Parent:
0:57e0a19da71e
2 timers, 8 kHz analog sampling on PA0 and analog output on PA4 + a H-L signal on PC8 to measure the time required by the A/D + D/A conversion.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:57e0a19da71e 1 #include "mbed.h"
bcostm 0:57e0a19da71e 2
bcostm 0:57e0a19da71e 3 Ticker toggle_led_ticker;
IZ0ABD 1:a71233fe1e1c 4 Ticker sampler;
bcostm 0:57e0a19da71e 5
IZ0ABD 1:a71233fe1e1c 6
IZ0ABD 1:a71233fe1e1c 7 // I/Os
IZ0ABD 1:a71233fe1e1c 8 AnalogIn analog_sample(A0); // Analog IN A0
IZ0ABD 1:a71233fe1e1c 9 AnalogIn threshold_vlaue(A1); // Threshold A1
IZ0ABD 1:a71233fe1e1c 10 AnalogOut my_output(PA_4); // Analog OUT A2
bcostm 0:57e0a19da71e 11 DigitalOut led1(LED1);
IZ0ABD 1:a71233fe1e1c 12 DigitalOut do0(PC_8);
IZ0ABD 1:a71233fe1e1c 13 DigitalOut do1(PC_9);
IZ0ABD 1:a71233fe1e1c 14
IZ0ABD 1:a71233fe1e1c 15 float analog_value;
IZ0ABD 1:a71233fe1e1c 16 //uint16_t analog_value;
bcostm 0:57e0a19da71e 17
bcostm 0:57e0a19da71e 18 void toggle_led() {
bcostm 0:57e0a19da71e 19 led1 = !led1;
bcostm 0:57e0a19da71e 20 }
bcostm 0:57e0a19da71e 21
IZ0ABD 1:a71233fe1e1c 22 void ADCisr() {
IZ0ABD 1:a71233fe1e1c 23 do0 = 1;
IZ0ABD 1:a71233fe1e1c 24 analog_value = analog_sample.read(); // Converts and read the analog input value (value from 0x0000 to 0xFFFF)
IZ0ABD 1:a71233fe1e1c 25 my_output.write(analog_value);
IZ0ABD 1:a71233fe1e1c 26 }
IZ0ABD 1:a71233fe1e1c 27
bcostm 0:57e0a19da71e 28 int main() {
bcostm 0:57e0a19da71e 29 // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
IZ0ABD 1:a71233fe1e1c 30 toggle_led_ticker.attach(&toggle_led, 0.5);
IZ0ABD 1:a71233fe1e1c 31 sampler.attach(&ADCisr, 0.000125); // the address of the function to be attached (&..) and the interval (8 Khz)
bcostm 0:57e0a19da71e 32 while (true) {
bcostm 0:57e0a19da71e 33 // Do other things...
IZ0ABD 1:a71233fe1e1c 34 do0 = 0;
IZ0ABD 1:a71233fe1e1c 35 sleep(); // Enter in sleep
bcostm 0:57e0a19da71e 36 }
bcostm 0:57e0a19da71e 37 }