您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > DS18B20------C51库文件
//Îĵµºó׺Ϊ.TXT,ʵ¼ÊʹÓÃÐ޸ĺó׺Ϊ.H£¬²¢ÔÚmainÎļþÖÐʹÓÃ#includeds18b20.h//MCU=89S52;XTAL=12MHZ;ʵ¼Ê²âÊÔת»»ËÙÂÊ9.5ms£¬Î¶ÈÊýֵδ·¢ÏÖÒì³££¨ÔʼĬÈÏÖµ12λ·Ö±æÂÊ£©#ifndef_DS18B20_h_#define_DS18B20_h_#includereg52.hsbitDIO=P0^0;//*µ¥×ÜÏß½Ó¿Ú,ÉÏÀ4K7£¬µ¥¶Ë¿ªÂ©////////////////////////////////////////voidds18b20delay_us(unsignedchar);voidds18b20_inital(void);voidds18b20_write(unsignedchar);unsignedchards18b20_read(void);unsignedintds18b20_tempvolum(void);//*·µ»ØÕûÐÍDS18B20ζÈÖµ£¬Êµ¼Êµ÷ÓÃÐëÅжÏÕý¸º¼°³Ë0.0625¡ãC////////////////////////////////////////*******************************ÑÓʱ*/voidds18b20delay_us(unsignedchari)//ÑÓʱ=(12/xtal_MHz)*(5+2*i)us£¬¾§Õñ²îÒ죬×ÔÐмÆËãµ÷Õû{while(--i);}/*******************************³õʼ»¯*/voidds18b20_inital(void){unsignedchari=1;while(i){DIO=1;DIO=0;ds18b20delay_us(255);//515us(t480us)DIO=1;ds18b20delay_us(5);//15us(t=15~60us)if(DIO){i=0;}ds18b20delay_us(75);//155us(t=60~240us)DIO=1;}}/**********************************дROM*/voidds18b20_write(unsignedcharir){unsignedchari;for(i=0;i8;i++){DIO=0;ds18b20delay_us(5);//15usif((ir&0x01)){DIO=1;}ir=ir1;ds18b20delay_us(20);//45usDIO=1;}}/*************************************¶ÁREG*/unsignedchards18b20_read(void){unsignedchari,temperature=0;for(i=0;i8;i++){temperature=temperature1;DIO=0;DIO=1;ds18b20delay_us(5);//15usif(DIO){temperature=temperature|0X80;}ds18b20delay_us(20);//45us}return(temperature);}/***************************************ȡζÈÖµ*/unsignedintds18b20_tempvolum(void){unsignedcharswap=0;unsignedintdat=0;ds18b20_inital();ds18b20_write(0xcc);//skipromds18b20_write(0x44);//tempcovDIO=0;DIO=1;ds18b20delay_us(5);//15uswhile(!DIO);ds18b20_inital();ds18b20_write(0xcc);//skipromds18b20_write(0xbe);//readregswap=ds18b20_read();//LSBdat=ds18b20_read();//HSBdat=dat8;dat=dat|swap;return(dat);}#endif
本文标题:DS18B20------C51库文件
链接地址:https://www.777doc.com/doc-6509978 .html