#include "../../include.h"
#if EXTERNAL_MODULE_DS18B20_MODE !=0
void DS18B20_Init(void)
{
SETBIT(DS18B20_DDR, DS18B20_DATA);
CLRBIT(DS18B20_PORT, DS18B20_DATA);
TARGET_Delayus(900, 0);
SETBIT(DS18B20_PORT, DS18B20_DATA);
CLRBIT(DS18B20_DDR, DS18B20_DATA);
TARGET_Delayus(480, 0);
SETBIT(DS18B20_DDR, DS18B20_DATA);
}
void DS18B20_WriteOneByte(uint8 u8_Data)
{
uint8 i,m;
TARGET_CloseInterrupt();
SETBIT(DS18B20_DDR, DS18B20_DATA);
for(i = 0; i < 8; i++)
{
m = u8_Data & 0x01;
if(m == 0x01)
{
TARGET_Delayus(2, 0);
CLRBIT(DS18B20_PORT, DS18B20_DATA);
TARGET_Delayus(8, 0);
SETBIT(DS18B20_PORT, DS18B20_DATA);
TARGET_Delayus(85, 0);
}
else
{
TARGET_Delayus(2, 0);
CLRBIT(DS18B20_PORT, DS18B20_DATA);
TARGET_Delayus(85, 0);
SETBIT(DS18B20_PORT, DS18B20_DATA);
}
u8_Data = u8_Data >> 1;
}
TARGET_OpenInterrupt();
}
uint8 DS18B20_ReadOneByte(void)
{
uint8 temp,k,n;
TARGET_CloseInterrupt();
temp = 0;
for(n = 0; n < 8; n++)
{
SETBIT(DS18B20_DDR, DS18B20_DATA);
CLRBIT(DS18B20_PORT, DS18B20_DATA);
TARGET_Delayus(2, 0);
SETBIT(DS18B20_PORT, DS18B20_DATA);
CLRBIT(DS18B20_DDR, DS18B20_DATA);
TARGET_Delayus(1, 0);
k = DS18B20_PIN; //读数据,从低位开始
k &= DS18B20_DATA;
if (k)
{
temp |= (1 << n);
}
else
{
temp &= ~(1 << n);
}
_delay_us(100);
SETBIT(DS18B20_DDR, DS18B20_DATA);
SETBIT(DS18B20_PORT, DS18B20_DATA);
}
TARGET_OpenInterrupt();
return temp;
}
void DS18B20_ReadTemperature( uint8 *u8_Temperature )
{
uint8 teml, temh, temm, temh_d, teml_d, temm_d, count;
DS18B20_Init();
DS18B20_WriteOneByte(0xcc);
DS18B20_WriteOneByte(0x44);
TARGET_Delayms(200, 1);
DS18B20_Init();
DS18B20_WriteOneByte(0xcc);
DS18B20_WriteOneByte(0xbe);
teml = DS18B20_ReadOneByte();
temh = DS18B20_ReadOneByte();
temm = teml;
temm &= 0x0F;
teml >>= 4;
temh <<= 4;
count = teml | temh;
temh_d = count / 10;
temm_d = (temm * 6) / 10;
teml_d = count % 10;
u8_Temperature[0] = temh_d;
u8_Temperature[1] = teml_d;
u8_Temperature[2] = temm_d;
}
#endif |