模拟量输入-电位器

Dependencies:   mbed

Committer:
anywill
Date:
Wed Oct 19 13:49:54 2016 +0000
Revision:
0:1082d8e6442f
?????potentiometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
anywill 0:1082d8e6442f 1 //平台nucleo+mbed
anywill 0:1082d8e6442f 2 //实验内容:GPIO读取电位器电压
anywill 0:1082d8e6442f 3 //A0接电位器输出端 一端接3.3v 另一端接GND
anywill 0:1082d8e6442f 4 //注:由于电阻发热实际输入最大显示不了3300
anywill 0:1082d8e6442f 5 #include "mbed.h"
anywill 0:1082d8e6442f 6
anywill 0:1082d8e6442f 7 AnalogIn analog_value(A0);//读取模拟值
anywill 0:1082d8e6442f 8
anywill 0:1082d8e6442f 9 DigitalOut led(LED1); //D13 板载LED
anywill 0:1082d8e6442f 10
anywill 0:1082d8e6442f 11 int main() {
anywill 0:1082d8e6442f 12 float meas;
anywill 0:1082d8e6442f 13
anywill 0:1082d8e6442f 14 printf("\nAnalogIn example\n");
anywill 0:1082d8e6442f 15
anywill 0:1082d8e6442f 16 while(1) {
anywill 0:1082d8e6442f 17 meas = analog_value.read(); // 转换并读取输入的模拟量 (value from 0.0 to 1.0)
anywill 0:1082d8e6442f 18 meas = meas * 3300; // 将模拟量映射到 0 to 3300mv
anywill 0:1082d8e6442f 19 printf("measure = %.0f mV\n", meas);
anywill 0:1082d8e6442f 20 if (meas > 2000) { // 若输入电压大于2V则点亮LED
anywill 0:1082d8e6442f 21 led = 1;
anywill 0:1082d8e6442f 22 }
anywill 0:1082d8e6442f 23 else {
anywill 0:1082d8e6442f 24 led = 0;
anywill 0:1082d8e6442f 25 }
anywill 0:1082d8e6442f 26 wait(1); // 1s
anywill 0:1082d8e6442f 27 }
anywill 0:1082d8e6442f 28 }