基于STM32的OLED屏显⽰AHT20采集的温湿度数据
⽂章⽬录
本实验使⽤的⼯具:
STM32野⽕mini开发板、AHT20温湿度传感器、0.96⼨OLED显⽰屏
⼀、实现温湿度数据采集并通过串⼝显⽰
⽬的:为了查看能否实现AHT20传感器的温湿度采集
具体操作过程请参考⼩编的另⼀篇博客:
⼆、实现将温湿度采集数据显⽰到OLED屏
本过程是基于上⾯实现串⼝显⽰的⼯程上,进⾏OLED屏数据显⽰代码的添加。
本实验完整⼯程代码下载详见参考链接。
1.代码下载
2.部分代码的编写
2.1.显⽰温湿度的代码
voidread_AHT20(void)
{
uint8_ti;
for(i=0;i<6;i++)
{
readByte[i]=0;
}
I2C_Start();
I2C_WriteByte(0x71);
ack_status=Receive_ACK();
readByte[0]=I2C_ReadByte();
Send_ACK();
readByte[1]=I2C_ReadByte();
Send_ACK();
readByte[2]=I2C_ReadByte();
Send_ACK();
readByte[3]=I2C_ReadByte();
Send_ACK();
readByte[4]=I2C_ReadByte();
Send_ACK();
readByte[5]=I2C_ReadByte();
SendNot_Ack();
//Send_ACK();
I2C_Stop();
if((readByte[0]&0x68)==0x08)
{
H1=readByte[1];
H1=(H1<<8)|readByte[2];
H1=(H1<<8)|readByte[3];
H1=H1>>4;
H1=(H1*1000)/1024/1024;
T1=readByte[3];
T1=T1&0x0000000F;
T1=(T1<<8)|readByte[4];
T1=(T1<<8)|readByte[5];
T1=(T1*2000)/1024/1024-500;
AHT20_OutData[0]=(H1>>8)&0x000000FF;
AHT20_OutData[1]=H1&0x000000FF;
AHT20_OutData[2]=(T1>>8)&0x000000FF;
AHT20_OutData[3]=T1&0x000000FF;
}
el
{
AHT20_OutData[0]=0xFF;
AHT20_OutData[1]=0xFF;
AHT20_OutData[2]=0xFF;
AHT20_OutData[3]=0xFF;
printf("lyy");
}
printf("rn");
printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
printf("rn");
t=T1/10;
t1=T1%10;
a=(float)(t+t1*0.1);
h=H1/10;
h1=H1%10;
b=(float)(h+h1*0.1);
sprintf(strTemp,"%.1f",a);//调⽤Sprintf函数把DHT11的温度数据格式化到字符串数组变量strTemp中
sprintf(strHumi,"%.1f",b);//调⽤Sprintf函数把DHT11的湿度数据格式化到字符串数组变量strHumi中
//printf(strTemp);
//printf("/r/n");
GUI_ShowCHine(16,00,16,"温湿度显⽰",1);
GUI_ShowCHine(16,20,16,"温度",1);
GUI_ShowString(53,20,strTemp,16,1);
GUI_ShowCHine(16,38,16,"湿度",1);
GUI_ShowString(53,38,strHumi,16,1);
delay_ms(1500);
delay_ms(1500);
delay_ms(1500);
delay_ms(1500);
}
"温",0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,/*"温",10*/
"湿",0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,/*"湿",12*/
"度",0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,/*"度",11*/
"显",0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x04,0x40,0x44,0x44,0x24,0x44,0x14,0x48,0x14,0x50,0x04,0x40,0xFF,0xFE,0x00,0x00,/*"显",13*/
"⽰",0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x01,0x00,
0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x41,0x02,0x81,0x02,0x05,0x00,0x02,0x00,/*"⽰",14*/
函数
intmain(void)
{
delay_init();//延时函数初始化
uart_init(115200);
IIC_Init();
NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
OLED_Init();//初始化OLED
OLED_Clear(0);
while(1)
{
read_AHT20_once();
OLED_Clear(0);
delay_ms(1500);
}
}
3.编译并烧录
点击左上⾓的编译按钮进⾏编译,⽣成相应的.hex⽂件
打开mcuisp软件进⾏.hex⽂件烧录
4.运⾏结果
由于还没有接上AHT20传感器,故温湿度显⽰为0,另外,采集的速度可以⾃⾏进⾏设置,我这⾥设置的采集间隔⽐较长。
三、⼩结
本次实验在实现了温湿度数据采集并通过串⼝显⽰和利⽤OLED屏数据显⽰代码的基础上,完成OLED屏显⽰AHT20采集的温湿度数据也并
不复杂,其中,温湿度数据采集的过程使⽤的是模拟I2C,OLED屏的数据显⽰采⽤的是SPI。
四、参考链接
本文发布于:2022-12-28 04:45:17,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/44702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |