【项目介绍】
使用RTT的i2c驱动OLED屏以及SHT30温湿度计,实现一个温湿度计。
【移植步骤】
1、打开移植工程NXP-MCXA153开发之三硬件i2c驱动ssd1306-电子产品世界论坛 (eepw.com.cn)
2、打开env图形配置工具,添加相sht3x的驱动。
设置路径为:RT-Thread online packages → peripheral libraries and drivers → sensors drivers
保存后重新使用以pkg --update下载在线的驱动库到工程。
再使用scons --target=mdk5重新生成工程。
3、把sht30的SDA与SCL与P3_28、P3_27接上。
4、添加代码如下:
#include <rtdevice.h> #include "drv_pin.h" #include "sht3x.h" #include "ssd1306.h" #define LED_PIN ((3*32)+12) static sht3x_device_t sht3x_dev = RT_NULL; //声明sht3x rt_uint8_t sht_addr = SHT3X_ADDR_PD ; //定义从机地址 int main(void) { char show_char[20]; #if defined(__CC_ARM) rt_kprintf("using armcc, version: %d\n", __ARMCC_VERSION); #elif defined(__clang__) rt_kprintf("using armclang, version: %d\n", __ARMCC_VERSION); #elif defined(__ICCARM__) rt_kprintf("using iccarm, version: %d\n", __VER__); #elif defined(__GNUC__) rt_kprintf("using gcc, version: %d.%d\n", __GNUC__, __GNUC_MINOR__); #endif rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); /* Set GPIO as Output */ rt_kprintf("MCXA153 HelloWorld\r\n"); sht3x_dev = sht3x_init("i2c0",sht_addr); //初始化sht3x if(NULL == sht3x_dev) { rt_kprintf("init sht3x failed\r\n"); } else { rt_kprintf("init sht3x successed!\r\n"); } ssd1306_Init(); while (1) { ssd1306_Fill(Black); ssd1306_SetCursor(4, 0); ssd1306_WriteString("MCXA153Demo", Font_11x18, White); if (sht3x_dev) { /* read the sensor data */ sht3x_read_singleshot(sht3x_dev); rt_sprintf(show_char,"Hum:%d.%d",(int)sht3x_dev->humidity, (int)(sht3x_dev->humidity * 10) % 10); ssd1306_SetCursor(12, 20); ssd1306_WriteString(show_char, Font_11x18, White); rt_sprintf(show_char,"Temp:%d.%d",(int)sht3x_dev->temperature, (int)(sht3x_dev->temperature * 10) % 10); ssd1306_SetCursor(2, 40); ssd1306_WriteString(show_char, Font_11x18, White); } ssd1306_UpdateScreen(); rt_pin_write(LED_PIN, PIN_HIGH); /* Set GPIO output 1 */ rt_thread_mdelay(500); /* Delay 500mS */ rt_pin_write(LED_PIN, PIN_LOW); /* Set GPIO output 0 */ rt_thread_mdelay(500); /* Delay 500mS */ } }
代码主要的思路为初始化sht3x、ssd1306,然后读取温湿度值,通过ssd1306显示到OLED屏中。
【项目效果】