stc15OLED显示温度代码
时间: 2023-10-29 14:32:30 浏览: 161
以下是一个基于STC15单片机和IIC OLED屏幕的示例代码,用于读取DS18B20温度传感器的值并将其显示在OLED屏幕上:
```c
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "ds18b20.h" // DS18B20温度传感器库
#include "OLED_I2C.h" // IIC OLED屏幕库
#define uchar unsigned char
#define uint unsigned int
void delay_ms(uint ms) {
uint i, j;
for (i = ms; i > 0; i--) {
for (j = 110; j > 0; j--);
}
}
void main() {
uchar temp[10];
float ftemp;
OLED_Init(); // 初始化OLED屏幕
while (1) {
ftemp = DS18B20_ReadTemp(); // 读取DS18B20温度传感器的值
sprintf(temp, "%.2f", ftemp); // 将浮点数转换为字符串
OLED_ShowStr(0, 0, "Temperature:"); // 在OLED屏幕上显示文本
OLED_ShowStr(0, 2, temp); // 在OLED屏幕上显示温度值
delay_ms(2000); // 延迟2秒钟以便下一次读取
OLED_Clear(); // 清除OLED屏幕
}
}
```
在这个示例中,我们使用DS18B20温度传感器来读取温度值,并将其显示在IIC OLED屏幕上。代码中使用了DS18B20温度传感器库和IIC OLED屏幕库,您需要将这些库文件添加到您的工程中。在主函数中,我们初始化OLED屏幕,并在循环中读取温度传感器的值,将其转换为字符串,并在OLED屏幕上显示。在每次循环结束后,我们清除OLED屏幕以便下一次显示。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)