8H8K64U采集DS18B20温度传感器数据并IIC通信OLED显示,实物图如下
效果展示
主函数代码
- /*----------------------------------------------------------------*/
- #include <STC8H.h>
- #include <intrins.h>
- #include "configurations.h"
- #include "delay.h"
- #include "DS18B20.h"
- #include "oled.h" //OLED引脚定义、控制函数
-
- //#include "bmp.h" //图片字模存放位置
- //#include "logo.h" // picture position
-
- /*-------------- GPIO initialize ------------------*/
- void GPIO_Init();
- void Display();
-
- void main()
- {
- GPIO_Init();
- OLED_Init(); //初始化OLED
- OLED_ColorTurn(0); //0正常显示,1 反色显示
- OLED_DisplayTurn(0); //0正常显示 1 屏幕翻转显示
-
- while(1)
- {
- OLED_ShowString(0,0,"Temp:",16);
- Display(); //DS18B20 数据采集
-
- OLED_ShowString(0,4,"ECNU",16); //显示字符串
- OLED_ShowString(40,4,"2024/08/14",16);//显示字符串
- OLED_ShowString(0,6,"Shanghai,China",16); //显示字符串
-
- delay_ms(500); //延时
- OLED_Clear(); //清屏
- }
-
- }
-
- void GPIO_Init()
- {
- P0M1 = 0x00; P0M0 = 0x00;
- P1M1 = 0x00; P1M0 = 0x00;
- P2M1 = 0x00; P2M0 = 0x00;
- P3M1 = 0x00; P3M0 = 0x00;
- P4M1 = 0x00; P4M0 = 0x00;
- P5M1 = 0x00; P5M0 = 0x00;
- P6M1 = 0x00; P6M0 = 0x00;
- P7M1 = 0x00; P7M0 = 0x00;
- }
-
- /*----------- Display temperature ----------------*/
- void Display()
- {
- u8 ms = 3; // number of Delay_ms
- u16 val;
- u16 shi,ge,xiaoshu;
- val = Readtemp();
- shi = val/100;
- ge = val/10%10;
- xiaoshu = val%10;
- if( MinusFlag==1 ) // negtive
- {
- OLED_ShowString(43,0,"-",16);
- OLED_ShowNum(60,0,(u16)(shi),1,16);
- OLED_ShowNum(70,0,(u16)(ge),1,16);
- OLED_ShowString(80,0,".",16);
- OLED_ShowNum(90,0,(u16)(xiaoshu),1,16);
- OLED_ShowString(103,0,"C",16);
- }
- else // MinusFlag==0, positive
- {
- OLED_ShowString(43,0,"+",16);
- OLED_ShowNum(60,0,(u16)(shi),1,16);
- OLED_ShowNum(70,0,(u16)(ge),1,16);
- OLED_ShowString(80,0,".",16);
- OLED_ShowNum(90,0,(u16)(xiaoshu),1,16);
- OLED_ShowString(103,0,"C",16);
- }
- }
复制代码
完整项目工程见附件。
|