聚丰项目 > AB32+OLED+DS18B20温度实时显示
使用DS18B20监测温度
jf_52293744
分享jf_52293744
团队成员
盛建成 学生
使用iic的OLED,单总线的DS18B20温度传感器来实时监测温度。
在RTT Studio的软件库下载OLED和DS18B20的包,把domo中的任务创建和入口函数,可以直接拿来使用;
代码上传了gitee:https://gitee.com/sheng_jiancheng/sjc_-prj.git
因为是基本上第一次接触单片机操作系统,所以很fw,下面是主要的几个任务块,我是只有一个18b20的那个黑头头,没有上拉电阻,开始初始化都过不了,直接找不到18B20,后面把驱动ds18b20_connect(rt_base_t pin)函数里面的pin mode改成Pullup可以成功检测到了,但是温度读回来一直都是0,我是废物,没找到原因,后面一定好好查查,如果有大佬看见希望可以指点一哈。我也很懵逼,按道理什么的初始化都成功了,不应该这样。
//线程入口函数: static void read_temp_entry(void *parameter) { rt_device_t dev = RT_NULL; struct rt_sensor_data sensor_data; rt_size_t res; dev = rt_device_find(parameter); if (dev == RT_NULL) { rt_kprintf("Can't find device:%s\n", parameter); return; } if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) { rt_kprintf("open device failed!\n"); return; } rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100); while (1) { res = rt_device_read(dev, 0, &sensor_data, 1); if (res != 1) { rt_kprintf("read data failed!size is %d\n", res); rt_device_close(dev); return; } else { if (sensor_data.data.temp >= 0) { temp = (sensor_data.data.temp & 0xffff) >> 5; // get temp rt_kprintf("temp:%3d.%dC, timestamp:%5d\n", sensor_data.data.temp / 10, sensor_data.data.temp % 10, sensor_data.timestamp); } else { rt_kprintf("temp:-%2d.%dC, timestamp:%5d\n", abs(sensor_data.data.temp / 10), abs(sensor_data.data.temp % 10), sensor_data.timestamp); } } rt_thread_mdelay(100); } } //线程创建函数: static int ds18b20_read_temp_sample(void) { rt_thread_t ds18b20_thread; ds18b20_thread = rt_thread_create("18b20tem", read_temp_entry, "temp_ds18b20", 1024, RT_THREAD_PRIORITY_MAX / 2, 20); if (ds18b20_thread != RT_NULL) { rt_thread_startup(ds18b20_thread); } return RT_EOK; } INIT_APP_EXPORT(ds18b20_read_temp_sample); //DS18B20的初始化 static int rt_hw_ds18b20_port(void) { struct rt_sensor_config cfg; cfg.intf.user_data = (void *)DS18B20_DATA_PIN; rt_hw_ds18b20_init("ds18b20", &cfg); return RT_EOK; } INIT_COMPONENT_EXPORT(rt_hw_ds18b20_port); static void oled_display_entry(void *parameter) { char text[200]; while(1) { rt_memset(text, 0, sizeof(text)); rt_sprintf(text, "%d", temp); ssd1306_SetCursor(60, 6);//添加代码,设置显示光标位置 ssd1306_WriteString(text, Font_11x18, White);//添加代码,设置显示内容 // ssd1306_UpdateScreen();添加代码,更新显示屏信息 rt_memset(text, 0, sizeof(text)); rt_sprintf(text, "%d", humi); ssd1306_SetCursor(60, 40);//添加代码,设置显示光标位置 ssd1306_WriteString(text, Font_11x18, White);//添加代码,设置显示内容 ssd1306_UpdateScreen();//添加代码,更新显示屏信息 rt_thread_mdelay(100); } } void oled_display_thread_create() { rt_thread_t oled_display_thread; oled_display_thread = rt_thread_create("oled_display", oled_display_entry, RT_NULL, 1024, 20, 40); if (oled_display_thread != RT_NULL) { rt_thread_startup(oled_display_thread); } }
视频演示:
(0.94 MB)下载