Andrea Pallotta
/
Nucleo_analog_loop
import test
main.cpp@2:b4f8b72a22ec, 2018-02-25 (annotated)
- Committer:
- andrewPV
- Date:
- Sun Feb 25 14:34:52 2018 +0000
- Revision:
- 2:b4f8b72a22ec
- Parent:
- 1:89aa667d1a9b
Importing test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:bf84ee4ea1d2 | 1 | #include "mbed.h" |
bcostm | 0:bf84ee4ea1d2 | 2 | |
andrewPV | 2:b4f8b72a22ec | 3 | |
bcostm | 0:bf84ee4ea1d2 | 4 | AnalogIn in(A0); |
bcostm | 0:bf84ee4ea1d2 | 5 | |
bcostm | 0:bf84ee4ea1d2 | 6 | #if !DEVICE_ANALOGOUT |
bcostm | 0:bf84ee4ea1d2 | 7 | #error You cannot use this example as the AnalogOut is not supported on this device. |
bcostm | 0:bf84ee4ea1d2 | 8 | #else |
bcostm | 0:bf84ee4ea1d2 | 9 | AnalogOut out(PA_4); |
bcostm | 0:bf84ee4ea1d2 | 10 | #endif |
bcostm | 0:bf84ee4ea1d2 | 11 | |
andrewPV | 2:b4f8b72a22ec | 12 | /* #if !DEVICE_FILEHANDLE |
andrewPV | 2:b4f8b72a22ec | 13 | #error You cannot use this example as the FileHandle is not supported on this device. |
andrewPV | 2:b4f8b72a22ec | 14 | #else |
andrewPV | 2:b4f8b72a22ec | 15 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
andrewPV | 2:b4f8b72a22ec | 16 | #endif */ |
andrewPV | 2:b4f8b72a22ec | 17 | |
bcostm | 0:bf84ee4ea1d2 | 18 | DigitalOut led(LED1); |
andrewPV | 2:b4f8b72a22ec | 19 | DigitalOut set(PB_0); |
bcostm | 0:bf84ee4ea1d2 | 20 | |
andrewPV | 1:89aa667d1a9b | 21 | |
bcostm | 0:bf84ee4ea1d2 | 22 | int main() |
bcostm | 0:bf84ee4ea1d2 | 23 | { |
andrewPV | 2:b4f8b72a22ec | 24 | /* FILE *fp = fopen("/DETAILS.TXT", "w"); // Open "out.txt" on the local file system for writing |
andrewPV | 1:89aa667d1a9b | 25 | fprintf(fp, "Hello World!"); |
andrewPV | 2:b4f8b72a22ec | 26 | fclose(fp); */ |
andrewPV | 1:89aa667d1a9b | 27 | |
bcostm | 0:bf84ee4ea1d2 | 28 | printf("\nAnalog loop example\n"); |
bcostm | 0:bf84ee4ea1d2 | 29 | printf("*** Connect A0 and PA_4 pins together ***\n"); |
andrewPV | 2:b4f8b72a22ec | 30 | int i (0); |
andrewPV | 2:b4f8b72a22ec | 31 | while(i <= 10) { |
andrewPV | 2:b4f8b72a22ec | 32 | set.write(0); |
andrewPV | 2:b4f8b72a22ec | 33 | float in_value = in.read(); |
bcostm | 0:bf84ee4ea1d2 | 34 | for (float out_value = 0.0f; out_value < 1.1f; out_value += 0.1f) { |
bcostm | 0:bf84ee4ea1d2 | 35 | // Output value using DAC |
bcostm | 0:bf84ee4ea1d2 | 36 | out.write(out_value); |
bcostm | 0:bf84ee4ea1d2 | 37 | wait(0.1); |
bcostm | 0:bf84ee4ea1d2 | 38 | // Read ADC input |
bcostm | 0:bf84ee4ea1d2 | 39 | float in_value = in.read(); |
bcostm | 0:bf84ee4ea1d2 | 40 | // Display difference between two values |
andrewPV | 2:b4f8b72a22ec | 41 | float diff = fabs(out_value - in_value)*3.3f; |
andrewPV | 2:b4f8b72a22ec | 42 | in_value *= 3.3f; |
andrewPV | 2:b4f8b72a22ec | 43 | out_value *= 3.3f; |
andrewPV | 2:b4f8b72a22ec | 44 | printf("(out:%.4f V) - (in:%.4f V) = (%.4f V) ", out_value, in_value, diff); |
andrewPV | 2:b4f8b72a22ec | 45 | out_value /= 3.3f; |
andrewPV | 2:b4f8b72a22ec | 46 | in_value /= 3.3f; |
andrewPV | 2:b4f8b72a22ec | 47 | diff /= 3.3f; |
bcostm | 0:bf84ee4ea1d2 | 48 | if (diff > 0.05f) { |
bcostm | 0:bf84ee4ea1d2 | 49 | printf("FAIL\n"); |
bcostm | 0:bf84ee4ea1d2 | 50 | } else { |
bcostm | 0:bf84ee4ea1d2 | 51 | printf("OK\n"); |
bcostm | 0:bf84ee4ea1d2 | 52 | printf("\033[1A"); // Moves cursor up of 1 line |
bcostm | 0:bf84ee4ea1d2 | 53 | } |
bcostm | 0:bf84ee4ea1d2 | 54 | led = !led; |
andrewPV | 2:b4f8b72a22ec | 55 | ++i; |
bcostm | 0:bf84ee4ea1d2 | 56 | } |
andrewPV | 2:b4f8b72a22ec | 57 | /* in_value *= 3.3f; |
andrewPV | 2:b4f8b72a22ec | 58 | printf("(out:%.4f V)", in_value); |
andrewPV | 2:b4f8b72a22ec | 59 | ++i; */ |
bcostm | 0:bf84ee4ea1d2 | 60 | } |
bcostm | 0:bf84ee4ea1d2 | 61 | } |
bcostm | 0:bf84ee4ea1d2 | 62 |