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

📄 _485_funcs.c

📁 CAN工业节点设计、CAN控制器为SJA1000.C源码
💻 C
字号:
#include "xkcan_defs.h"
#include "_485_defs.h"
#include <regx52.h>

_t_485_tuoke_dh4_rx	idata rx485FrameTemp;
ubyte *				idata pRx485Frame;
ubyte				idata iRx485Frame;

_t_485_tuoke_dh4	idata tx485FrameTemp;
ubyte *				idata pTx485Frame;
ubyte				idata iTx485Frame;


/*unsigned int CRC(unsigned char *Buff,unsigned char Len)
{
	unsigned char i,j;
	unsigned char crc[2];
	crc[1]=0xff;			//Upper
	crc[0]=0xff;			//Lower
	for(i=0;i<Len;i++)
	{
		crc[0] = crc[0] ^ Buff[i];
			for(j=0;j<8;j++)
		{
			if((crc[0] & 0x01 )== 0x01 )
			{
				if((crc[1] & 0x01) == 0x01)
				{
					crc[0] >>=1;
					crc[0] |= 0x80;
					crc[1] >>=1;
				}
				else
				{
					crc[0] >>=1;
					crc[1] >>=1;
				}
				crc[1] ^= 0xA0;
				crc[0] ^= 0x01;
			}
			else
			{
				if((crc[1] & 0x01) == 0x01)
				{
					crc[0] >>=1;
					crc[0] |= 0x80;
					crc[1] >>=1;
				}
				else
				{
					crc[0] >>=1;
					crc[1] >>=1;
				}
			}
		}
	}
	return crc[1]*0x100+crc[0];
} */

void _485_Tuoke_DH4_CRC(ubyte * buff)
{
	ushort i, j;
	uint crc;
	
	crc = 0xffff;

	for ( i = 0; i < 5; i ++ )
	{
		crc = crc ^ (int)(buff[i]);

		for (j = 0; j < 8; j ++ )
		{
			if ( (crc & 0x01) == 0x01 )
			{
				crc >>= 1;
				crc ^= 0xA001;
			}
			else
			{
				crc >>= 1;
			}
		}
	}
	*(buff+6) = *(ubyte*)(&crc);
	*(buff+5) = *((ubyte*)(&crc) + 1);
}

ubyte _485_Tuoke_DH4_CRCCheck(ubyte * buff)
{
	ushort i, j;
	uint crc;
	
	crc = 0xffff;

	for ( i = 0; i < 5; i ++ )
	{
		crc = crc ^ (int)(buff[i]);

		for (j = 0; j < 8; j ++ )
		{
			if ( (crc & 0x01) == 0x01 )
			{
				crc >>= 1;
				crc ^= 0xA001;
			}
			else
			{
				crc >>= 1;
			}
		}
	}
	if (( *(buff+6) == *(ubyte*)(&crc) ) && (*(buff+5) == *((ubyte*)(&crc) + 1)) )
		return 1;
	else 
		return 0;
}


ubyte _485_GenerateReadFrame(ubyte stntype, ubyte stn, void * pFrame)
{
	if (stntype == INSTR_TUOKE_DH4)
	{
		((_t_485_tuoke_dh4 *)pFrame)->stn 	= stn;
		((_t_485_tuoke_dh4 *)pFrame)->pre[0]= INSTR_TUOKE_DH4_TX_PRE;
		((_t_485_tuoke_dh4 *)pFrame)->pre[1]= INSTR_TUOKE_DH4_TX_PRE;
		((_t_485_tuoke_dh4 *)pFrame)->pre[2]= INSTR_TUOKE_DH4_TX_PRE;
		((_t_485_tuoke_dh4 *)pFrame)->fc	= INSTR_TUOKE_DH4_READ_DATA;
		((_t_485_tuoke_dh4 *)pFrame)->addr	= INSTR_TUOKE_DH4_ADDR_DATA;
		((_t_485_tuoke_dh4 *)pFrame)->dat_L	= 0x00;
		((_t_485_tuoke_dh4 *)pFrame)->dat_H	= 0x00;
		((_t_485_tuoke_dh4 *)pFrame)->CRC_L	= 0x00;
		((_t_485_tuoke_dh4 *)pFrame)->CRC_H	= 0x00;

		((_t_485_tuoke_dh4 *)pFrame)->suf	= INSTR_TUOKE_DH4_TX_SUF;
		
 		_485_Tuoke_DH4_CRC((ubyte *)pFrame + 3);
	}
	else
	{
		//TODO:	Add codes support other types of intelligent instruments.
		//		Define the STNTYPE in _485_defs.h
		//		Define the struct _t_485_xxxx for the specific instrument.
	}
	return 0;
}

ubyte _485_Init()
{

	SCON = 	0x50;		// uart in mode 1 (8 bit), REN=1 
	TMOD = 	0x20 ;		// Timer 1 in mode 2 
	PCON |= 0x80;		// SMOD1 = 1 
	TH1	= 0xF3;			// 9600 Bds at 24MHz 
	TL1	= 0xF3;			// 9600 Bds at 24MHz 
	ES 	= 1; 			// Enable serial interrupt	
	TR1 = 1; 			// Timer 1 run 
	
	pRx485Frame = (ubyte *) &rx485FrameTemp;
	iRx485Frame = 0;
	pTx485Frame = (ubyte *) &tx485FrameTemp;
	iTx485Frame = 0;


	return 0;
}

void _485_SendData(ubyte stntype)
{
	if ( flag_uart_txc == 1 && flag_485_tx == 1 )
	{
		flag_uart_txc = 0;
		SBUF = *pTx485Frame++;
		iTx485Frame++;
		if (stntype == INSTR_TUOKE_DH4)
		{
			if ( iTx485Frame == INSTR_TUOKE_DH4_FRAME_LEN)
			{
				// If all were sent, clear flag_uart_send
				iTx485Frame = 0;
				pTx485Frame = (ubyte *) &tx485FrameTemp;
				flag_485_tx = 0;
			}
		}
	}
}

ubyte _485_ReceiveData()
{
	ubyte stntype;
	if ( flag_uart_rcv == 1 )	
	{
		flag_uart_rcv = 0;

	////////////////////////////////////////////////////////////////////
	// Here, the code is written for Tuoke DH4 inteligent instruments
	// If there're several kinds of instruments connected to the 485 bus,
	// Codes to determine who sent the data should be added here!!!!!!

		if (iRx485Frame < 3)
		{	// Check the prefix
			if (uart_data == INSTR_TUOKE_DH4_RX_PRE)
				iRx485Frame++;
			else
				iRx485Frame = 0;
		}
		else if (iRx485Frame < 10)
		{	// Prefix is fine, receive data
			iRx485Frame++;
			*pRx485Frame++ = uart_data;
		}
		else if (uart_data == INSTR_TUOKE_DH4_RX_SUF) 
		{	// Check the suffix
			flag_485_rcv = 1;
			pRx485Frame = (ubyte *) &rx485FrameTemp;
		 	iRx485Frame = 0;
		}
		else
		{	// 
			iRx485Frame = 0;
			pRx485Frame = (ubyte *) &rx485FrameTemp;
		}
	////////////////////////////////////////////////////////////////////////
	//	Code samples to determin the station type
	////////////////////////////////////////////////////////////////////////
	//	if (blabla)
	//	{
		stntype = INSTR_TUOKE_DH4;
		return stntype;
	//	}

	////////////////////////////////////////////////////////////////////////

	}
}

	

⌨️ 快捷键说明

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