简体   繁体   English

在 LCD 上显示电位器值

[英]Display Potentiometer Value on LCD

I am doing a project using the Mbed OS where I have to use a LCD 1602 to display the value of the potentiometer.我正在使用 Mbed OS 做一个项目,我必须使用 LCD 1602 来显示电位器的值。

I was able to connect and display "Hello World" on the LCD in my previous project but I don't know how create one where it'll read the potentiometer.在我之前的项目中,我能够连接并在 LCD 上显示“Hello World”,但我不知道如何创建一个可以读取电位计的设备。 I've read through the Arm Mbed web site and still don't know how to create the code.我已经阅读了 Arm Mbed web 站点,但仍然不知道如何创建代码。

I'm using a Nucleo board.我正在使用 Nucleo 板。

The source code for my project is as follows.我的项目的源代码如下。

#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()");

        }
    }
}

I assume that you want to read the input voltage that is modified by changing the setting of a potentiometer, a kind of variable resistor that has a knob that by twisting divides the incoming voltage into a different output voltage.我假设您想读取通过更改电位器设置修改的输入电压,电位器是一种可变电阻器,具有一个旋钮,通过扭转将输入电压分成不同的 output 电压。

It looks like you are using the wrong class, AnalogOut , and should instead be using the class AnalogIn .看起来您使用了错误的 class, AnalogOut ,而应该使用 class AnalogIn See https://os.mbed.com/docs/mbed-os/v5.15/apis/analogin.html请参阅https://os.mbed.com/docs/mbed-os/v5.15/apis/analogin.html

Use the AnalogIn API to read an external voltage applied to an analog input pin.使用AnalogIn API 读取施加到模拟输入引脚的外部电压。 AnalogIn() reads the voltage as a fraction of the system voltage. AnalogIn()将电压读取为系统电压的一部分。 The value is a floating point from 0.0(VSS) to 1.0(VCC).该值是从 0.0(VSS) 到 1.0(VCC) 的浮点数。 For example, if you have a 3.3V system and the applied voltage is 1.65V, then AnalogIn() reads 0.5 as the value.例如,如果您有一个 3.3V 的系统并且施加的电压为 1.65V,则AnalogIn()读取 0.5 作为值。

So your program should look something like the following.所以你的程序应该如下所示。 I don't have your facilities so can't test this but it is in accordance with the Mbed documentation.我没有您的设施,因此无法对此进行测试,但它符合 Mbed 文档。 You will need to make sure you have things wired up to the proper analog pin and that you specify the correct analog pin to be reading from.您需要确保将东西连接到正确的模拟引脚,并指定要读取的正确模拟引脚。

#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() is deprecated but it should work. wait()已弃用,但它应该可以工作。 See https://os.mbed.com/docs/mbed-os/v5.15/apis/wait.html请参阅https://os.mbed.com/docs/mbed-os/v5.15/apis/wait.html

See as well Working with Text Displays .另请参阅使用文本显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM