简体   繁体   中英

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.

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. I've read through the Arm Mbed web site and still don't know how to create the code.

I'm using a Nucleo board.

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.

It looks like you are using the wrong class, AnalogOut , and should instead be using the class AnalogIn . See 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() reads the voltage as a fraction of the system voltage. The value is a floating point from 0.0(VSS) to 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.

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. 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. See https://os.mbed.com/docs/mbed-os/v5.15/apis/wait.html

See as well Working with Text Displays .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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