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

📄 ds1820.c

📁 关于DSC1820温度传感器的单片机应用程序
💻 C
字号:
#include <REG932.h>          		/*头文件的包含*/
#include <intrins.h>

#define  uchar unsigned char		/*宏定义*/
#define  uint  unsigned int
#define  ulong  unsigned long
#define  _Nop()  _nop_()        /*定义空指令*/




/********************************************************************
延时n微秒,n<=255
********************************************************************/
void Delay_1us(uchar data us)
{
	do
	{
		us--;
	}
	while (us > 0);
}

/***********************************************************
从EEPROM读数据
************************************************************/
uchar read_eeprom(uchar data addr_rd)
{
	DEECON = 0x00;
	DEEADR = addr_rd;
	while((DEECON&0x80) == 0);
	DEECON = 0x00;
	return(DEEDAT);
}

/***********************************************************
向EEPROM写数据
************************************************************/
void write_eeprom(uchar data addr_wr,uchar data dat_wr)
{
	DEECON = 0x00;
	DEEDAT = dat_wr;
	DEEADR = addr_wr;
	while ((DEECON&0x80) == 0);
	DEECON = 0x00;
}

/***********************************************************
CCU初始化
************************************************************/
void CCU_init(void)
{
	OCB=1;
	OCC=1;
	TPCR2H=0x00;	// 
	TPCR2L=0x18;	// 设置CCU预分频系数为25
	TCR21=0x04;		// 设置锁相环输入为737280Hz
	CCCRB=0x01;		// PWM 输出通道 B
	CCCRC=0x01;		// PWM 输出通道 C
	
	TCR20=0x80;		// 启动 PLL, 输出方式: stop
	OCB=1;				// 等待一个周期
	OCC=1;
	while(PLEEN==0);// 等待 PLL 锁定

	TOR2H=0x13;		// 重装值 0x1388=5000
	TOR2L=0x88;		// 

//	TOR2H=0x09;		// 重装值 0x09c4=2500
//	TOR2L=0xc4;		// 
	TCR21|=0x80;	// 更新重装值

	OCRBH=0x00;		// 初始化占空比 B 
	OCRBL=0x00;			
	TCR21|=0x80;	// 更新占空比数据

	TCR20=0x82;		// 设置输出模式为不对称 PWM
}

/***********************************************************
电压控制
************************************************************/
void out_pwm(uint pwm_outB,uint pwm_outC)
{
	uchar data pwmh,pwml;

	pwmh = pwm_outB / 256;
	pwml = pwm_outB % 256;
	OCRBH = pwmh;
	OCRBL = pwml;

	pwmh = pwm_outC / 256;
	pwml = pwm_outC % 256;
	OCRCH = pwmh;
	OCRCL = pwml;
	TCR21 |= 0x80;
}



//////////////////////////////////////////////////////////////////////////
////////////////////总线写1时序控制函数///////////////////////////////////
void DS18B20_Write_1(void)
{
	P1 = 0x00;			//8个DQ 线全部设置为低电平
	Delay_1us(10);	//延时10us左右
	P1 = 0xff;			//8个DQ线全部输出高电平
	Delay_1us(30);	//延时30us左右
}

//////////////////////////////////////////////////////////////////////////
////////////////////总线写0时序控制函数///////////////////////////////////
void DS18B20_Write_0(void)
{
	P1 = 0x00;			//8个DQ 线全部设置为低电平
	Delay_1us(40);	//延时
	P1 = 0xff;			//端口恢复高电平
	Delay_1us(1);
}

//////////////////////////////////////////////////////////////////////////
////////////////////总线读取一个数据位时序控制函数////////////////////////
unsigned char DS18B20_ReadDQ(void)
{
	unsigned char DQ_S=0;
	P1 = 0x00;			//8个DQ 线全部设置为低电平
	Delay_1us(10);
	P1 = 0xff;			//端口置1,准备读取
	Delay_1us(1);		//延时待总线准备好数据
	DQ_S = P1;			//一次性读取8条DQ线的数据状态
	P1 = 0xff;			//恢复端口电平
	Delay_1us(30);	//延时
	return DQ_S;		//返回读取的值
}

