下面是我的函数
分3个文件:头文件,DS18B20系列子函数文件,主函数部分(既数据处理和显示部分)
头文件
#ifndef __TEMP_H_
#define __TEMP_H_
#include<reg52.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit DSPORT=P2^2;
void Delay1ms(uint );
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
DS18B20系列子函数文件,主要是读写一字节和开始转换温度以及读取温度
#include"temp.h"
/*******************************************************************************
* : Delay1ms
*******************************************************************************/
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
/*******************************************************************************
* : Ds18b20Init
* : ʼ
* :
* : ʼɹ1ʧܷ0
*******************************************************************************/
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //-480us~960us
i = 70;
while(i--);
DSPORT = 1;
i = 0;
while(DSPORT)
{
Delay1ms(1);
i++;
if(i>5)//ȴ>5MS
{
return 0;
}
}

最低0.47元/天 解锁文章
1万+





