/**************************************************************************************************/
#include“DS_1602.h”
/*************************************************************************************/
/***********-----DS18B20.C-----***********/
/*************************************************************************************/
/*------初始化DS18B20------*/
void UESC_DS_Init()
{
unsigned char i,j;
ds =1; //ds复位
i = 3; while (--i);//延时2us
ds =0; //拉低
_nop_();//延时750us
_nop_();
i = 9;
j = 189;
do
{
while (--j);
} while (--i);//延时750us结束
ds=1; //拉高
_nop_();//延时60us
_nop_();
i = 1;
j = 175;
do
{
while (--j);
} while (--i);//延时60us结束
//此处对DS18B20存在的判断省略
_nop_();//延时480us
_nop_();
i = 6;
j = 150;
do
{
while (--j);
} while (--i);//延时480us结束
ds=1;
}
/*------写指令------*/
void UESC_DS_WriteByte(unsigned char dat)
{
unsigned char i,j,x;
bit testbit;
for (x=8; x》0; x--)
{
testbit=dat&0x01;
dat》》=1;
if(testbit)//如果testbit为1就写1
{
ds=0;
i = 42; while (--i);//拉低15us
ds=1;
_nop_(); //延时45us
_nop_();
i = 1;
j = 130;
do
{
while (--j);
}while (--i);//延时45us结束
ds=1;
}
else//否则写0
{
ds=0;
i = 42; while (--i);//拉低15us
ds=0;
_nop_(); //延时45us
_nop_();
i = 1;
j = 130;
do
{
while (--j);
}while (--i);//延时45us结束
ds=1;
}
}
}
/*------读一位数据------*/
unsigned char UESC_DS_Readbit()
{
unsigned char i;
bit dat;
ds = 1;
i = 3; while (--i);//延时2us
ds = 0;
i = 15; while (--i);//延时6us
ds=1;
i = 9; while (--i);//延时4us
dat=ds; //读取状态位
i = 87; while (--i);// 延时30us
return(dat);
}
/*------读一个字节数据------*/
unsigned char UESC_DS_ReadByte() //循环执行8次读一位
{
unsigned int i;
unsigned char dat=0,dat1;
for(i=0; i《8; i++) //循环执行8次读一位操作函数
{
dat1=UESC_DS_Readbit();
dat = (dat1《《 7) | (dat 》》 1);//先读出最低位
}
return (dat);

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部1条评论
快来发表一下你的评论吧 !