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
? 發(fā)送溫度轉(zhuǎn)換 0xBE
? 寫EEPROM? 0x4E
*/
/////////////低層驅(qū)動(dòng)子程序///////////
void Init18B20(void); //初始化
void Write18B20(uchar ch);//寫數(shù)據(jù)
unsigned char Read18B20(void);//讀數(shù)據(jù)?
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;//整數(shù),小數(shù)高位,小數(shù)低位
uint? ResultSignal;//符號(hào)位
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);//發(fā)送溫度轉(zhuǎn)換指令
?? Delay10ms(25);
?? Delay10ms(25);
?? Delay10ms(25);
?? Delay10ms(25);//等待1s轉(zhuǎn)換
?? Init18B20();
?? Write18B20(0xCC);//跳過ROM
?? Write18B20(0xBE);//發(fā)送溫度轉(zhuǎn)換指令
?? 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";???? //負(fù)
??? }
???? 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);//至少延時(shí)480us
?? DQ="1";
?? Delay15(10);//至少延時(shí)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++)
?? {;}
?}
}
評(píng)論
查看更多