📄 temperature.c
字号:
#include "config.h"
extern void Delay_xus(uint xus);
extern uchar SCRATCHPAD_MAP[8];
//if return value euqal to 0, then get the DS18B20 presence pulse, else erro
uchar DS18B20_Reset(void)
{
uchar tmp;
DQ_OUTPUT;
DQ_L;
Delay_xus(550); //the parameter should be more than 480us
DQ_INPUT;
Delay_xus(90); //wait for DS18B20 delay, the delay should be more than 60us, and less than 120us
if((PINC&(1<<PC0)) == 0x00)
{
tmp = 0x00;
}
else tmp = 0x01;
Delay_xus(220); //wait for present delay, the value will be more than 300us
return tmp;
}
//extern uchar buf[16];
void Write0Slot(void)
{
DQ_OUTPUT;
DQ_L;
Delay_xus(80); //the parameter should be more than 60 and less than 120us
DQ_INPUT;
Delay_xus(2); //2us recovery
}
void Write1Slot(void)
{
DQ_OUTPUT;
DQ_L;
Delay_xus(2);
DQ_INPUT;
Delay_xus(80); //the parameter should be more than 60 and less than 120us
}
uchar ReadSlot(void)
{
uchar tmp; // read a bit
DQ_OUTPUT;
DQ_L;
Delay_xus(2);
DQ_INPUT;
Delay_xus(10);
if(PINC&(1<<PC0)) tmp = 0x01;
else tmp = 0x00;
Delay_xus(80); //the parameter should be more than 60us and less than 120us
return tmp;
}
void Transfer_Byte(uchar data)
{
uchar i, tmp;
tmp = data;
for(i=0; i<8; i++)
{
//if((tmp&0x80) == 0x80)
if((tmp&0x01) == 0x01)
{
Write1Slot();
}
else
{
Write0Slot();
}
//tmp = (tmp<<1);
tmp = (tmp>>1);
}
}
uchar DS18B20_Initialize(void)
{
uchar tmp, data, i, j;
tmp = DS18B20_Reset();
if(tmp != 0x00) return 1; //the DS18B20 is absent
Transfer_Byte(SKIP_ROM);
Transfer_Byte(WRITE_SCRATCHPAD);
Transfer_Byte(TH_LIMIT);
Transfer_Byte(TL_LIMIT);
Transfer_Byte(Config_REG);
tmp = DS18B20_Reset();
if(tmp != 0x00) return 1; //the DS18B20 is absent
Transfer_Byte(SKIP_ROM);
Transfer_Byte(READ_SCRATCHPAD);
for(j=0; j<8; j++)
{
data = 0;
for(i=0; i<8; i++)
{
tmp =ReadSlot();
if(tmp == 0x01) data |= 0x80;
//if(tmp == 0x01) data |= 0x01;
if(i != 0x07) data = data>>1;
//if(i != 0x07) data = data<<1;
}
SCRATCHPAD_MAP[j] = data;
}
tmp = DS18B20_Reset();
if(tmp != 0x00) return 1; //the DS18B20 is absent
Transfer_Byte(SKIP_ROM);
Transfer_Byte(COPY_SCRATCHPAD);
Delay_xus(5000); //delay 5ms
Delay_xus(5000); //delay 5ms
Delay_xus(5000); //delay 5ms
return 0;
}
uint Read_Temperature(void)
{
uchar tmp, i, j, data;
uint T_RESULT;
tmp = DS18B20_Reset();
if(tmp != 0x00) return 0xffff; //the DS18B20 is absent
Transfer_Byte(SKIP_ROM);
Transfer_Byte(CONVERT_T);
while(1) //waiting for the convert complishment
{
tmp = ReadSlot();
if(tmp == 0x01) break;
}
tmp = DS18B20_Reset();
if(tmp != 0x00) return 1; //the DS18B20 is absent
Transfer_Byte(SKIP_ROM);
Transfer_Byte(READ_SCRATCHPAD);
for(j=0; j<8; j++)
{
data = 0;
for(i=0; i<8; i++)
{
tmp =ReadSlot();
if(tmp == 0x01) data |= 0x80;
if(i != 0x07) data = data>>1;
}
SCRATCHPAD_MAP[j] = data;
}
T_RESULT = SCRATCHPAD_MAP[1];
T_RESULT = T_RESULT << 8;
T_RESULT |= SCRATCHPAD_MAP[0];
return T_RESULT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -