找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2603|回复: 0
打印 上一主题 下一主题
收起左侧

STC89C52单片机的DS18B20 OLED显示源代码

[复制链接]
跳转到指定楼层
楼主
ID:389321 发表于 2019-7-28 22:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于STC89C52的DS18B20源代码
单总线通信,温度测量精准,并在OLED上实时显示出来。

单片机源程序如下:
  1. #include "reg52.h"
  2. #include "ds18b20.h"
  3. #include "typedef.h"
  4. #include "oled.h"
  5. #include "delay.h"
  6. #include "intrins.h"
  7. #include "key.h"

  8. void main()
  9. {
  10. //        float temp;
  11. //        int t;
  12.         u8 c;
  13.         OLED_Init();
  14.         OLED_Clear();
  15.         
  16.         while(1)
  17.         {
  18. //                OLED_Clear();
  19.           /*temp = 10*Get_Tmp();
  20.                 t = ((int)temp)%10;
  21.                 OLED_ShowNum(0,0,temp/10,2,16);
  22.                 OLED_ShowString(8*2+1,0,".");
  23.                 OLED_ShowNum(8*3+1,0,t,1,16);
  24.                 OLED_ShowString(8*4+1,0,"'C");*/
  25.                 c = Key_scan();
  26.                 OLED_ShowChar(0,0,c);
  27.         }
  28. }
