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

📄 test.c

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

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

void rt12864_initialization(void);
void rt12864_sixteen_int(uchar row,uchar tier,uint dt);
void dally_timer1(uint data ms);


uint xdata ds18b20_dat[];



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

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

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

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

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

//////////////////////////////////////////////////////////////////////////
////////////////////写字节操作函数////////////////////////////////////////
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;

	SP = 0x4F;
	
	rt12864_initialization();
	dally_timer1(40);

	while (1)
	{
		rt12864_sixteen_int(0,0,uint dt);
	}
}













⌨️ 快捷键说明

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