[英]Why "lcd_puts" doesn't display the value of a variable at LCD 16*2?
[英]Display Potentiometer Value on LCD
我正在使用 Mbed OS 做一个项目,我必须使用 LCD 1602 来显示电位器的值。
在我之前的项目中,我能够连接并在 LCD 上显示“Hello World”,但我不知道如何创建一个可以读取电位计的设备。 我已经阅读了 Arm Mbed web 站点,但仍然不知道如何创建代码。
我正在使用 Nucleo 板。
我的项目的源代码如下。
#include "mbed.h"
#include "TextLCD.h"
AnalogOut mypot(A0);
TextLCD lcd(D1, D2, D4, D5, D6, D7 ); // rs, rw, e, d4, d5, d6, d7
int main() {
while(1){
for(i=0.0f, i<1.0f , i+=1.0f){
mypot = i
lcd.printf("mypot.read()");
}
}
}
我假设您想读取通过更改电位器设置修改的输入电压,电位器是一种可变电阻器,具有一个旋钮,通过扭转将输入电压分成不同的 output 电压。
看起来您使用了错误的 class, AnalogOut
,而应该使用 class AnalogIn
。 请参阅https://os.mbed.com/docs/mbed-os/v5.15/apis/analogin.html
使用
AnalogIn
API 读取施加到模拟输入引脚的外部电压。AnalogIn()
将电压读取为系统电压的一部分。 该值是从 0.0(VSS) 到 1.0(VCC) 的浮点数。 例如,如果您有一个 3.3V 的系统并且施加的电压为 1.65V,则AnalogIn()
读取 0.5 作为值。
所以你的程序应该如下所示。 我没有您的设施,因此无法对此进行测试,但它符合 Mbed 文档。 您需要确保将东西连接到正确的模拟引脚,并指定要读取的正确模拟引脚。
#include "mbed.h"
#include "TextLCD.h"
AnalogIn mypot(A0);
TextLCD lcd(D1, D2, D4, D5, D6, D7 ); // rs, rw, e, d4, d5, d6, d7
int main() {
while(1) { // begin infinite loop
// read the voltage level from the analog pin. the value is in
// the range of 0.0 to 1.0 and is interpreted as a percentage from 0% to 100%.
float voltage = mypot.read(); // Read input voltage, a float in the range [0.0, 1.0]
lcd.printf("normalized voltage read is %f\n", voltage);
wait(0.5f); // wait a half second then we poll the voltage again.
}
}
wait()
已弃用,但它应该可以工作。 请参阅https://os.mbed.com/docs/mbed-os/v5.15/apis/wait.html
另请参阅使用文本显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.