//////////////////////////////////////////////////////////////////////////
////////////////////总线复位时序控制函数//////////////////////////////////
void DS18B20_Reset(void)
{
	unsigned char Error_Counter=0;
	P1 = 0x00;				//8个DQ 线全部设置为低电平
	Delay_1us(500);		//保持总线低电平500us
	P1 = 0xff; 
	Delay_1us(100); 
	if(P1!=0x00) B20_Error = P1;	//如检测到DS18B20总线响应了回复信号,则读取当前8条
																//总线的状态
	Delay_1us(50);
	P1 = 0xff; 
	for(Error_Counter=0;Error_Counter<200;Error_Counter++)
	{
		if((P1&(~B20_Error))==(~B20_Error)) break;	//如检测到总线的回复信号结
																								//束,则退出循环
		Delay_1us(1);
	}
	P1 = 0xff;				//恢复端口电平
	Delay_1us(200);		//延时 200us~~~
}

//////////////////////////////////////////////////////////////////////////
////////////////////写字节操作函数////////////////////////////////////////
void DS18B20_WriteByte(unsigned char Com)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		if(Com&0x01)
			DS18B20_Write_1();
		else
			DS18B20_Write_0();
			Com = Com>>1; 
	}
}

//////////////////////////////////////////////////////////////////////////
////////////////////读数据操作函数////////////////////////////////////////
unsigned char Read_buf_8ch[16];   //buffer of Read DS18B20
void DS18B20_Read2Byte(void)
{
	unsigned int i;
	for(i=0;i<16;i++)
	{
		Read_buf_8ch[i] = DS18B20_ReadDQ();
	}
}

//////////////////////////////////////////////////////////////////////////
////////////////////启动温度转换控制函数//////////////////////////////////
void DS18B20_Conver(void)
{
	DS18B20_Reset();
	DS18B20_WriteByte(0xcc);  //Skip ROM
	DS18B20_WriteByte(0x44);  //启动测温
}




读取温度值函数:
void DS18B20_ReadTemp(void)
{
 DS18B20_Reset();
 DS18B20_WriteByte(0xcc);   //Skip ROM
 DS18B20_WriteByte(0xbe);   //送入读取数据命令
 DS18B20_Read2Byte();
}

char i,j;
unsigned int uiData[8];
unsigned char Mask;
//OS the resoult of Temperature
for(i=15;i>=0;i--)
{
 Mask = 0x01;
 for(j=0;j<8;j++)
 {
  uiData[j] = uiData[j]<<1;
  if(Read_buf_8ch[i]&Mask) uiData[j]++;
  Mask = Mask<<1;
 }
}

/********************************************************************
  初始化、主控制
********************************************************************/
void main(void)
{
	AUXR1=0x00;
	
	P0M1=0x00;			//0000 0000	
	P0M2=0x00;			//0000 0000	准双向
	
	P1M1=0x00;			//0000 0000	
	P1M2=0x00;			//0000 0000 准双向
	
	P2M1=0x00;			//0000 0000	
	P2M2=0x00;			//0000 0000	准双向
	
	P3M1=0x00;			//0000 0000	
	P3M2=0x00;			//0000 0000 准双向

	P0   = 0xff;
	P1   = 0x3f;
	P2   = 0xff;
	P3   = 0xff;

	TH0 = 0x00;
	TL0 = 0x00;
	TH1 = 0x0b;
	TL1 = 0x0b;
	TMOD = 0x21;			//定时器1工作在8位自动重装模式
	TAMOD = 0x00;
	SP   = 0x7f;			//初始化堆栈
	
	CCU_init();














⌨️ 快捷键说明

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