这是一段Arduino利用oled来显示DHT11检测出来的温湿度的代码。
代码是课堂实验用的,不是自己编的,发到这里是给可能需要的人。
不好勿喷。使用中如果出现库的问题,可能是有些库没有加上,出现这个问题
可以对照代码,把没有的库加上。
// oled中文显示dht11测量值
#include <dht11.h> // DHT11数字传感器库
dht11 DHT11;
#define DHT11PIN 2 // Data链接数字针脚2
#include "U8glib.h" // OLED库
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C
#define word1W 16 //定义第一个汉字的宽度
#define word1H 16 //定义第一个汉字的高度
#define word2W 16 //定义第二个汉字的宽度
#define word2H 16 //定义第二个汉字的高度
#define word3W 16 //定义第三个汉字的宽度
#define word3H 16 //定义第三个汉字的高度
#define word4W 16 //定义第四个汉字的宽度
#define word4H 16 //定义第四个汉字的高度
#define word5W 16 //定义第五个汉字的宽度
#define word5H 16 //定义第五个汉字的高度
//第一个汉字的编码“温”
static unsigned char word1[] U8G_PROGMEM = {
0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00};
//第二个汉字的编码“照”
static unsigned char word2[] U8G_PROGMEM = {
0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00};
//第三个汉字的编码“度”
static unsigned char word3[] U8G_PROGMEM = {
0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70};
//第四个汉字的编码“监”
static unsigned char word4[] U8G_PROGMEM = {
0x20,0x02,0x24,0x02,0x24,0x3E,0x24,0x02,0x24,0x09,0x24,0x11,0xA4,0x10,0x20,0x00,
0x00,0x00,0xFC,0x1F,0x24,0x12,0x24,0x12,0x24,0x12,0x24,0x12,0xFF,0x7F,0x00,0x00};
//第五个汉字的编码“测”
static unsigned char word5[] U8G_PROGMEM = {
0x00,0x20,0xE4,0x23,0x28,0x22,0x28,0x2A,0xA1,0x2A,0xA2,0x2A,0xA2,0x2A,0xA8,0x2A,
0xA8,0x2A,0xA4,0x2A,0xA7,0x2A,0x84,0x20,0x44,0x21,0x44,0x22,0x24,0x28,0x10,0x10};
//绘画第一个汉字的子程序
void word1draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawXBMP( 10, 0,word1W,word1H, word1);//x坐标10,y坐标0,宽度,高度,编码word1
}
//绘画第二个汉字的子程序
void word2draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawXBMP( 28, 0,word2W,word2H, word2);//x坐标28,y坐标0,宽度,高度,编码word2
}
//绘画第三个汉字的子程序
void word3draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawXBMP( 46, 0,word3W,word3H, word3);//x坐标46,y坐标0,宽度,高度,编码word3
}
//绘画第四个汉字的子程序
void word4draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawXBMP( 62, 0,word4W,word4H, word4);//x坐标62,y坐标0,宽度,高度,编码word4
}
//绘画第五个汉字的子程序
void word5draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawXBMP(80, 0,word5W,word5H, word5);//x坐标80,y坐标0,宽度,高度,编码word5
}
void draw(void)
{
u8g.setFont(u8g_font_fub14); //设置字体和自号,目前测试字号有fub14,17,20,30,49n
u8g.setPrintPos(0, 40); // 对应 x,y 轴值
u8g.print("H(%) :"); //打印%
u8g.setPrintPos(80, 40); // 对应 x,y 轴值
u8g.print((float)DHT11.humidity);//对DHT11的值打印
u8g.setPrintPos(0, 60); // 对应 x,y 轴值
u8g.print("T(oC):"); //打印oC
u8g.setPrintPos(80, 60); // 对应 x,y 轴值
u8g.print((float)DHT11.temperature); //对DHT11的值打印
}
void setup()
{
Serial.begin(9600);//串口波特率
Serial.println("DHT11 TEST PROGRAM "); //打印DHT11的值
}
void loop()
{
Serial.println("\n"); //打印换行
//int chk = DHT11.read(DHT11PIN);
Serial.print("Humidity (%): "); //打印Humidity (%):
Serial.println((float)DHT11.humidity, 2); //对DHT11的值打印
Serial.print("Temperature (oC): ");//打印Temperature (oC):
Serial.println((float)DHT11.temperature, 2); //对DHT11的值打印
u8g.firstPage();
do {
word1draw();//调用绘画第一个汉字的子程序显示汉字
word2draw();//调用绘画第二个汉字的子程序显示汉字
word3draw();//调用绘画第三个汉字的子程序显示汉字
word4draw();//调用绘画第四个汉字的子程序显示汉字
word5draw();//调用绘画第五个汉字的子程序显示汉字
draw();
}
while( u8g.nextPage() );//循环u8g.nextPage()
delay(200);//延时200s
}