⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20.c

📁 光电池LED自动照明控制系统的设计与实现
💻 C
字号:
//========================================================
//	文件名称:	DS18B20.c
//	功能描述:	DS18B20的相关操作,通过用户接口函数取得函数值
//	维护记录:	2005-11-22	v1.0
//========================================================

#include "DS18B20.h"
#include "SPCE061A.h"
#include	"SPLC501User.h"

//========================================================
//	语法格式:	void Delay(unsigned int uiTime)
//	实现功能:	延时,在调用Delay(1)时,延时时间小于1us
//	参数:		uiTime
//	返回值:	无
//========================================================

void Delay(unsigned int uiTime)
{
	while(uiTime > 0)
	{
		uiTime -= 1;
	}
}

//========================================================
//	语法格式:	void Set_DQ(int Dir)
//	实现功能:	改变IOB15口的状态
//	参数:		Dir,1:IOB15输出1;0:IOB15悬浮输入
//	返回值:	无
//========================================================

void Set_DQ(int Dir)
{
	if(Dir > 0)
		*P_IOB_Dir |= 0x8000;
	else
		*P_IOB_Dir &= 0x7fff;
	*P_IOB_Buffer |= 0x8000;
}

//========================================================
//	语法格式:	void Clr_DQ(int Dir)
//	实现功能:	改变IOA15口的状态
//	参数:		Dir,1:IOB15输出0;0:IOB15悬浮输入
//	返回值:	无
//========================================================

void Clr_DQ(int Dir)
{
	if(Dir > 0)
		*P_IOB_Dir |= 0x8000;
	else
		*P_IOB_Dir &= 0x7fff;
	*P_IOB_Buffer &= 0x7fff;
}

//========================================================
//	语法格式:	unsigned int Read_DQ(void)
//	实现功能:	读取DQ的值
//	参数:		无
//	返回值:	DQ的值
//========================================================

unsigned int Read_DQ(void)
{
	unsigned int uiTemp;
	uiTemp = 0;
	if((*P_IOB_Data & 0x8000) > 0)
		uiTemp = 1;
	return(uiTemp);
}

//========================================================
//	语法格式:	unsigned int Read_18B20_Byte(void)
//	实现功能:	读取18B20的值
//	参数:		无
//	返回值:	18B20的值
//========================================================

unsigned int Read_18B20_Byte(void)
{
	int i;
	unsigned int Data;
	Data = 0;
	for(i=0;i<8;i++)
	{
		Set_DQ(1);
		Delay(1);
		Clr_DQ(1);
		Delay(2);
		Set_DQ(0);			//切为输入
		Data = Data >> 1;
		if(Read_DQ()>0)
			Data |= 0x0080;
		Delay(31);
	}
	return(Data);
}

//========================================================
//	语法格式:	void Write_18B20_Byte(unsigned int Data)
//	实现功能:	写18B20
//	参数:		要写的字节
//	返回值:	无
//========================================================

void Write_18B20_Byte(unsigned int Data)
{
	int i;
	Set_DQ(1);
	Delay(1);
	for(i=0;i<8;i++)
	{
		Clr_DQ(1);
		Delay(1);
		if((Data&0x0001)>0)
			Set_DQ(1);
		Data = Data >> 1;
		Delay(31);
		Set_DQ(1);
		Delay(1);	
	}
}

//========================================================
//	语法格式:	int Init_18B20(void)
//	实现功能:	初始化18B20
//	参数:		要写的字节
//	返回值:	无
//========================================================

int Init_18B20(void)
{
	int flag;
	Set_DQ(1);
	Delay(1);
	Clr_DQ(1);
	Delay(250);
	Set_DQ(0);
	Delay(31);
	if(Read_DQ()>0)
		flag = 1;
	else
	{
		flag = 0;
		Delay(220);
	}
	Set_DQ(1);
	return(flag);
}

//========================================================
//	语法格式:	unsigned int Read_Temp(void)
//	实现功能:	读取温度
//	参数:		无
//	返回值:	温度值
//========================================================

