DS18B20温度控制程序文档格式.docx
- 文档编号:16568567
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:13
- 大小:16.39KB
DS18B20温度控制程序文档格式.docx
《DS18B20温度控制程序文档格式.docx》由会员分享,可在线阅读,更多相关《DS18B20温度控制程序文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
*/
//ucharcodesmg_wu[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
ucharcodesmg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
voiddelay_us()
{
_nop_();
_nop_();
}
voiddelay(uchart)
while(t--);
voiddelayms(uinta)
uinti,j;
for(i=a;
i>
0;
i--)
for(j=110;
j>
j--);
voidinit_ds18b20()
/*ucharn;
//这种写法不行
DQ=1;
delay(8);
DQ=0;
delay(80);
n=DQ;
delay(4);
delay(35);
voidwrite_byte(uchardat)
uchari;
for(i=0;
i<
8;
i++)
{
DQ=0;
DQ=dat&
0x01;
delay(4);
DQ=1;
dat>
>
=1;
}
delay(3);
ucharread_byte()
uchari,value;
value>
if(DQ)
value|=0x80;
returnvalue;
uintreadtemp()
uchara,b;
uintT;
init_ds18b20();
write_byte(0xcc);
//跳过ROM
write_byte(0x44);
//启动温度测量
delay(250);
write_byte(0xbe);
a=read_byte();
//保存低位
b=read_byte();
//保存高位
//b<
<
=4;
//不要小数部分
//b+=(a&
0xf0)>
4;
T=b;
T<
=8;
T|=a;
T*=0.625;
returnT;
voiddisplay()
P1=0xbf;
P0=smg_du[temp/100];
delayms(3);
P0=0x00;
P1=0xdf;
P0=smg_du[temp%100/10]|0x80;
//或个0x80的目的是把小数点显示出来
P1=0xef;
P0=smg_du[temp%100%10];
voidSZdisplay()
P0=smg_du[sdtemp/100];
delayms
(1);
P0=smg_du[sdtemp%100/10]|0x80;
//同上
P0=0x00;
P0=smg_du[sdtemp%100%10];
voidWDSheZhi()
ucharp;
if(!
key0)
delayms(5);
if(!
{
a=0;
//有键按下a就清0,以防止有键按下时就开始记数
flag=1;
sdtemp++;
/*zhen=sdtemp/10;
xiao=sdtemp%10;
wirtebyte(0,zhen);
delayms
(2);
//最少要2ms
wirtebyte(1,xiao);
if(sdtemp==610)sdtemp=400;
while(!
{
SZdisplay();
//按键时也让数码管显示出来//必须要写在此处
delayms(6);
//这个延时必须要,不然数码管会有点儿闪
p++;
if(p>
100)
{
sdtemp+=5;
SZdisplay();
p=70;
}
zhen=sdtemp/10;
delayms
(2);
//最少2ms
if(sdtemp==610)sdtemp=400;
}
P=0;
}
delay(5);
while(!
key0);
//按键时也让数码管显示出来//必须要写在此处
TR0=1;
//启动定时器计数
elseif(!
key1)
//同上
sdtemp--;
if(sdtemp==0)sdtemp=400;
key1)
sdtemp-=5;
if(sdtemp==0)sdtemp=400;
p=0;
key1);
//当按键一松手时启动定时器计数
}
voidinitime0()
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
voidmain()
initime0();
//初始化函数一定要放在while
(1)前面才有效
zhen=readbyte(0);
xiao=readbyte
(1);
sdtemp=zhen*10+xiao;
while
(1)
/*wirtebyte(0,zhen);
//可以不要
delayms
(2);
wirtebyte(1,xiao);
temp=readtemp();
WDSheZhi();
flag)
{
display();
else
SZdisplay();
if(a==200)
a=0;
flag=0;
if(temp<
sdtemp)hot=1;
elsehot=0;
}
voidtime0()interrupt1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
///////////////////////////////////////
voidinit_ii(void)
sda=1;
scl=1;
voidstart()
sda=1;
scl=1;
delay_us();
sda=0;
voidstop()
voidack()
while((sda==1)&
&
(i<
200))i++;
scl=0;
voidno_ack()
voidiicw_byte(uchardat)
uchari;
scl=0;
for(i=0;
{
if(dat&
0x80)
sda=1;
else
sda=0;
dat=dat<
1;
delay_us();
scl=1;
scl=0;
}
delay_us();
uchariicr_byte()
uchari,dat;
if(sda)
dat++;
returndat;
voidwirtebyte(ucharadd,uchardat)
init_ii();
start();
iicw_byte(0xa0);
ack();
iicw_byte(add);
iicw_byte(dat);
stop();
ucharreadbyte(ucharadd)
uchara;
iicw_byte(0xa1);
a=iicr_byte();
no_ack();
returna;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DS18B20 温度 控制程序