|
//AVR+GCC
#include
#include
#define DQ 0x05
#define DQ_1 PORTB |= (1<
#define DQ_0 PORTB &= ~(1<
#define DQ_IN DDRB |= (1<
#define DQ_OUT DDRB &= ~(1<
//t = (ret*7+18)us
//2 = 32us
//5 = 52us
//10 = 87us
void Delay(int ret) //
{
int i;
for(i=0;i
}
unsigned char Reset_DS18B20(void)
{
unsigned char ret;
DQ_IN;
DQ_1;
DQ_0; //
Delay(70); //
DQ_1;
DQ_OUT;
Delay(16); //
ret = PINB;
if(ret & 0x20)
{
return 0x00;
}
else
{
Delay(50);
return 0x55;
}
}
void WR_One_Byte(unsigned char ret)
{
unsigned char i;
DQ_IN;
DQ_1;
for(i=0;i<8;i++)
{
DQ_0;
Delay(0); //
if(ret&0x01) DQ_1; //
Delay(4);
DQ_1;
ret >>= 0x01; //
Delay(0);
}
}
unsigned char RD_One_Byte(void )
{
unsigned char i,ret;
ret=0;
for(i=0;i<8;i++)
{
DQ_IN;
DQ_1;
DQ_0; //
ret >>= 0x01;
DQ_OUT;
DQ_1; //
if(PINB & 0x20) ret |= 0x80;
Delay(5); //
}
return ret;
}
void Start_Temp(void)
{
if(Reset_DS18B20()==0x55)
{
Delay(300); //
WR_One_Byte(0xcc); //
WR_One_Byte(0x44);
}
}
unsigned int Read_Temp(void)
{
//float wendu;
unsigned char TempL,TempH;
unsigned int Temp;
if(Reset_DS18B20()==0x55)
{
Delay(300); //
WR_One_Byte(0xcc);
WR_One_Byte(0xbe);
TempL=RD_One_Byte();
TempH=RD_One_Byte();
Temp = TempH;
Temp <<= 0x08;
Temp |= TempL;
if(Temp & 0xf000)
{
Temp = 0;
}
return Temp;
/*
if(Temp & 0xf000)
{
Temp = 0;
}
else
{
wendu = Temp;
wendu *= 0.0625;
Temp = (unsigned int)wendu;
}
return Temp;*/
}
else
{
return 0;
}
}
|
|