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

📄 cewen.c

📁 基于18b20和单片机控制的温度采集及显示系统
💻 C
字号:
  #include<reg52.h>
#include<math.h>
unsigned char code  SEG[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff}; 
unsigned int i=0,fuhao=11,baiwei=0,shiwei=0,gewei=0,xiaoshu=0;

sbit DQ=P3^5;//定义通信端口
 
//延时函数
void delay(unsigned int j)
{
 while(j--);
}

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

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

//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char j=0;
for(j=8;j>0;j--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
//delay(4);
}

//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC);//跳过读序号列号的操作
WriteOneChar(0x44);//启动温度转换
Init_DS18B20();
WriteOneChar(0xCC);//跳过度序号列号的操作
WriteOneChar(0xBE);//读取温度寄存器等(共可读9个寄存器)前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
if(b&0xf8)
{
t=(~t)+1;
tt=0-t*0.0625;
}
else
tt=t*0.0625;
tt=tt*10+0.5;//放大10倍输出并四舍五入- 此行没用 
return(tt);
}



void SHOWTEMP( int t)
{
if(t<0)
fuhao=10;
else fuhao=11;

t=abs(t);
shiwei=t/100;
gewei=(t-shiwei*100)/10;
xiaoshu=t-shiwei*100-gewei*10;

i++; 
if(i>4) i=1;
if(i==1)
{ P0=0xfe; P2=SEG[fuhao];  }
if(i==2)
{
 P0=0xfd; P2=SEG[shiwei]; }
if(i==3)
{ P0=0xfb; P2=SEG[gewei]-0x80;}
if(i==4)
{ P0=0xf7; P2=SEG[xiaoshu]; }

}



SHOW(void) interrupt 1
{
  TH0=0xfc;     
  TL0=0x17;      
  TR0=1; 
 } 




int main()
{
	int temp=0;
	TH0=0xfc;   
	TL0=0x17;      
	 TR0=1; IE=0x82;	 TMOD=0x01;
	while(1)
	{
	temp=ReadTemperature();//读温度
	SHOWTEMP(temp);
	}
	return 0;
}

⌨️ 快捷键说明

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