18b20 C程序
//将DQ与Vcc之间接入4.7K上拉电阻
#include "reg52.h"
#include "INTRINS.H"
#include "1602.c"
#define uchar unsigned char
#define uint unsigned int
//#define CLEARSCREEN LCD_en_command(0x01) 1602清屏
sbit DQ="P1"^0;
/*操作命令代码
跳过ROM 0xCC
发送温度转换 0xBE
写EEPROM 0x4E
*/
/////////////低层驱动子程序///////////
void Init18B20(void); //初始化
void Write18B20(uchar ch);//写数据
unsigned char Read18B20(void);//读数据
void Delay15(uchar n);
void Delay10ms(uint delay_num);
void Display(void);
/////////////////////////////////////
code uchar decimalH[]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
code uchar decimalL[]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
uint ResultTemperatureH ,ResultTemperatureLH,ResultTemperatureLL;//整数,小数高位,小数低位
uint ResultSignal;//符号位
main()
{ uint TemH,TemL,delay,k=0;
for(; ; )
{
Init18B20();
Write18B20(0xCC);//跳过ROM
_nop_();
//Write18B20(0x4E);//写EEPROM
// Write18B20(0x00);//Th
//Write18B20(0x00);//Tl
Write18B20(0x7f);//12 bits温度分辨率
Init18B20();
Write18B20(0xCC);//跳过ROM
_nop_();
Write18B20(0x44);//发送温度转换指令
Delay10ms(25);
Delay10ms(25);
Delay10ms(25);
Delay10ms(25);//等待1s转换
Init18B20();
Write18B20(0xCC);//跳过ROM
Write18B20(0xBE);//发送温度转换指令
TemL="Read18B20"(); //读低位温度值
TemH="Read18B20"(); //读高位温度值
Delay10ms(2);
TemH=(TemH<<4)|(TemL>>4);
TemL="TemL"&0x0f; //取低4位
if(TemH&0x80)
{ TemH=~TemH;
TemL=~TemL+1;
ResultSignal="1"; //负
}
else ResultSignal="0"; //正
ResultTemperatureLH="decimalH"[TemL];
ResultTemperatureLL="decimalL"[TemL];
ResultTemperatureH="TemH";
Display();
for(delay=0;delay<60000;delay++);
for(delay=0;delay<20000;delay++);
}
}
void Display(void)
{ uint i,j,q;
LCD_init();
CLEARSCREEN;
LCD_en_command(0x01);
delay_nms(2);
q="ResultTemperatureH/100";
i=(ResultTemperatureH%100)/10;
j="ResultTemperatureH-"(i*10+q*100);
LCD_write_string(0,LINE1," Jaq1217 18B20 ");
if(ResultSignal)
{ LCD_write_string(0,LINE2," T is:- . "); }
else {LCD_write_string(0,LINE2," T is:+ . ");}
LCD_write_char(0x07,LINE2,q|0x30);
LCD_write_char(0x08,LINE2,i|0x30);
LCD_write_char(0x09,LINE2,j|0x30);
LCD_write_char(0x0b,LINE2,(ResultTemperatureLH/10)|0x30);
LCD_write_char(0x0c,LINE2,(ResultTemperatureLH%10)|0x30);
LCD_write_char(0x0d,LINE2,(ResultTemperatureLL/10)|0x30);
LCD_write_char(0x0e,LINE2,(ResultTemperatureLL%10)|0x30);
}
unsigned char Read18B20(void)
{ unsigned char ch;
unsigned char q ;
for(q=0;q<8;q++)
{ch=ch>>1;
DQ="0";
_nop_();
DQ="1";
_nop_();_nop_();_nop_();_nop_();
if(DQ==1)
{ch="ch|0x80";}
else {ch="ch"&0x7f;}
Delay15(3);
DQ="1";
}
return (ch);
}
void Write18B20(uchar ch)
{ uchar i;
for(i=0;i<8;i++)
{ DQ="0";
Delay15(1);
DQ="ch"&0x01;
Delay15(3);
DQ="1";
ch="ch">>1;
_nop_();
}
}
void Init18B20(void)
{ DQ="0";
Delay15(33);//至少延时480us
DQ="1";
Delay15(10);//至少延时100us
/*if(DQ==1) return 0; //初始化失败
else return 1;
DQ=1; Delay15(18); */
}
void Delay15(uchar n)
{ do{ _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_();
n--;
}while(n);
}
void Delay10ms(uint delay_num)
{
uchar i;
while(delay_num--)
{
for(i=0;i<125;i++)
{;}
}
}
评论
查看更多