📄 wenducaiji.c
字号:
#define uchar unsigned char
#define uint unsigned int
#include<REG51.H>
sbit DQ=P3^7;
sbit P3_0=P3^0;
sbit P3_1=P3^1;
sbit P3_2=P3^2;
sbit P3_3=P3^3;
uint tcnt;
uint cursor=0;
uchar code Seg1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code Seg0[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar code Seg2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(uint t)
{
uint i;
while(t--)
{for (i=0;i<125;i++);}
}
void Tdelay(unsigned int i) //为8μs的延时子函数
{
while(i--);
}
Init_DS18B20(void) //初始化子程序
{
unsigned char x=0;
DQ = 1;
Tdelay(8);
DQ = 0; //发复位脉冲
Tdelay(80); //主机发送复位脉冲至少持续480uS(此处延时应该有640us)
DQ = 1; //释放总线
Tdelay(14); //释放总线等待一段时间,存在等待
Tdelay(20);
}
//读一个字节
ReadOneChar(void)
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
Tdelay(4);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Tdelay(5);
DQ = 1;
dat>>=1;
}
}
//读取温度
ReadTemperature(void)
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
Init_DS18B20(); //初始化
WriteOneChar(0xCC); //CCH跳过ROM
WriteOneChar(0x44); //44H温度变换
Init_DS18B20(); //再次初始化
WriteOneChar(0xCC); //CCH跳过ROM
WriteOneChar(0xBE); //BEH读暂存存储器
a=ReadOneChar(); //读低八位
b=ReadOneChar(); //读高八位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*100;
return(t);
}
void display(uchar L1,uchar L2,uchar L3,uchar L4)
{
P2=0xFE;P0=L1;delay(1); //temp
P2=0xFD;P0=L2;delay(1); //temp
P2=0xFB;P0=L3;delay(1); //temp
P2=0xF7;P0=L4;delay(1); //temp
}
void main()
{ uint i=0;
while(1)
{ i=ReadTemperature();
delay(3);
display(Seg1[i/1000],Seg0[i/100%10],Seg1[i/10%10],Seg1[i%10]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -