Nam

Dependencies:   Thuvien_mbed

Committer:
nhancap98
Date:
Mon Jun 10 11:40:34 2019 +0000
Revision:
1:5b18ad9301f3
Parent:
0:fe978a0a248e
SPKT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nhancap98 0:fe978a0a248e 1 #include "mbed.h"
nhancap98 0:fe978a0a248e 2
nhancap98 0:fe978a0a248e 3 AnalogIn in(A0);
nhancap98 0:fe978a0a248e 4 AnalogOut out(PA_4);
nhancap98 0:fe978a0a248e 5 DigitalOut led(LED1);
nhancap98 0:fe978a0a248e 6 int main()
nhancap98 0:fe978a0a248e 7 {
nhancap98 0:fe978a0a248e 8 printf("\n Vong lap Analog\n\r");
nhancap98 0:fe978a0a248e 9 printf("*** ket noi chan A0 va PA_4 voi nhau ***\n\r");
nhancap98 0:fe978a0a248e 10 while(1) {
nhancap98 0:fe978a0a248e 11 for (float out_value = 0.0f; out_value < 1.1f; out_value += 0.1f) {
nhancap98 0:fe978a0a248e 12 // Giá tr? d?u ra s? d?ng DAC
nhancap98 0:fe978a0a248e 13 out.write(out_value);
nhancap98 0:fe978a0a248e 14 wait(0.1);
nhancap98 0:fe978a0a248e 15 // doc ADC input
nhancap98 0:fe978a0a248e 16 float in_value = in.read();
nhancap98 0:fe978a0a248e 17 // DAC - ADC
nhancap98 0:fe978a0a248e 18 float diff = fabs(out_value - in_value);
nhancap98 0:fe978a0a248e 19 printf("(out:%.4f) - (in:%.4f) = (%.4f) \n\r", out_value, in_value, diff);
nhancap98 0:fe978a0a248e 20 if (diff > 0.05f) {
nhancap98 0:fe978a0a248e 21 printf("FAIL\n\r");
nhancap98 0:fe978a0a248e 22 } else {
nhancap98 0:fe978a0a248e 23 printf("\nOK\n\r");
nhancap98 0:fe978a0a248e 24 printf("\033[1A\n\r"); // di chuyen con tro len dong truoc do
nhancap98 0:fe978a0a248e 25 // printf("DAC and ADC:");
nhancap98 0:fe978a0a248e 26 printf("(DAC out:%.4f) - (ADC in:%.4f) = (su khac biet giua hai gia tri: %.4f) \n\n\n\r", out_value, in_value, diff);
nhancap98 0:fe978a0a248e 27 }
nhancap98 0:fe978a0a248e 28 led = !led; //nhap nhay led
nhancap98 0:fe978a0a248e 29 }
nhancap98 0:fe978a0a248e 30 }
nhancap98 0:fe978a0a248e 31 }
nhancap98 0:fe978a0a248e 32