⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20_7seg.txt

📁 51单片机18b20温度监测数码管显示
💻 TXT
字号:
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

uint temp;                // variable of temperature   定义一个变量

sbit P2_0=P2^0;           //数码管位选
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit DQ=P3^3;             //define interface 定义接口

uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,0x90};   //数字编码
uchar code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02, 0x78,0x00,0x10};//带小数点的编码

//-----------------------------------------------
void delay(uint i)       //delay    延时子程序
{
  while(i--);
}

//-----------------------------------------------
void delay10ms()        //delay            延时10MS子函数
{
  uchar a,b;
  for(a=50;a>0;a--)
    for(b=60;b>0;b--);
}

//初始化ds18b2子函数
//-----------------------------------------------
Init_DS18B20(void)
{
 unsigned char x=0;
 DQ = 1;                                          //DQ复位
 delay(8);                                        //稍做延时
 DQ = 0;                                          //单片机将DQ拉低
 delay(80);                                       //精确延时 大于 480us
 DQ = 1;                                          //拉高总线
 delay(14);
 x=DQ;                                            //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
 delay(20);
}

//读字节子函数
//-----------------------------------------------
ReadOneChar(void)
{
 unsigned char i=0;
 unsigned char dat = 0;
 for (i=8;i>0;i--)
  {
   DQ = 0;                                           // 给脉冲信号
   dat>>=1;
   DQ = 1;                                           // 给脉冲信号
   if(DQ)
    dat|=0x80;
   delay(4);
  }
 return(dat);
}

//写字节子函数
//-----------------------------------------------
WriteOneChar(unsigned char dat)
{
 unsigned char i=0;
 for (i=8; i>0; i--)
 {
   DQ = 0;
   DQ = dat&0x01;
   delay(5);
   DQ = 1;
   dat>>=1;
 }
}

/*void tmpwritebyte(uchar dat)    //write a byte to ds18b20 给温度传感器写一个字节
{
   uint i;
   uchar j;
   bit testb;
   for(j=1;j<=8;j++)
   {
     testb=dat&0x01;
     dat=dat>>1;
     if(testb)      //write 1
     {
       DQ=0;
       i++;i++;
       DQ=1;
       i=8;while(i>0)i--;
     }
     else
     {
       DQ=0;        //write 0
       i=8;while(i>0)i--;
       DQ=1;
       i++;i++;
     }
   }
}*/

//-----------------------------------------------
void tmpchange(void)                                        //DS18B20 begin change    发送温度转换命令
{
 Init_DS18B20();                                            //初始化DS18B20
 delay(200);                                                //延时
 WriteOneChar(0xcc);                                        //跳过序列号命令
 WriteOneChar(0x44);                                        //发送温度转换命令
}

//-----------------------------------------------
uint tmp()                                                  //get the temperature          得到温度值
{
 float tt;
 uchar a,b;
 Init_DS18B20();
 delay(1);
 WriteOneChar(0xcc);
 WriteOneChar(0xbe);                                        //发送读取数据命令
 a=ReadOneChar();                                           //连续读两个字节数据
 b=ReadOneChar();
 temp=b;
 temp<<=8;                                               
 temp=temp|a;                                               //两字节合成一个整型变量。
 tt=temp*0.0625;                                            //得到真实十进制温度值,因为DS18B20
                                                            //可以精确到0.0625度,所以读回数据的最低位代表的是0.0625度。
 temp=tt*10+0.5;                                            //放大十倍,这样做的目的将小数点后第一位也转换为可显示数字,同时进行一个四舍五入操作。
 return temp;                                               //返回温度值

}

/*void readrom()        //read the serial 读取温度传感器的序列号
{                       //本程序中没有用到此函数
   uchar sn1,sn2;
   Init_DS18B20();
   delay(1);
   WriteOneChar(0x33);
   sn1=ReadOneChar();
   sn2=ReadOneChar();
}*/

//-----------------------------------------------
void display(uint tem)            //display        显示子函数
{
 uchar A1,A2,A2t,A3;
 if(tem>=100)
  {
   A1=table[tem/100];
   A2t=tem%100;
   A2=table1[A2t/10];
   A3=table[A2t%10];
  }
 else
  {
   A2t=tem%100;
   A1=table1[A2t/10];
   A2=table[A2t%10];
   A3=0xFF;
  }
 
 P0=A1;
 P2_0=0;
 P2_1=1;
 P2_2=1;
 delay10ms();
 P0=A2;
 P2_1=0;
 P2_0=1;
 P2_2=1;
 delay10ms();
 if(A3!=0xFF)
  {
   P0=A3;
   P2_2=0;
   P2_0=1;
   P2_1=1;
  }
}

//-----------------------------------------------
void main()                               //主函数
{
 
 while(1)
 {
  tmpchange();                            //温度转换
  display(tmp());                         //显示温度值
 }

}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -