/****************************************************************************************************************
*************************
*Author:JYW
*Time:2019_04_26
*Describe:LetAllLinesOfCodeSpeak
*****************************************************/
/*******************************************
*Auther:JYW
*Time:2019_04_26
*Describe:代码练习DS18B20等驱动练习
************************************/
#define DS18B20IO P0_7
unsigned char Ds18b20Init()
{
unsigned char Status = 0x00;
unsigned int CON_T = 0;
unsigned char Flag_1 = 1;
P0DIR |= 0x80;
DS18B20IO = 1;
Delay_us(250);
DS18B20IO = 0;
Delay_us(750);
DS18B20IO = 1;
P0DIR &=0x7F;
while((DS18B20IO != 0)&&(Flag_1 == 1))
{
CONT_1++;
Delay_us(10);
if(CONT_1 > 8000)Flag_1 = 0
Status = DS18B20IO;
}
P0DIR |= 0x80;
DS18B20IO = 1;
Delay_us(100);
return Status;
}
void Ds18b20Write(unsigned char infor)
{
unsigned int i;
P0DIR |=0x80;
for(i=0;i<8;i++)
{
if((infor & 0x01))
{
DS18B20IO = 0;
Delay_us(6);
DS18B20IO = 1;
Delay_us(50);
}
else
{
DS18B20IO = 0;
Delay_us(50);
DS18B20IO = 1;
Delay_us(6);
}
infor >>=1;
}
}
unsigned char Ds18b20Read(void)
{
unsigned char Value = 0x00;
unsigned int i;
P0DIR |=0x80;
DS18B20IO = 1;
Delay_us(10);
for(i=0;i<8;i++)
{
Value >>=1;
P0DIR |=0x80;
DS18B20IO = 0;
Delay_us(3);
DS18B20IO = 1;
Delay_us(3);
P0DIR &= 0x7F;
if(1== DS18B20IO) Value |=0x80;
Delay_us(15);
}
return Value;
}
unsigned char ReadDs18b20()
{
unsigned char V1,V2;
unsigned char temp;
Ds18b20Init();
Ds18b20Write(0xcc);
Ds18b20Write(0x44);
Ds18b20Initial();
Ds18b20Write(0xcc);
Ds18b20Write(0xbe);
V1 = Ds18b20Read(); //低位0110 0000
V2 = Ds18b20Read(); //高位0010 0011
temp = ((V1 >> 4)+((V2 & 0x07)*16)); //转换数据
// 0 011 0110
return temp;
}