#申请原创# 【目的】用RT-Thread完成温湿度数据采集
【APM32F107VCT6 MINI开发板测评】驱动OLED 在这篇文章的基础上增加hs3003温湿度计的驱动,并显示到OLED上。
1、打开menuconfig,配置hs3003温湿度计。
2、配置完后,退出保存并用scons --target=mdk5重新生成工程。
3、打开工程,在main.c中增加ssd1306的初始化,并添加显示温湿度显示函数。代码如下:
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "sensor_renesas_hs300x.h"
#include "ssd1306.h"
/* defined the LED1 pin: PE7 */
#define LED1_PIN GET_PIN(E, 6)
int main(void)
{
uint32_t sysclock = 0;
float humi_f;
float temp_f;
char mqtt_send_buf[128] = {0};
/* set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
/* Print system clock */
sysclock = RCM_ReadSYSCLKFreq();
rt_kprintf("System Clock: %d\n", sysclock);
ssd1306_Init(); //初始化OLED
ssd1306_SetCursor(30, 0); //设置显示位置
ssd1306_WriteString("AMP32", Font_16x26, White); //显示AMP图标
while (1)
{
hs300x_read_data(&temp_humi_dev,&humi_f,&temp_f); //读取温湿度
ssd1306_SetCursor(10, 26);
rt_sprintf(mqtt_send_buf,"temp:%d.%02d",(int)temp_f,(int)(temp_f*100)%100); //生成显示字符串
ssd1306_WriteString(mqtt_send_buf, Font_11x18, Black); //显示温度
ssd1306_SetCursor(10, 26+18);
rt_sprintf(mqtt_send_buf,"humi:%d.%02d",(int)humi_f,(int)(humi_f*100)%100);
ssd1306_WriteString(mqtt_send_buf, Font_11x18, Black);
ssd1306_UpdateScreen(); //更新显示
//rt_kprintf(mqtt_send_buf);
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
4、编译后下载到开发板,显示效果如下:
|