0
我試圖在Adafruit ST7565 GLCD上顯示電位計的值。我的串行監視器給我的值在1.62-1.67之間,而GLCD的範圍從-20,000到+20,000。我不確定算術/數據類型是否錯誤,或者我是否爲「sprintf」轉換不正確地分配內存。將電位計值轉換爲GLCD顯示屏的字符串
#include "ST7565.h"
#include "stdlib.h"
char buffer[5];
int ledPin = 13; // LED connected to digital pin 13
char str[8];
// the LCD backlight is connected up to a pin so you can turn it on & off
#define BACKLIGHT_LED 10
// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
// pin 7 - Data/Command select (RS or A0)
// pin 6 - LCD reset (RST)
// pin 5 - LCD chip select (CS)
ST7565 glcd(9, 8, 7, 6, 5);
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
void setup() {
Serial.begin(9600);
// turn on backlight
pinMode(BACKLIGHT_LED, OUTPUT);
digitalWrite(BACKLIGHT_LED, HIGH);
// initialize and set the contrast to 0x18
glcd.begin(0x18);
glcd.display(); // show splashscreen
delay(3000);
glcd.clear();
Serial.println(" ");
digitalWrite(BACKLIGHT_LED, HIGH);
glcd.drawstring(0,0," ");
glcd.display();
glcd.clear();
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0/1023.0);
// print out the value you read:
Serial.println(voltage);
digitalWrite(BACKLIGHT_LED, HIGH);
Serial.println(voltage);
sprintf(str,"%d",voltage); // converts to decimal base.
glcd.drawstring(0,0,str);
glcd.display();
delay(500);
glcd.clear();
}
任何洞察力的讚賞。我沒有太多正式的編程經驗,因此鏈接關於數據類型的教程將不會有任何用處。我需要看到一個像這樣的具體例子,才能真正理解。
'sprintf(str,「%d」,voltage);'錯了,'voltage'不是'int'。你在尋找'snprintf(str,sizeof str,「%d」,電壓);'而不是? (爲了安全起見,請注意使用'snprintf()'。) – 2013-08-02 22:56:03