📄 18b20.c
字号:
#include"main.h"
#include"18b20.h"
#include"init.h"
static uchar read_18B20(void) ;
extern uint temperature ;
uint getemp(void)
{
uchar tempL ,tempH ;
static uint b1820 ;
while(init_bus() == 0x00) //等待总线初始化完成
;
_SEI() ; //打开全局中断
write_18B20(0xCC); //发送SKIP ROM指令
write_18B20(0x44); //发送温度转换指令
delay(62000) ; //100ms
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
delay(62000) ;
_WDR() ;
while(init_bus() == 0x00) //等待总线初始化完成
;
_SEI() ; //打开全局中断
write_18B20(0xCC); //发送SKIP ROM指令
write_18B20(0xBE); //发送读取存储器指令
NOP10
_WDR() ;
tempL = read_18B20(); //读取温度低字节
tempH = read_18B20() ; //读取温度高字节
// if(tempL == 255 || tempH >= 7 || (tempH == 0 && tempL == 0) )
if( tempH >= 7 || (tempH == 0 && tempL == 0) )
{
return(b1820);
}else
{
b1820 = 0 ;
b1820 = tempH ;
b1820 <<= 8 ;
b1820 = b1820 + (uint)tempL ;
return(b1820);
}
}
void write_18B20(uchar byte)
{
uchar i;
// _CLI() ; //关中断
for(i=0;i<8;i++)
{
// DQ=0;
TEMP_OUT ;
NOP10 ;
TEMP_RESET ;
NOP10 ; //延时13us左右
NOP10 ;
NOP10 ;
NOP10 ;
NOP1;
NOP1;
NOP1 ;
// DQ=byte&0x01;
if((byte & 0x01) == 0x01)
TEMP_SET ;
else
TEMP_RESET ;
byte = byte >> 1;
delay(25) ; //50US
//延时50us以上
// DQ=1;
TEMP_OUT ;
NOP10 ;
TEMP_SET ;
}
_SEI() ; //开中断
}
static uchar read_18B20(void)
{
uchar i ;
uchar temp ;
// _CLI() ; //关中断
for(i=0;i<8;i++)
{
temp = temp >> 1;
// DQ=0;
TEMP_OUT ;
NOP10 ;
TEMP_RESET ;
// _nop_(); //延时1us
NOP1 ;
NOP1 ;
NOP1 ;
NOP1 ;
// DQ=1;
TEMP_SET ;
TEMP_IN ;
NOP10; //5us
NOP10;
// if(DQ==0)
NOP10 ;
if(TEMP_READ == 0x80)
{
temp = temp | 0x80;
}
else
{
temp = temp & 0x7F ;
}
delay(20) ; //延时40us
delay(25) ; //延时50us
NOP10 ;
// DQ=1;
TEMP_OUT ;
NOP10 ;
TEMP_SET ;
}
_SEI() ; //开中断
return(temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -