#include<pic1687x.h>
#define uchar unsigned char
#define uint unsigned int
#define DQ RD0
#define ds1820_high TRISD0=1
#define ds1820_low() TRISD0=0,RD0=0
uint receive[4];
void ds18b20_delay(char x,char y)
{
char z;
do{
z=y;
do{;}while(--z);
}while(--x);
}
/*ds18b20_in_1()
{
ds18b20_fang_1=1;
}
ds18b20_out_low_1()
{
ds18b20_fang_1=0;
ds18b20_data_1=0;
}*/
uchar ds18b20_init_1()
{
uchar i,temp_abse;
temp_abse=0;
for(i=0;i<20;i++)
{
ds1820_low();
ds18b20_delay(2,70);
ds1820_high;
ds18b20_delay(2,8);
if(DQ==0)
{
temp_abse=1;
break;
}
}
ds18b20_delay(2,60);
return (temp_abse);
}
void ds18b20_write_1(dat)
uchar dat;
{
uchar i,temp;
for(i=8;i>0;i--)
{
temp=dat&0x01; //最低位移出
ds1820_low();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop"); //从高拉至低电平,产生写时间隙
if(temp==1) //如果写1,拉高电平
{
ds1820_high;
}
ds18b20_delay(2,7); //延时63us
ds1820_high;
asm("nop");
asm("nop");
dat=dat>>1; //右移一位
}
}
uchar ds18b20_read_1()
{
uchar i;
uchar value=0; //读出温度
static bit j;
for(i=8;i>0;i--)
{
value>>=1;
ds1820_low();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop"); //6us
ds1820_high; //拉至高电平
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop"); //4us
j=DQ;
if(j) value|=0x80;
ds18b20_delay(2,7); //63us
}
return(value);
}
ds18b20_1()
{
uchar ds18b20_absel,temper_h,temper_l;
float temper;
uint aaa;
ds1820_high;
ds18b20_absel=ds18b20_init_1();
ds18b20_init_1();
if(ds18b20_absel==1)
{
ds18b20_write_1(0xcc);
ds18b20_write_1(0x44);
ds18b20_delay(0xff,0xff);
ds18b20_absel=ds18b20_init_1();
// ds18b20_init_1();
ds18b20_write_1(0xcc);
ds18b20_write_1(0xbe);
temper_l=ds18b20_read_1();
temper_h=ds18b20_read_1();
}
else
{
temper_h=0xff;
temper_l=0xff;
}
ds1820_high;
temper=(temper_h*256+temper_l)*6.25;
aaa=(int)temper;
return aaa;
}
init_serial()
{
TRISC=0xff;
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x19;
GIE=1;
PEIE=1;
RCIE=1;
}
void interrupt ZD()
{
uchar date;
if(RCIF==1&RCIE==1)
{
date=RCREG;
switch(date)
{
case 0x44: TXREG=receive[0]; break;
case 0x55: TXREG=receive[1]; break;
case 0x66: TXREG=receive[2]; break;
case 0x77: TXREG=receive[3]; break;
//case 0x88: TXREG=0X18;break;
default:break;
}
}
}
main()
{
uint temp1;
init_serial();
while(1)
{
temp1=ds18b20_1();
receive[0]=temp1/1000;
receive[1]=temp1%1000/100;
receive[2]=temp1%100/10;
receive[3]=temp1%10;
}
}
zdc.rar_ZDC
版权申诉
69 浏览量
2022-09-22
21:29:07
上传
评论
收藏 34KB RAR 举报
APei
- 粉丝: 64
- 资源: 1万+