《温度传感器DS18B20驱动程序.doc》由会员分享,可在线阅读,更多相关《温度传感器DS18B20驱动程序.doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除#include ds18b20.h#include #if 0*FUNCTION NAME: InitDS18b20 *CREATE DATE: 2009/11/12 *CREATED BY: XS *FUNCTION: 复位DS18B20和检测存在脉冲 *MODIFY DATE: 2009/11/12 *INPUT: 无 *RETURN: 初始化成功,返回1,否则返回0 BOOL InitDs18b20(void) BOOL flag = FALSE;BIT bt;bt = ET0;ET0 = 0;DQ = 1; Delay70us(1);
2、/稍作延时DQ = 0; / 复位 Delay70us(8); /精确延时大于480usDQ = 1; /拉高总线,等待存在脉冲Delay70us(1);/ 60-75us之间if(0=DQ)flag = TRUE;Delay70us(8);/ 复位后大于480us延时ET0 = bt;return flag;*FUNCTION NAME: ReadByte *CREATE DATE: 2009/11/12 *CREATED BY: XS *FUNCTION: 从DS18B20中读取一个字节的数据 *MODIFY DATE: 2009/11/12 *INPUT: 无 *RETURN: 读取的数
3、据 UCHAR ReadByte(void)UCHAR i;UCHAR dat = 0;BIT bt;bt = ET0;ET0 = 0;for (i=0;i= 1;DQ = 0; Delay5us();DQ = 1; Delay5us();if(1=DQ)dat |=0x80;Delay70us(1);ET0 = bt; return(dat);*FUNCTION NAME: WriteByte *CREATE DATE: 2009/11/12 *CREATED BY: XS *FUNCTION: 向DS18B20中写入一个字节的数据 *MODIFY DATE: 2009/11/12 *INP
4、UT: 写入的数据 *RETURN: 无 void WriteByte(UCHAR wDat)UCHAR i;BIT bt;bt = ET0;ET0 = 0; for (i=0; i= 1;ET0 = bt;*FUNCTION NAME: ReadDs18b20 *CREATE DATE: 2009/11/12 *CREATED BY: XS *FUNCTION: 从DS18B20储存器中读取多字节数据 *MODIFY DATE: 2009/11/12 *INPUT: 读取数据存入的地址和需要读取数据的数量 *RETURN: 无 void ReadScrat(UCHAR *prDat,UCHA
5、R num)UCHAR i;for(i=0;i=ERR_COUNTER)/ 连续3次初始化失败,即认为DS18B20出现故障return 0;WriteByte(SKIP_ROM); / 跳过读序号列号的操作WriteByte(CONVERT_T); / 启动温度转换Delay70us(20); / 延时至少500us,以供温度转换while(1)if(InitDs18b20()counter = 0;break;elsecounter+;if(counter=ERR_COUNTER)/ 连续3次初始化失败,即认为DS18B20出现故障return 0;WriteByte(SKIP_ROM);
6、 /跳过读序号列号的操作WriteByte(READ_SCRAT); /读取温度寄存器等(共可读9个寄存器)ReadScrat(tempBuff, 9);if(0=CRC8(tempBuff,9)if(0=(tempBuff1&0x80)/为正温度tempDat = (tempBuff1&0x07)*0x100 + tempBuff0;elsetempDat = (0xFF-tempBuff1)&0x07)*0x100 + (0x100-tempBuff0);tempDat = -tempDat;*prTemperature = tempDat * 0.0625;return TRUE;ret
7、urn FALSE;*FUNCTION NAME: GetDS18B20Temperature *CREATE DATE: 2009/8/17 *CREATED BY: XS *FUNCTION: 采样3次求均值作为温度值 *MODIFY DATE: 2009/8/17 *INPUT: 温度储存的地址 *RETURN: 正常为1,故障为0 BOOL GetDS18B20Temperature(float *pgTemperature)UCHAR i;UCHAR counter = 0;float tempDat = 0;float tempVal = 0;for(i=0;i=ERR_COUNT
8、ER)/连续10次温度采样错误,则判断为故障,并返回return FALSE;*pgTemperature = tempVal / 3;/SendStr(boxTemp:);/print(*pgTemperature ,1);/SendChar(n);return TRUE;*FUNCTION NAME: CRC8 *CREATE DATE: 2009/8/17 *CREATED BY: XS *FUNCTION: CRC效验 *MODIFY DATE: 2009/8/17 *INPUT: 需要效验的数据地址和个数 *RETURN: 效验结果 UCHAR CRC8(UCHAR *point,U
9、CHAR CRClen) /效验成功则crc返回0 UCHAR i,j,crc_8,crcbuff; crc_8 = 0; for(i=0; iCRClen; i+) crcbuff=*point+; for(j=0;j= 1; else crc_8 = 0x18; crc_8 = 1; crc_8 |= 0x80; crcbuff = 1; return crc_8; *FUNCTION NAME: Delay5us *CREATE DATE: 2009/8/17 *CREATED BY: XS *FUNCTION: 延时5us *MODIFY DATE: 2009/8/17 *INPUT:
10、 无 *RETURN: 无 void Delay5us(void)/UCHAR i;/for(i=0;i7;i+);_nop_();_nop_();#endif*FUNCTION NAME: Delay70us *CREATE DATE: 2009/8/17 *CREATED BY: XS *FUNCTION: 延时70us *MODIFY DATE: 2009/8/17 *INPUT: 次数 *RETURN: 无 void Delay70us(UCHAR timers)UCHAR i,j;for(j=0;jtimers;j+)for(i=0;i22;i+)/ Delay5us();_nop_();_nop_();_nop_();【精品文档】第 9 页