找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8288|回复: 7
收起左侧

DIY_oled显示屏+ds18b20 显示温度和时间 单片机程序

  [复制链接]
ID:298663 发表于 2019-6-11 13:05 | 显示全部楼层 |阅读模式
1.jpg

电路原理图如下:
0.png

单片机源程序如下:
  1. #include "STC15W.H"
  2. #include "iic.h"
  3. //#include "zifu.h"
  4. #include "delay.h"
  5. #include "stdio.h"
  6. #include "intrins.h"
  7. #include "temp.h"

  8. /*******定义时间变量********************/
  9. unsigned int count=0;
  10. unsigned int count1=9;
  11. unsigned int second0;
  12. unsigned int second1;
  13. unsigned int min0;
  14. unsigned int min1;
  15. unsigned int hour0;
  16. unsigned int hour1;
  17. unsigned char s[3];
  18. unsigned char h1[1],h0[1],m1[1],m0[1],s1[1],s0[1];
  19. /****************************************************/


  20. bit flag = 0; //定义秒针闪烁的标志位

  21. /**********************定义温度的参数变量*****************/
  22. code unsigned char float_tab[16]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//小数表
  23. unsigned int  Display_Digit[]={0,0,0,0};
  24. unsigned char wendu=0;
  25. unsigned char aa,bb;
  26. unsigned char CurrentT=0;
  27. unsigned char Temp_Value[]={0x00,0x00};
  28. unsigned char DS18B20_IS_OK;
  29. unsigned char bai[1],shi[1],ge[1];
  30. /********************************************************/
  31. /*  定时器0的配置**************************************/
  32. void Timer0Init()                //定时50ms出发一次中断
  33. {
  34.         AUXR &= 0x7F;               
  35.         TMOD &= 0xF0;               
  36.         TL0 = 0x00;               
  37.         TH0 = 0x4C;               
  38.         //TF0 = 0;               
  39.         TR0 = 1;               
  40.         ET0=1;
  41.         EA=1;
  42. }
  43. void TIMER0()  interrupt 1
  44. {
  45.         count++;  //50毫秒触发一次
  46.         if(count==20) //定时为1秒
  47.         {
  48.                 count=0;second0+=1; flag=~flag;
  49.         }
  50.         if(second0>9)
  51.         {
  52.                 second0=0;second1+=1;
  53.         }
  54.         if(second1>5)
  55.         {
  56.                 second1=0;min0+=1;
  57.         }
  58.         if(min0>9)
  59.         {
  60.                 min0=0;min1+=1;
  61.         }
  62.         if(min1>5)
  63.         {
  64.                 min1=0;hour0+=1;
  65.         }
  66.         if(hour1<2)
  67.         {
  68.                 if(hour0>9)
  69.                 {
  70.                         hour0=0;hour1+=1;
  71.                 }
  72.                
  73.         }
  74.         if(hour1==2)
  75.         {
  76.                 if(hour0>3)
  77.                 {
  78.                         second0=0;second1=0;
  79.                         min0=0;min1=0;
  80.                         hour0=0;hour1=0;
  81.                 }
  82.         }
  83.         
  84.         
  85.         
  86.         
  87.         
  88. }

  89. //读取温度        
  90. void Read_Temperature()
  91. {
  92.                 if(Init_DS18B20()==1)
  93.                 DS18B20_IS_OK=0;
  94.                 else
  95.                 {
  96.                         
  97.                 WriteOneByte(0xcc);                          //跳过序列号
  98.                 WriteOneByte(0x44);                           //启动温度转换
  99.                 Init_DS18B20();
  100.                 WriteOneByte(0xcc);
  101.                 WriteOneByte(0xbe);                                //读取温度
  102.                 Temp_Value[0] = ReadOneByte();           //低八位
  103.                 Temp_Value[1] = ReadOneByte();           //高八位
  104.                 DS18B20_IS_OK=1;        
  105.                         
  106.                         
  107.                 }
  108.         
  109. }



  110. void main()
  111. {
  112.         

  113.         Timer0Init()        ;
  114.         Initial_M096128x64_ssd1306();
  115.         OLED_CLS();
  116.         Delay_ms(5);
  117.         
  118.         
  119.         while(1)
  120.         {
  121.                
  122.                 Read_Temperature();
  123.         if((Temp_Value[1]&0xf8)==0xf8)                        //判断高5位,如果都为1则是负数
  124.         {
  125.                  Temp_Value[1] = ~Temp_Value[1];                  //如果为负数。高八位需要取反,第八位需要取反后加1.
  126.                 Temp_Value[0] = ~Temp_Value[0]+1;
  127.                 if(Temp_Value[0]==0x00)
  128.                         Temp_Value[1]++;
  129.                 wendu = 1;
  130.                         
  131.         }
  132.         else{wendu = 0;}
  133.          aa=Temp_Value[0]&0x0f;           //小数点后一位算法
  134.          bb=aa*0.0625*10;
  135.          CurrentT= ((Temp_Value[0]&0xf0)>>4) | ((Temp_Value[1]&0x07)<<4);         //整数值
  136.          Display_Digit[1] = CurrentT/10; //百位          CurrentT%1000/100;
  137.          Display_Digit[2] = CurrentT%10;          //十位          CurrentT%100/10;
  138.          Display_Digit[3] =  bb%10;                  //个位          CurrentT%10;
  139.         
  140.         
  141.                
  142.                 OLED_P8x16Str(0,0,"PARA_SYSTEM-V0");
  143.                
  144.         
  145.                 OLED_ShowCHinese(0,2,0);   //当
  146.                 OLED_ShowCHinese(16,2,1);  //前
  147.                 OLED_ShowCHinese(32,2,2);  //温
  148.                 OLED_ShowCHinese(48,2,3);  //度
  149.                 OLED_P8x16Str(64,2,":");
  150.                
  151.                 //当温度为负数的时候,显示-号
  152.           if(wendu==1)
  153.                 {
  154.                  OLED_P8x16Str(72,2,"-");
  155.                         
  156.                 }
  157.                 else
  158.                 {
  159.                          OLED_P8x16Str(72,2," ");
  160.                 }
  161.                
  162.                
  163.                 sprintf(bai,"%d",Display_Digit[1]);
  164.     OLED_P8x16Str(80,2,bai);
  165.           sprintf(shi,"%d",Display_Digit[2]);
  166.     OLED_P8x16Str(88,2,shi);
  167.                 OLED_P8x16Str(96,2,".");
  168.                 sprintf(ge,"%d",Display_Digit[3]);
  169.     OLED_P8x16Str(104,2,ge);
  170.                
  171.         
  172.                
  173.                 OLED_ShowCHinese(0,4,4);   //时
  174.                 OLED_ShowCHinese(16,4,5);  //间
  175.                 OLED_P8x16Str(32,4,":");
  176.                
  177.                
  178.                
  179.                
  180.                 sprintf(h1,"%d",hour1);
  181.     OLED_P8x16Str(40,4,h1);
  182.                 sprintf(h0,"%d",hour0);
  183.     OLED_P8x16Str(48,4,h0);
  184.                
  185.                 if(flag==1)
  186.                 {
  187.                         OLED_P8x16Str(56,4,":");
  188.                 }
  189.                 else
  190.                 {
  191.                         OLED_P8x16Str(56,4," ");
  192.                 }
  193.                
  194.                 sprintf(m1,"%d",min1);
  195.     OLED_P8x16Str(64,4,m1);
  196.                 sprintf(m0,"%d",min0);
  197.     OLED_P8x16Str(72,4,m0);
  198.                
  199.                 if(flag==1)
  200.                 {
  201.                         OLED_P8x16Str(80,4,":");
  202.                 }
  203.                 else
  204.                 {
  205.                         OLED_P8x16Str(80,4," ");
  206.                 }
  207.                
  208.                 sprintf(s1,"%d",second1);
  209.     OLED_P8x16Str(88,4,s1);
  210.                 sprintf(s0,"%d",second0);
  211.     OLED_P8x16Str(96,4,s0);
  212.                
  213.                
  214.                
  215.                
  216.                 Delay_ms(10);

  217.                
  218.                
  219.         }
  220.         
  221.         
  222.         
  223. }
