找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机ds18b20测温oled显示程序

[复制链接]
跳转到指定楼层
楼主
D0 P0.0
D1 P0.1
RES P0.2
DC  P0.3
CS  P0.4

DS18B20  P0.7


单片机源程序如下:
  1. #include<reg52.h>
  2.                                  
  3. #define  u8 unsigned char
  4. #define  u32 unsigned int
  5.        
  6. sbit DQ = P0^7;
  7. sbit CS=P0^4; //片选
  8. sbit RST =P0^2;//复位
  9. sbit DC =P0^3;//数据/命令控制
  10. sbit SCL=P0^0;//时钟 D0(SCLK?
  11. sbit SDIN=P0^1;//D1(MOSI) 数据

  12. u8 TempData[7];                          //温度存储显示值
  13. bit ReadTempFlag;                          //读时间标志

  14. void WriteOneChar(u8 dat);             //写入DS18B20一个字节
  15. u8 ReadOneChar(void);                 //读取DS18B20一个字节
  16. u32 ReadTemperature(void);             //读取DS18B20温度值
  17. bit Init_DS18B20(void);                //初始化DS18B20
  18. void Time0(void);                     //定时器初始化
  19. void Delay2us(u8 t);             //2微秒延时函数
  20. void Delay1ms(u8 t);              //1毫秒延时函数
  21. void OLED_Init();                                           //设置显示模式
  22. void delay100us(u32 x);             //100us延时程序

  23. unsigned char code Hzk[][32]={
  24. {0x10 ,0x22, 0x64 ,0xC ,0x80, 0x0 ,0xFE, 0x92, 0x92, 0x92 ,0x92, 0x92 ,0xFF ,0x2 ,0x0, 0x0 }, /* wen */
  25. {0x4, 0x4, 0xFE, 0x1, 0x40, 0x7E, 0x42 ,0x42 ,0x7E, 0x42, 0x7E, 0x42, 0x42, 0x7E, 0x40, 0x0},  /*1*/

  26. {0x0, 0x0, 0xFC, 0x24 ,0x24, 0x24 ,0xFC ,0xA5 ,0xA6, 0xA4 ,0xFC, 0x24, 0x34, 0x26, 0x4, 0x0},    /* du */
  27. {0x40 ,0x20, 0x9F, 0x80, 0x42, 0x42, 0x26, 0x2A, 0x12, 0x2A, 0x26, 0x42, 0x40, 0xC0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 0x0},
  52. };


  53. void delay_ms(unsigned int ms)
  54. {                        
  55.         unsigned int a;
  56.         while(ms)
  57.         {
  58.                 a=1800;
  59.                 while(a--);
  60.                 ms--;
  61.         }
  62.         return;
  63. }

  64. void OLED_WR_Byte(u8 dat,u8 cmd)
  65. {      
  66.         u8 i;                          
  67.         if(cmd)
  68.           DC=1;
  69.         else
  70.           DC=0;                  
  71.         CS=0;
  72.         for(i=0;i<8;i++)
  73.         {                          
  74.                 SCL=0;
  75.                 if(dat&0x80)
  76.                         {
  77.                    SDIN=1;
  78.                         }
  79. else
  80.                    SDIN=0;
  81.                                 SCL=1;
  82.                 dat<<=1;   
  83.         }                                                   
  84.         CS=1;
  85.         DC=1;            
  86. }

  87. void OLED_Clear(void)  
  88. {  
  89.         u8 i,n;                    
  90.         for(i=0;i<8;i++)  
  91.         {  
  92.                 OLED_WR_Byte (0xb0+i,0);    //设置页地址(0~7)
  93.                 OLED_WR_Byte (0x00,0);      //设置显示位置—列低地址
  94.                 OLED_WR_Byte (0x10,0);      //设置显示位置—列高地址   
  95.                 for(n=0;n<128;n++)OLED_WR_Byte(0,1);
  96.         } //更新显示
  97. }

  98. void OLED_Set_Pos(unsigned char x, unsigned char y)
  99. {
  100.         OLED_WR_Byte(0xb0+y,0);
  101.         OLED_WR_Byte(((x&0xf0)>>4)|0x10,0);
  102.         OLED_WR_Byte((x&0x0f)|0x01,0);
  103. }   

  104. void OLED_ShowCHinese(u8 x,u8 y,u8 no)
  105. {                                 
  106.         u8 t,adder=0;
  107.         OLED_Set_Pos(x,y);      
  108.     for(t=0;t<16;t++)
  109.                 {
  110.                                 OLED_WR_Byte(Hzk[2*no][t],1);
  111.                                 adder+=1;
  112.      }      
  113.                 OLED_Set_Pos(x,y+1);      
  114.     for(t=0;t<16;t++)
  115.                         {      
  116.                                 OLED_WR_Byte(Hzk[2*no+1][t],1);
  117.                                 adder+=1;
  118.       }                                       
  119. }

  120. void OLED_Showshuzi(u8 m,u8 n,u8 su)
  121. {                                 
  122.         u8 p,adder=0;
  123.         OLED_Set_Pos(m,n);      
  124.     for(p=0;p<16;p++)
  125.                 {
  126.                                 OLED_WR_Byte(Sz[2*su][p],1);
  127.                                 adder+=1;
  128.      }      
  129.                 OLED_Set_Pos(m,n+1);      
  130.     for(p=0;p<16;p++)
  131.                         {      
  132.                                 OLED_WR_Byte(Sz[2*su+1][p],1);
  133.                                 adder+=1;
  134.       }                                       
  135. }

  136. void OLED_Init(void)
  137. {
  138.     RST=1;
  139.         delay_ms(100);
  140.         RST=0;
  141.         delay_ms(100);
  142.         RST=1;
  143.         OLED_WR_Byte(0xAE,0);//--turn off oled panel
  144.         OLED_WR_Byte(0x00,0);//---set low column address
  145.         OLED_WR_Byte(0x10,0);//---set high column address
  146.         OLED_WR_Byte(0x40,0);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  147.         OLED_WR_Byte(0x81,0);//--set contrast control register
  148.         OLED_WR_Byte(0xCF,0); // Set SEG Output Current Brightness
  149.         OLED_WR_Byte(0xA1,0);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  150.         OLED_WR_Byte(0xC8,0);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  151.         OLED_WR_Byte(0xA6,0);//--set normal display
  152.         OLED_WR_Byte(0xA8,0);//--set multiplex ratio(1 to 64)
  153.         OLED_WR_Byte(0x3f,0);//--1/64 duty
  154.         OLED_WR_Byte(0xD3,0);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  155.         OLED_WR_Byte(0x00,0);//-not offset
  156.         OLED_WR_Byte(0xd5,0);//--set display clock divide ratio/oscillator frequency
  157.         OLED_WR_Byte(0x80,0);//--set divide ratio, Set Clock as 100 Frames/Sec
  158.         OLED_WR_Byte(0xD9,0);//--set pre-charge period
  159.         OLED_WR_Byte(0xF1,0);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  160.         OLED_WR_Byte(0xDA,0);//--set com pins hardware configuration
  161.         OLED_WR_Byte(0x12,0);
  162.         OLED_WR_Byte(0xDB,0);//--set vcomh
  163.         OLED_WR_Byte(0x40,0);//Set VCOM Deselect Level
  164.         OLED_WR_Byte(0x20,0);//-Set Page Addressing Mode (0x00/0x01/0x02)
  165.         OLED_WR_Byte(0x02,0);//
  166.         OLED_WR_Byte(0x8D,0);//--set Charge Pump enable/disable
  167.         OLED_WR_Byte(0x14,0);//--set(0x10) disable
  168.         OLED_WR_Byte(0xA4,0);// Disable Entire Display On (0xa4/0xa5)
  169.         OLED_WR_Byte(0xA6,0);// Disable Inverse Display On (0xa6/a7)
  170.         OLED_WR_Byte(0xAF,0);//--turn on oled panel

  171.         OLED_WR_Byte(0xAF,0); /*display ON*/
  172.         OLED_Clear();
  173.         OLED_Set_Pos(0,0);        
  174. }     
  175.                               



  176. void main()
  177. {                  
  178.       
  179.         u32 temp,i,j;
  180.         long tempM;
  181.         u8 TempH;
  182.         Time0();                          //定时器初始化
  183.         OLED_Init();                                //设置显示模式
  184.         while(1)         
  185.         {
  186.                 if(ReadTempFlag==1)      //定时标志
  187.                 {
  188.                         ReadTempFlag=0;
  189.                         temp=ReadTemperature();//读取温度

  190.                         tempM=temp*0.0625*100;        //换算为10进制温度值再放大100倍        12位精度直接乘0.625
  191.                         TempH=tempM/100;             //取整数      
  192.                   
  193.                        
  194.                         i=(TempH%100)/10;                //显示十位温度
  195.                         j=(TempH%100)%10;               //显示个位温度
  196. //                        k=TempL/10;                   //显示温度小数1位
  197.                                 OLED_ShowCHinese(0,0,0);
  198.         OLED_ShowCHinese(18,0,1);
  199.         OLED_ShowCHinese(36,0,2);
  200.         OLED_Showshuzi(54,0,0);
  201.         OLED_Showshuzi(72,0,i);
  202.         OLED_Showshuzi(90,0,j);
  203.         OLED_ShowCHinese(108,0,1);
  204.                 }
  205.         }
  206. }

  207. /*------------------------------------------------
  208.             定时器初始化子程序
  209. ------------------------------------------------*/
  210. void Time0(void)
  211. {
  212.          TMOD = 0x01;          //使用模式1,16位定时器                  
  213.          TH0=(65536-5000)/256;              //给定初值,定时5ms
  214.          TL0=(65536-5000)%256;
  215.          EA=1;               //总中断打开
  216.          ET0=1;             //定时器中断打开
  217.          TR0=1;            //定时器T0开关打开
  218. }
  219. /*------------------------------------------------
  220.                  定时器中断子程序
  221. ------------------------------------------------*/
  222. void Time0r(void) interrupt 1
  223. {
  224.         static u32 num;             //局部变量定义
  225.         TH0=(65536-5000)/256;        //重新赋值 5ms
  226.         TL0=(65536-5000)%256;
  227.         num++;
  228.         if(num>=25)        
  229.         {
  230.                 num = 0;
  231.                 ReadTempFlag=1;  //读标志位置1
  232.         }
  233. }


  234. void Delay2us(u8 t)
  235. {   
  236.         while(--t);
  237. }

  238. void Delay1ms(u8 t)
  239. {
  240.         while(t--)
  241.         {
  242.                 //大致延时1mS
  243.                 Delay2us(245);
  244.                 Delay2us(245);
  245.         }
  246. }
  247. /*------------------------------------------------
  248.                     18b20初始化
  249. ------------------------------------------------*/
  250. bit Init_DS18B20(void)
  251. {
  252.         bit dat=0;
  253.         DQ = 1;    //DQ复位
  254.         Delay2us(5);   //稍做延时
  255.         DQ = 0;         //单片机将DQ拉低
  256.         Delay2us(200); //精确延时 大于 480us 小于960us
  257.         Delay2us(200);
  258.         DQ = 1;        //拉高总线
  259.         Delay2us(50); //15~60us 后 接收60-240us的存在脉冲
  260.         dat=DQ;        //如果x=0则初始化成功, x=1则初始化失败
  261.         Delay2us(25); //稍作延时返回
  262.         return dat;
  263. }

  264. /*------------------------------------------------
  265.                     读取一个字节
  266. ------------------------------------------------*/
  267. u8 ReadOneChar(void)
  268. {
  269.         u8 i=0;
  270.         u8 dat = 0;
  271.         for (i=8;i>0;i--)
  272.         {
  273.                 DQ = 0; // 给脉冲信号
  274.                 dat>>=1;
  275.                 DQ = 1; // 给脉冲信号
  276.                 if(DQ)
  277.                 dat|=0x80;
  278.                 Delay2us(25);
  279.         }
  280.         return(dat);
  281. }
  282. /*------------------------------------------------
  283.                     写入一个字节
  284. ------------------------------------------------*/
  285. void WriteOneChar(u8 dat)
  286. {
  287.         u8 i=0;
  288.         for(i=8; i>0; i--)
  289.         {
  290.                 DQ = 0;
  291.                 DQ = dat&0x01;
  292.                 Delay2us(25);
  293.                 DQ = 1;
  294.                 dat>>=1;
  295.         }
  296.         Delay2us(25);
  297. }

  298. /*------------------------------------------------
  299.                     读取温度
  300. ------------------------------------------------*/
  301. u32 ReadTemperature(void)
  302. {
  303.         u8 a=0;
  304.         u32 b=0;
  305.         u32 t=0;
  306.         Init_DS18B20();
  307.         WriteOneChar(0xCC); // 跳过读序号列号的操作
  308.         WriteOneChar(0x44); // 启动温度转换         44H开启温度转换
  309.         Delay1ms(10);
  310.         Init_DS18B20();
  311.         WriteOneChar(0xCC); //跳过读序号列号的操作
  312.         WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
  313.         a=ReadOneChar();   //低位
  314.         b=ReadOneChar();   //高位
  315.         b<<=8;
  316.         t=a+b;
  317.         return(t);
  318. }
  319. /*******100us延时程序********/

  320. void delay100us(u32 x)
  321. {
  322.         u32 a,b;
  323.         for(a=x;a>0;a--)
  324.                 for(b=10;b>0;b--);
  325. }
复制代码

所有程序51hei提供下载:
新建文件夹.rar (41.09 KB, 下载次数: 54)

评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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