[英]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.