复制代码

所有资料51hei提供下载:

Sheet2.rar

4.48 KB, 下载次数: 62, 下载积分: 黑币 -5

layout示意图

48bfad32c2118735a4643e2c4bc11d34.rar

1.81 MB, 下载次数: 41, 下载积分: 黑币 -5

视频展示效果

app_测试.rar

77.09 KB, 下载次数: 165, 下载积分: 黑币 -5

程序

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:648535 发表于 2019-11-28 10:44 | 显示全部楼层
这个函数的作用是什么啊?
回复

使用道具 举报

ID:195752 发表于 2019-12-31 16:31 | 显示全部楼层
学习,正想显示温度
回复

使用道具 举报

ID:605938 发表于 2020-3-16 21:46 | 显示全部楼层
请问12864的驱动程序是怎么写啊,能给我个例成和新手能够看懂的程序吗,谢谢
回复

使用道具 举报

ID:125616 发表于 2020-7-20 23:23 | 显示全部楼层
OLED_P8x16Str  大神请问,这个函数是显示小数的吗
回复

使用道具 举报

ID:401598 发表于 2020-7-22 08:06 | 显示全部楼层
DIY精神万岁!
回复

使用道具 举报

ID:856401 发表于 2021-4-11 22:23 来自手机 | 显示全部楼层
yuanweilong789 发表于 2020-7-20 23:23
OLED_P8x16Str  大神请问,这个函数是显示小数的吗

这个是OLED显示字符串的,它第一行就是显示引号里面的字符串
回复

使用道具 举报

ID:966104 发表于 2021-9-20 08:19 | 显示全部楼层
正想学习彩屏……。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表