DS18B20测试程序
#include
#define uint unsigned int
#define uchar unsigned char
/************DS18B20**********/
sbit DQ=P3^7;
uchar temph=0,templ=0;
uint t=0;
float tt=0;
void delay_b20(uint z)//ds18b20延时
{
while(z--);
}
void b20_reset(void)//ds18b20复位
{
uchar ready=0;
DQ=1;
delay_b20(8);
DQ=0;
delay_b20(80);
DQ=1;
delay_b20(14);
ready=DQ;
delay_b20(20);
}
/*uchar b20_r_bit(void)//读ds18b201Bit
{
uchar i;
DQ=0;
DQ=1;
for(i=0;i<3;i++);
return(DQ);
}
void b20_w_bit(uchar bitval)//写ds18b201Bit
{
DQ=0;
if(bitval==1)
DQ=1;
delay_b20(5);
DQ=1;
}
void b20_w_byte(uchar val)//写入ds18b20一字节
{
uchar i,temp;
for(i=0;i<8;i++)
{
temp=val>>1;
temp=temp&0x01;
b20_w_bit(temp);
delay_b20(5);
}
}
uchar b20_r_byte(void)//读出ds18b20一字节
{
uchar i,j,date;
j=1;
date=0;
for(i=0;i<8;i++)
{
if(b20_r_bit())
{
date=date+(j<
}
delay_b20(4);
}
return(date);
}*/
uchar b20_r_byte(void)//读DS18B20一字节
{
uchar i=0;
uchar dat=0;
for(i=0;i<8;i++)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay_b20(4);
}
return(dat);
}
void b20_w_byte(uchar dat)//写DS18B20一字节
{
uchar i=0;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay_b20(5);
DQ=1;
dat>>=1;
}
}
uint b20_gettemp(void)//获取DS18B20上温度
{
b20_reset();
b20_w_byte(0xcc);
b20_w_byte(0x44);
//delay_b20(125);
b20_reset();
b20_w_byte(0xcc);
b20_w_byte(0xbe);
templ=b20_r_byte();
temph=b20_r_byte();
t=temph;
t<<=8;
t=t|templ;
tt=t*0.0625;
t=tt*10+0.5;
//delay_b20(200);
return(t);
}
/*************LCD12864*************/
sbit beer=P2^0;
#define P0 P0
uchar co
uchar co
uchar co
uchar co
sbit lcd_rs=P1^0;//定义LCD引脚
sbit lcd_rw=P1^1;
sbit lcd_en=P1^2;
sbit lcd_psb=P2^4;
sbit lcd_rst=P2^3;
void delay(uint z) //延时
{
uint x,y;
for(x=110;x>0;x--);
for(y=z;y>0;y--);
}
void lcd_w_com(uchar com)//写命令
{
lcd_rs=0;
lcd_rw=0;
lcd_en=0;
P0=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void lcd_w_da
{
lcd_rs=1;
lcd_rw=0;
lcd_en=0;
P0=date;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void lcd_init()//液晶初始化
{
lcd_rst=0;
delay(10);
lcd_rst=1;
delay(10);
lcd_psb=1;
P3=0xff;
key5=1;
key6=1;
lcd_w_com(0x30);
lcd_w_com(0x0c);
lcd_w_com(0x14);
lcd_w_com(0x01);
}
void lcd_pos(uchar x,uchar y)//选择写入位置
{
uchar pos;
if(x==0)
{x=0x80;}
else if(x==1)
{x=0x90;}
else if(x==2)
{x=0x88;}
else if(x==3)
{x=0x98;}
pos=x+y;
lcd_w_com(pos);
}
void clr_screen()//清屏
{
lcd_w_com(0x34);
delay(5);
lcd_w_com(0x30);
delay(5);
lcd_w_com(0x01);
}
void disp_chinese(uchar co
{
while(*s>0)
{
lcd_w_da
s++;
delay(500);
}
}
void disp_temp()
{
uint i=0;
uchar a=0,b=0,c=0;
/*uint shi,ge,xiaoshu;
uchar a=0,b=0,c=0;
lcd_pos(3,3);
i=b20_gettemp();
shi=i/100;
ge=i/10%10;
xiaoshu=i%10;
lcd_pos(3,3);
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
disp_chinese("℃");*/
lcd_pos(3,3);
i=b20_gettemp();
a=i/100;
lcd_w_da
b=i/10-a*10;
lcd_w_da
lcd_w_da
c=i-a*100-b*10;
lcd_w_da
disp_chinese("℃");
}
void main()
{
lcd_init();
while(1)
{
disp_title();
disp_temp();
}
}
评论