复制代码
  1. //  功能描述   : OLED 4接口演示例程(51系列)
  2. //              说明:
  3. //              ----------------------------------------------------------------
  4. //              GND    电源地
  5. //              VCC  接5V或3.3v电源
  6. //              D0   P2^0(SCL)
  7. //              D1   P2^1(SDA)
  8. //              RES  接P22
  9. //              DC   接P23
  10. //              CS   接P24               
  11. //              ----------------------------------------------------------------
  12. //******************************************************************************/。
  13. #include "oled.h"
  14. //#include "stdlib.h"
  15. #include "oledfont.h"           
  16. #include "delay.h"
  17. //OLED的显存
  18. //存放格式如下.
  19. //[0]0 1 2 3 ... 127        
  20. //[1]0 1 2 3 ... 127        
  21. //[2]0 1 2 3 ... 127        
  22. //[3]0 1 2 3 ... 127        
  23. //[4]0 1 2 3 ... 127        
  24. //[5]0 1 2 3 ... 127        
  25. //[6]0 1 2 3 ... 127        
  26. //[7]0 1 2 3 ... 127                           
  27. //void delay_ms(unsigned int ms)
  28. //{                        
  29. //        unsigned int a;
  30. //        while(ms)
  31. //        {
  32. //                a=1800;
  33. //                while(a--);
  34. //                ms--;
  35. //        }
  36. //        return;
  37. //}
  38. #if OLED_MODE==1
  39. //向SSD1106写入一个字节。
  40. //dat:要写入的数据/命令
  41. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  42. void OLED_WR_Byte(u8 dat,u8 cmd)
  43. {
  44.         DATAOUT(dat);            
  45.         if(cmd)
  46.           OLED_DC_Set();
  47.         else
  48.           OLED_DC_Clr();                  
  49.         OLED_CS_Clr();
  50.         OLED_WR_Clr();         
  51.         OLED_WR_Set();
  52.         OLED_CS_Set();         
  53.         OLED_DC_Set();         
  54. }                        
  55. #else
  56. //向SSD1306写入一个字节。
  57. //dat:要写入的数据/命令
  58. //cmd:数据/命令标志 0,表示命令;1,表示数据;
  59. void OLED_WR_Byte(u8 dat,u8 cmd)
  60. {        
  61.         u8 i;                          
  62.         if(cmd)
  63.           OLED_DC_Set();
  64.         else
  65.           OLED_DC_Clr();                  
  66.         OLED_CS_Clr();
  67.         for(i=0;i<8;i++)
  68.         {                          
  69.                 OLED_SCLK_Clr();
  70.                 if(dat&0x80)
  71.                         {
  72.                    OLED_SDIN_Set();
  73.                         }
  74. else
  75.                    OLED_SDIN_Clr();
  76.                                 OLED_SCLK_Set();
  77.                 dat<<=1;   
  78.         }                                                   
  79.         OLED_CS_Set();
  80.         OLED_DC_Set();            
  81. }
  82. #endif
  83.         void OLED_Set_Pos(unsigned char x, unsigned char y)
  84. {
  85.         OLED_WR_Byte(0xb0+y,OLED_CMD);
  86.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  87.         OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
  88. }            
  89. //开启OLED显示   
  90. void OLED_Display_On(void)
  91. {
  92.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  93.         OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  94.         OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  95. }
  96. //关闭OLED显示     
  97. void OLED_Display_Off(void)
  98. {
  99.         OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  100.         OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  101.         OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  102. }                                            
  103. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!         
  104. void OLED_Clear(void)  
  105. {  
  106.         u8 i,n;                    
  107.         for(i=0;i<8;i++)  
  108.         {  
  109.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  110.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  111.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  112.                 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
  113.         } //更新显示
  114. }


  115. //在指定位置显示一个字符,包括部分字符
  116. //x:0~127
  117. //y:0~63
  118. //mode:0,反白显示;1,正常显示                                 
  119. //size:选择字体 16/12
  120. void OLED_ShowChar(u8 x,u8 y,u8 chr)
  121. {              
  122.         unsigned char c=0,i=0;        
  123.                 c=chr-' ';//得到偏移后的值                        
  124.                 if(x>Max_Column-1){x=0;y=y+2;}
  125.                 if(SIZE ==16)
  126.                         {
  127.                         OLED_Set_Pos(x,y);        
  128.                         for(i=0;i<8;i++)
  129.                         OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  130.                         OLED_Set_Pos(x,y+1);
  131.                         for(i=0;i<8;i++)
  132.                         OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  133.                         }
  134.                         else {        
  135.                                 OLED_Set_Pos(x,y+1);
  136.                                 for(i=0;i<6;i++)
  137.                                 OLED_WR_Byte(F6x8[c][i],OLED_DATA);
  138.                                 
  139.                         }
  140. }
  141. //m^n函数
  142. u32 oled_pow(u8 m,u8 n)
  143. {
  144.         u32 result=1;         
  145.         while(n--)result*=m;   
  146.         return result;
  147. }                                 
  148. //显示2个数字
  149. //x,y :起点坐标         
  150. //len :数字的位数
  151. //size:字体大小
  152. //mode:模式        0,填充模式;1,叠加模式
  153. //num:数值(0~4294967295);                           
  154. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
  155. {                 
  156.         u8 t,temp;
  157.         u8 enshow=0;                                                   
  158.         for(t=0;t<len;t++)
  159.         {
  160.                 temp=(num/oled_pow(10,len-t-1))%10;
  161.                 if(enshow==0&&t<(len-1))
  162.                 {
  163.                         if(temp==0)
  164.                         {
  165.                                 OLED_ShowChar(x+(size2/2)*t,y,' ');
  166.                                 continue;
  167.                         }else enshow=1;
  168.                           
  169.                 }
  170.                  OLED_ShowChar(x+(size2/2)*t,y,temp+'0');
  171.         }
  172. }
  173. //显示一个字符号串
  174. void OLED_ShowString(u8 x,u8 y,u8 *chr)
  175. {
  176.         unsigned char j=0;
  177.         while (chr[j]!='\0')
  178.         {                OLED_ShowChar(x,y,chr[j]);
  179.                         x+=8;
  180.                 if(x>120){x=0;y+=2;}
  181.                         j++;
  182.         }
  183. }
  184. //显示汉字
  185. //void OLED_ShowCHinese(u8 x,u8 y,u8 no)
  186. //{                                 
  187. //        u8 t,adder=0;
  188. //        OLED_Set_Pos(x,y);        
  189. //    for(t=0;t<16;t++)
  190. //                {
  191. //                                OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  192. //                                adder+=1;
  193. //     }        
  194. //                OLED_Set_Pos(x,y+1);        
  195. //    for(t=0;t<16;t++)
  196. //                        {        
  197. //                                OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  198. //                                adder+=1;
  199. //      }                                       
  200. //}
  201. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  202. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  203. {         
  204. unsigned int j=0;
  205. unsigned char x,y;
  206.   
  207.   if(y1%8==0) y=y1/8;      
  208.   else y=y1/8+1;
  209.         for(y=y0;y<y1;y++)
  210.         {
  211.                 OLED_Set_Pos(x0,y);
  212.     for(x=x0;x<x1;x++)
  213.             {      
  214.                     OLED_WR_Byte(BMP[j++],OLED_DATA);                    
  215.             }
  216.         }
  217. }


  218. //初始化SSD1306                                            
  219. void OLED_Init(void)
  220. {
  221.   OLED_RST_Set();
  222.         delay_ms(100);
  223.         OLED_RST_Clr();
  224.         delay_ms(100);
  225.         OLED_RST_Set();
  226.         /*                                 
  227.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  228.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  229.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  230.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  231.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  232.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  233.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  234.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  235.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  236.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  237.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  238.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  239.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  240.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  241.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  242.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  243.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  244.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  245.         OLED_WR_Byte(0x12,OLED_CMD);
  246.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  247.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  248.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  249.         OLED_WR_Byte(0x02,OLED_CMD);//
  250.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  251.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  252.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  253.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  254.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  255.         */

  256.         OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  257.         OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  258.         OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  259.         OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  260.         OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  261.         OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  262.         OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  263.         OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  264.         OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  265.         OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  266.         OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  267.         OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  268.         OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  269.         OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  270.         OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  271.         OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  272.         OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  273.         OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  274.         OLED_WR_Byte(0x12,OLED_CMD);
  275.         OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  276.         OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  277.         OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  278.         OLED_WR_Byte(0x02,OLED_CMD);//
  279.         OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  280.         OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  281.         OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  282.         OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  283.         OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  284.         
  285.         OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  286.         OLED_Clear();
  287.         OLED_Set_Pos(0,0);         
  288. }  

复制代码


所有资料51hei提供下载:
DS18B20.zip (66.63 KB, 下载次数: 99)



评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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