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

📄 temp.c

📁 这是DS1620温度传感器的读写温度程序
💻 C
字号:
#include <REG2051.H>
#include <temp.h>
#include <intrins.h>

struct Frame frame; 


bit read_state()
{
	unsigned char i=0;
	unsigned char command=0xAC;
	bit state;
	
	
	RESET=0;
	CLK=1;
	_nop_();

	CLK=0;
	RESET=1;
	for(i=0;i<8;i++)
	{
		CLK=0;
		_nop_();
		_nop_();
		_nop_();
		DQ=(bit)(command&0x01);
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		CLK=1;
		command>>=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		
	}
	watchdog=(!watchdog);
	DQ=1;
	for(i=0;i<8;i++)
	{
		CLK=0;
		_nop_();
		_nop_();
		_nop_();
		state=DQ;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		CLK=1;
		watchdog=(!watchdog);
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	RESET=0;
	if(state)
	{
		return 1;
	}
	else
		return 0;

}
void read(unsigned char command1)
{
	unsigned char i=0;
	unsigned char command=command1;
	int back_up=0;
	bit data1=0;              //存贮读到的数据
	
	
	while(!read_state())        //可以用延时代替
	{
		debug=!debug;
	}
	//DelayMs(110);
	//watchdog=(!watchdog);
	
	RESET=0;
	CLK=1;
	_nop_();
	_nop_();
	RESET=1;
	CLK=0;
	

	for(i=0;i<8;i++)
	{
		CLK=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		DQ=(bit)(command&0x01);
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		
		CLK=1;
		command>>=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();

	}

	watchdog=(!watchdog);
	DQ=1;
	back_up=0;
	for(i=0;i<9;i++)
	{
		
		CLK=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		data1=DQ;
        if(data1)
        {
              back_up|=(0x01<<i);
        }
		CLK=1;
		_nop_();
        _nop_();
		_nop_();
		_nop_();
	}
	RESET=0;

	frame.info=back_up;
	DelayMs(1);
	
}
void write(unsigned char command,int t)
{

	unsigned char i=0;
	unsigned char command_copy=0;
	command_copy=command; // 将命令备份下来,以便于下面switch语句使用
	
	RESET=0;
	CLK=1;
	_nop_();
	_nop_();
	
	RESET=1;
	CLK=0;


	for(i=0;i<8;i++)
	{
		CLK=0;
		_nop_();
		_nop_();
		DQ=(bit)(command_copy&0x01);
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		CLK=1;
		command_copy>>=1;
		_nop_();
		_nop_();
		
	}

	switch(command)
	{
		
		case WRITE_CONFIG:
			command_copy=ISHOT;
			for(i=0;i<8;i++)
			{
				CLK=0;
				_nop_();
				_nop_();
				_nop_();
				_nop_();
				DQ=(bit)(command_copy&0x01);
				_nop_();
				_nop_();
				_nop_();
				_nop_();
				CLK=1;
				command_copy>>=1;
				_nop_();
				_nop_();
				_nop_();
				_nop_();

			}
			watchdog=(!watchdog);
			RESET=0;
		
			//延时10ms
			DelayMs(1);
			break;
		case (WRITE_TH||WRITE_TL):
			for(i=0;i<9;i++)
			{
				CLK=0;
				_nop_();
				_nop_();
				_nop_();
				_nop_();
				DQ=(bit)(t&0x01);
				_nop_();
				_nop_();
				_nop_();
				_nop_();				
				CLK=1;
				t>>=1;
				_nop_();
				_nop_();
				_nop_();
				_nop_();
				
			}
			RESET=0;
			watchdog=(!watchdog);
		  //延时10ms
			DelayMs(1);
			break;
				
	}
	RESET=0;
		
   //延时10ms
	DelayMs(1);
	return;
	
}
void init_ds1620()
{
	float f=0;
	int k=0;

	//set the continous wok mode
	write(WRITE_CONFIG,0);

	//set the high temperatrue
	f=40;
	k=((int)f*2)&0x01ff;
	write(WRITE_TH,0x0050);

	//set the low temperatrue
	f=-10;
	k=((int)f*2)&0x01ff;
	write(WRITE_TH,0x01ce);	
}

unsigned int CRC16(unsigned char *dt, int length)
{
	unsigned int crc = 0xffff, i, j, flag;
	for (i=0; i<length; i++)
	{

		crc = crc^dt[i];
		for (j=0; j<8; j++)
		{
		  flag = crc&1;
		  crc = crc>>1;
		  if (flag)
		    crc = crc^0xa001;
		}
	}
	watchdog=(!watchdog);
	return crc;
}


void send()
{
	/*wr_combuf[0]=frame.addr;
	wr_combuf[1]=frame.rorw;
	wr_combuf[2]=frame.instruction;
	wr_combuf[3]=(unsigned char )(frame.info>>8);
	wr_combuf[4]=(unsigned char )(frame.info&0x00FF);
	wr_combuf[5]=0x0;
	frame.crc16=CRC16(wr_combuf,6);
	wr_combuf[6]=(unsigned char )(frame.crc16>>8);
	wr_combuf[7]=(unsigned char )(frame.crc16&0x00ff);*/

	combuf[0]=frame.addr;
	combuf[1]=frame.rorw;
	combuf[2]=frame.instruction;
	combuf[3]=(unsigned char )(frame.info>>8);
	combuf[4]=(unsigned char )(frame.info&0x00FF);
	combuf[5]=0x0;
	frame.crc16=CRC16(combuf,6);
	combuf[6]=(unsigned char )(frame.crc16&0x00ff);
	combuf[7]=(unsigned char )(frame.crc16>>8);
	watchdog=(!watchdog);
	spi_send(combuf,8);
	
	
}

init_main()
{

	frame.addr=0x1;
	frame.rorw=0x1;
	frame.instruction=READ_TEMP;
	frame.info=0;
	frame.seq=0;
	frame.crc16=0;
}
void main()
{
	bit flag=0;
	int t=0;
	int backup=0;
	unsigned char lasttime=0;
	unsigned char curtime=0;
	init_main();
	init_spi();
	init_ds1620();
//send();           //for debug
	watchdog=(!watchdog);
	DelayMs(100);
	while(1)
	{
		write(START,0);			
		read(0xAA);
		backup=frame.info;      //this is very important

		if(timeover_flag==1)
		{
			DelayMs(5);
			if(counter>0)
			{
				//combuf[0]=counter;
				//spi_send(combuf,1);
				counter=0;
			}
		}
		watchdog=(!watchdog);
	    if(receive_ren)
		{
			flag=0;
			frame.crc16=CRC16(combuf,6);
			if(combuf[6]==(unsigned char )(frame.crc16&0x00ff))
			{
				if(combuf[7]==(unsigned char )(frame.crc16>>8))
				{
					if(combuf[0]==0x01)
					{
						frame.rorw=combuf[1];
						frame.instruction=combuf[2];
						frame.info=combuf[3]*256+combuf[4];
						flag=1;
					}
				}
				if(flag)
				{
					switch(frame.rorw)
					{
						case 0x01:          //上位机读取ds1620中的数据
							if(frame.instruction==0xAA)
							{
								frame.info=backup;
								send();
								break;
							}
							read(frame.instruction);
							send();
							break;
					    case  0x02:
							write(frame.instruction,frame.info);
							break;
								
					}
				}
				receive_ren=0;
			}
		}
		
	}
}

⌨️ 快捷键说明

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