聚豐項(xiàng)目 > AB32+OLED+DS18B20溫度實(shí)時(shí)顯示
使用DS18B20監(jiān)測溫度
jf_52293744
分享jf_52293744
團(tuán)隊(duì)成員
盛建成 學(xué)生
使用iic的OLED,單總線的DS18B20溫度傳感器來實(shí)時(shí)監(jiān)測溫度。
在RTT Studio的軟件庫下載OLED和DS18B20的包,把domo中的任務(wù)創(chuàng)建和入口函數(shù),可以直接拿來使用;
代碼上傳了gitee:https://gitee.com/sheng_jiancheng/sjc_-prj.git
因?yàn)槭腔旧系谝淮谓佑|單片機(jī)操作系統(tǒng),所以很fw,下面是主要的幾個(gè)任務(wù)塊,我是只有一個(gè)18b20的那個(gè)黑頭頭,沒有上拉電阻,開始初始化都過不了,直接找不到18B20,后面把驅(qū)動(dòng)ds18b20_connect(rt_base_t pin)函數(shù)里面的pin mode改成Pullup可以成功檢測到了,但是溫度讀回來一直都是0,我是廢物,沒找到原因,后面一定好好查查,如果有大佬看見希望可以指點(diǎn)一哈。我也很懵逼,按道理什么的初始化都成功了,不應(yīng)該這樣。
//線程入口函數(shù): 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); } } //線程創(chuàng)建函數(shù): 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);//添加代碼,設(shè)置顯示光標(biāo)位置 ssd1306_WriteString(text, Font_11x18, White);//添加代碼,設(shè)置顯示內(nèi)容 // ssd1306_UpdateScreen();添加代碼,更新顯示屏信息 rt_memset(text, 0, sizeof(text)); rt_sprintf(text, "%d", humi); ssd1306_SetCursor(60, 40);//添加代碼,設(shè)置顯示光標(biāo)位置 ssd1306_WriteString(text, Font_11x18, White);//添加代碼,設(shè)置顯示內(nèi)容 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)下載