keil p0 0c语言不了,求AT89S52单片机与DS18B20传感器用Keil c语言设计程序

你好。看来志同道合,我也做过DS18B20温控设备,单总线传感这是一个不错的选择哦。下面是C语言程序,硬件接线如下:

(1)把“单片机系统”区域中的P0.0-P0.7用8芯排线连接到“动态数码显示”区域中的ABCDEFGH端子上。

(2)把“单片机系统”区域中的P2.0-P2.7用8芯排线连接到“动态数码显示”区域中的S1S2S3S4S5S6S7S8端子上。

(3)把DS18B20芯片插入“四路单总线”区域中的任一个插座中,注意电源与地信号不要接反。

(4)把“四路单总线”区域中的对应的DQ端子连接到“单片机系统”区域中的P3.7/RD端子上。

至于你的7219的话就要你自己硬件接线了。

#include 

#include 

unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7,

0xef,0xdf,0xbf,0x7f};

unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00,0x40};

unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,

25,28,31,34,38,41,44,48,

50,53,56,59,63,66,69,72,

75,78,81,84,88,91,94,97};

unsigned char displaycount;

unsigned char displaybuf[8]={16,16,16,16,16,16,16,16};

unsigned char timecount;

unsigned char readdata[8];

sbit DQ=P3^7;

bit sflag;

bit resetpulse(void)

{

unsigned char i;

DQ=0;

for(i=255;i>0;i--);

DQ=1;

for(i=60;i>0;i--);

return(DQ);

for(i=200;i>0;i--);

}

void writecommandtods18b20(unsigned char command)

{

unsigned char i;

unsigned char j;

for(i=0;i<8;i++)

{

if((command & 0x01)==0)

{

DQ=0;

for(j=35;j>0;j--);

DQ=1;

}

else

{

DQ=0;

for(j=2;j>0;j--);

DQ=1;

for(j=33;j>0;j--);

}

command=_cror_(command,1);

}

}

unsigned char readdatafromds18b20(void)

{

unsigned char i;

unsigned char j;

unsigned char temp;

temp=0;

for(i=0;i<8;i++)

{

temp=_cror_(temp,1);

DQ=0;

_nop_();

_nop_();

DQ=1;

for(j=10;j>0;j--);

if(DQ==1)

{

temp=temp | 0x80;

}

else

{

temp=temp | 0x00;

}

for(j=200;j>0;j--);

}

return(temp);

}

void main(void)

{

TMOD=0x01;

TH0=(65536-4000)/256;

TL0=(65536-4000)%256;

ET0=1;

EA=1;

while(resetpulse());

writecommandtods18b20(0xcc);

writecommandtods18b20(0x44);

TR0=1;

while(1)

{

;

}

}

void t0(void) interrupt 1 using 0

{

unsigned char x;

unsigned int result;

TH0=(65536-4000)/256;

TL0=(65536-4000)%256;

if(displaycount==2)

{

P0=displaycode[displaybuf[displaycount]] | 0x80;

}

else

{

P0=displaycode[displaybuf[displaycount]];

}

P2=displaybit[displaycount];

displaycount++;

if(displaycount==8)

{

displaycount=0;

}

timecount++;

if(timecount==150)

{

timecount=0;

while(resetpulse());

writecommandtods18b20(0xcc);

writecommandtods18b20(0xbe);

readdata[0]=readdatafromds18b20();

readdata[1]=readdatafromds18b20();

for(x=0;x<8;x++)

{

displaybuf[x]=16;

}

sflag=0;

if((readdata[1] & 0xf8)!=0x00)

{

sflag=1;

readdata[1]=~readdata[1];

readdata[0]=~readdata[0];

result=readdata[0]+1;

readdata[0]=result;

if(result>255)

{

readdata[1]++;

}

}

readdata[1]=readdata[1]<<4;

readdata[1]=readdata[1] & 0x70;

x=readdata[0];

x=x>>4;

x=x & 0x0f;

readdata[1]=readdata[1] | x;

x=2;

result=readdata[1];

while(result/10)

{

displaybuf[x]=result%10;

result=result/10;

x++;

}

displaybuf[x]=result;

if(sflag==1)

{

displaybuf[x+1]=17;

}

x=readdata[0] & 0x0f;

x=x<<1;

displaybuf[0]=(dotcode[x])%10;

displaybuf[1]=(dotcode[x])/10;

while(resetpulse());

writecommandtods18b20(0xcc);

writecommandtods18b20(0x44);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值