unsigned int Read_Temp(void)
{
	int i;
	unsigned int uiT;
	unsigned int Data[10];
	
	Set_DQ(1);
	while(Init_18B20()>0)
		*P_Watchdog_Clear = 0x0001;
	Delay(40);
	Write_18B20_Byte(0xcc);
	Write_18B20_Byte(0x44);
	for(i=0;i<26;i++)
		Delay(15000);					//长时间延时
	while(Init_18B20()>0)
		*P_Watchdog_Clear = 0x0001;;
	Write_18B20_Byte(0xcc);
	Write_18B20_Byte(0xbe);
	for(i=0;i<8;i++)					//数据读取
		Data[i] = Read_18B20_Byte();
	i = Data[1];						//温度的高字节
	i = i << 8;
	uiT = Data[0];						//温度的低字节
	uiT |= i;							//高低字节组和,有效位数11位
	Init_18B20();
	return uiT;
}
//========================================================
//	语法格式:	void Display(unsigned int)
//	实现功能:	显示,将温度显示在1602液晶屏上
//	参数:		要显示的温度
//	返回值:	无
//========================================================
	
void Display(unsigned int uiData)
{   int m;
	unsigned int Data[10] = {0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039};
	
	unsigned int uiInteger;					//数据的整数部分
	unsigned int uiDecimal;					//数据的小数部分
	unsigned int uiBai;						//显示温度的百位
	unsigned int uiShi;						//显示温度的十位
	unsigned int uiGe;						//显示温度的个位
	unsigned int uiShi_d;					//显示温度的十分位
	unsigned int uiBai_d;					//显示温度的百分位
	unsigned int uiQian_d;					//显示温度的千分位
	unsigned int uiResidue;					//数据的余数,作除法
	
	uiInteger = 0;
	uiDecimal = 0;
	
	if(uiData & 0x0400)
		uiInteger += 64;					//转换为整数部分
	if(uiData & 0x0200)
		uiInteger += 32;
	if(uiData & 0x0100)
		uiInteger += 16;
	if(uiData & 0x0080)
		uiInteger += 8;
	if(uiData & 0x0040)
		uiInteger += 4;
	if(uiData & 0x0020)
		uiInteger += 2;
	if(uiData & 0x0010)
		uiInteger += 1;
	
	if(uiData & 0x0008)
		uiDecimal += 5000;					//转换为小数部分
	if(uiData & 0x0004)
		uiDecimal += 2500;
	if(uiData & 0x0002)
		uiDecimal += 1250;
	if(uiData & 0x0001)
		uiDecimal += 625;
	
	uiBai = uiInteger/100;					//小数点前面部分
	uiResidue = uiInteger%100;
	uiInteger = uiResidue;
	uiShi = uiInteger/10;
	uiResidue = uiInteger%10;
	uiGe = uiResidue;
	
	uiShi_d = uiDecimal/1000;				//小数点后面部分
	uiResidue = uiDecimal%1000;
	uiDecimal = uiResidue;
	uiBai_d = uiDecimal/100;
	uiResidue = uiDecimal%100;
	uiDecimal = uiResidue;
	uiQian_d = uiDecimal/10;
	uiResidue = uiDecimal%10;				//万分位
	
	//显示
	//LCD数据更新
 			m = LCD501_GetPaintMode();						//获取当前图象叠加模式
			LCD501_SetPaintMode(2);							//设置为清除模式
			LCD501_Rectangle(68,17,127,33,PAINT_SOLID);		//清除数据显示区
			LCD501_SetPaintMode(m);							//图象叠加模式恢复
	
//	LCD501_PutChar(68,40,(Data[uiBai])); 
	LCD501_PutChar(68,17,(Data[uiShi]));
	LCD501_PutChar(78,17,(Data[uiGe]));
	LCD501_PutChar(86,17,'.');
	LCD501_PutChar(96,17,(Data[uiShi_d]));
	LCD501_PutChar(106,17,(Data[uiBai_d]));
	LCD501_PutChar(116,17,(Data[uiQian_d]));
	
//	LCD501_PutChar(98,40,('0'+Data[uiGe]));
//	LCD501_PutChar(98,40,('0'+2));
/*	
	Write_Command(0x00c7);					//设置显示地址
	if(uiBai)
	Write_Data(Data[uiBai]);
	Write_Data(Data[uiShi]);
	Write_Data(Data[uiGe]);
	Write_Data('.');
	Write_Data(Data[uiShi_d]);
	Write_Data(Data[uiBai_d]);
	Write_Data(Data[uiQian_d]);
	Write_Data(Data[uiResidue]);
	Write_Data(0x005e);
	Write_Data('C');
*/

   

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -