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

📄 main.c

📁 RFID实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
* File :  main.c                                                            *
* COPYRIGHT BY HUOYAN LTD.COMPANY                                           *
* Version:      V1.2                                       			        *
*                                                                           *
* Created:      07.Aug.2006                                                 *
* Last Change:  25.DEC.2006                                                 *
*                                                                           *
* Author:       sun,nil                                                     *
*                                                                           *
* Compiler:     KEIL C51 V7.10                                              *
*                                                                           *
* Description:  AT89S52 Firmware for HY502  Serial Reader                   *
*   This project is designed for HY502XX                                    *
*   OSC use 11.0592MHz crystal.  										    *
****************************************************************************/

/*******************  www.ehuoyan.com *************************************/
/*-------------------------------------------------------------------------*/
#include "main.h" 
#include "hy502.h"

#define uchar	unsigned char
#define uint    unsigned int  

bit	 Cardinner=1;		// 卡在天线区状态 1 无卡,0有卡

bit ack;	//应答标志
uchar code cStatus1[]="Ok!";


uchar keytype=0x60;  //	 密钥标识:60为PICC_AUTHENT1A(61为PICC_AUTHENT1B)
uchar idata	  g_cBeeps=0x10;	 

bit           g_bReceCommandOk;	//flag of command receive OK
bit           g_bReceAA;		//flag of last received byte is "0xaa", used in interrupt
bit           g_bCard;			//flag of card in


/////////////////////////////////////////////////////////////////////////////////////////////////////
// 延时函数
void delay(unsigned int x)
{
	while(x)
	{
		x--;
	}
}

//////////**********IIC FUNCTION*********////////////////

/*****************************************************************************
*function: IIC start condition
*****************************************************************************/
void I2CStart(void)
{
	SDA = 1;
	SCL = 1;
	SDA = 0;
	_nop_();
	SCL = 0;
}
/*****************************************************************************
*function: IIC stop condition
*****************************************************************************/
void I2CStop(void)
{
	SCL = 0;
	SDA = 0;
	_nop_();
	SCL = 1;
	SDA = 1;
}
/*****************************************************************************
*function: IIC wait ACK
*****************************************************************************/
bit I2CWaitAck(void)
{
	unsigned char cErrTime = 255;
	SDA = 1;
	_nop_();
	SCL = 1;
	while(SDA)
	{
		cErrTime--;
		delay(10);
		if (0 == cErrTime)
		{
			I2CStop();
			return FAILURE;
		}
	}
	SCL = 0;
	return SUCCESS;
}
/*****************************************************************************
*function: IIC send ACK
*****************************************************************************/
void I2CSendAck(void)
{
	SDA = 0;
	_nop_();
	SCL = 1;
	SCL = 0;
}
/*****************************************************************************
*function: IIC send Not ACK
*****************************************************************************/
void I2CSendNotAck(void)
{
	SDA = 1;
	_nop_();
	SCL = 1;
	SCL = 0;
}
/*****************************************************************************
*function: send a byte over IIC bus
*****************************************************************************/
void I2CSendByte(unsigned char cSendByte)
{
	unsigned char data i = 8;
	while (i--)
	{
		SCL = 0;
		SDA = (bit)(cSendByte & 0x80);
		cSendByte += cSendByte;
		_nop_();
		SCL = 1;
	}
	SCL = 0;
}
/*****************************************************************************
*function: receive a byte over IIC bus
*****************************************************************************/
unsigned char I2CReceiveByte(void)
{
	unsigned char data i = 8;
	unsigned char data cR_Byte = 0;
	SDA = 1;
	while (i--)
	{
		cR_Byte += cR_Byte;
		SCL = 0;
		_nop_();  
		_nop_();
		SCL = 1;
		cR_Byte |= (unsigned char)SDA;
	}
	SCL = 0;
	return cR_Byte;
}
/*****************************************************************************
*function: read data from HY502 over IIC bus
*****************************************************************************/
unsigned char IicReadHY502(unsigned char *cP)
{
	unsigned char cCnt;
	unsigned char cCheckSum = 0;
	for (cCnt=0; cCnt<0xFF; cCnt++)	// wait HY502 working, while it's working, it will send "not ACK" to IIC master
	{
		delay(1);
		I2CStart();
		I2CSendByte(READ_HY502);
		if (I2CWaitAck() == SUCCESS)
		{
			break;
		}
	}
	if (0xFF == cCnt)
	{
		return FAILURE;
	}
	cP[0]=2;
	for (cCnt=0; cCnt<cP[0]; cCnt++)	// in the protocol, cP[0] is the length of this data package
	{
		cP[cCnt] = I2CReceiveByte();
		I2CSendAck();
		cCheckSum ^= cP[cCnt];
	}
	cP[cCnt] = I2CReceiveByte();
	I2CSendNotAck();
	I2CStop();
	if (cCheckSum != cP[cCnt])
	{
		return FAILURE;
	}
	else
	{
		return SUCCESS;
	}
}
/*****************************************************************************
*function: send data to HY502 over IIC bus
*****************************************************************************/
unsigned char IicSendHY502(unsigned char *cP)
{
	unsigned char i;
	unsigned char cCheckSum = 0;
	I2CStart();
	I2CSendByte(WRITE_HY502);
	if (I2CWaitAck() == SUCCESS)
	{
		for(i=0; i<cP[0]; i++)	// in the protocol, cP[0] is the length of this data package
		{
			cCheckSum ^= cP[i];
			I2CSendByte(cP[i]);
			if (I2CWaitAck() != SUCCESS)
			{
				return FAILURE;
			}
		}
		I2CSendByte(cCheckSum);
		if (I2CWaitAck() != SUCCESS)
		{
			return FAILURE;
		}		 
		I2CStop();
        return SUCCESS;
        }
	else
	{
		return FAILURE;
	}
}


/*****************************************************************************
*send command to Master over UART
*****************************************************************************/
void UartSend(unsigned char *cSendBuffer)
{
	unsigned char i;
	unsigned char cCheckSum;
	ES = 0;
	TI = 0;
	g_bReceCommandOk = 0;
	SBUF = 0xAA;
	while (!TI);
	TI = 0;
	SBUF = 0xBB;
	while (!TI);
	cCheckSum = 0;
	for (i=0; i<cSendBuffer[0]; i++)
	{
		cCheckSum ^= cSendBuffer[i];
		TI = 0;
		SBUF = cSendBuffer[i];
		while (!TI);
		if (cSendBuffer[i] == 0xAA)
//if there is a "0xAA" in the data serial but not the command header,
//add a "0x00" follow the "0xAA", CL (command length) will unchanged
		{
			TI = 0;
			SBUF = 0;
			while (!TI);
		}
	}
	TI = 0;
	SBUF = cCheckSum;
	while (!TI);
	TI = 0;
	ES = 1;
}
/*****************************************************************************
*serial interrupt routine
*****************************************************************************/
seri_int () interrupt 4 using 2
{
	static unsigned char i;
	static unsigned char cReceivedData;
	static unsigned char cCheckSum;
	if (RI)
	{
		cReceivedData = SBUF;
		RI = 0;
		if (g_bReceAA)
		{
			g_bReceAA = 0;
			if (0 != cReceivedData)
			{
				g_cReceNum = 0;
			}
		}
		else
		{
			if (0xAA == cReceivedData)
			{
				g_bReceAA = 1;
			}
			g_cReceBuf[g_cReceNum++] = cReceivedData;

			if (g_cReceNum > g_cReceBuf[0])
			{
				cCheckSum = 0;
				for (i=0; i <= g_cReceBuf[0]; i++)
				{
					cCheckSum ^= g_cReceBuf[i];
				}
				if (0 == cCheckSum)
				{
					g_bReceCommandOk = 1;
					ES = 0;
				}
				g_bReceAA = 0;
				g_cReceNum = 0;
			}
			if (g_cReceNum >= sizeof(g_cReceBuf))
			{
				g_cReceNum = 0;
				g_bReceAA = 0;
			}
		}
	}
	if (TI)
	{
		TI = 0;
	}
}

/*
uchar autocard(uchar *cardno)	 // 寻卡,防冲突,选择卡    返回状态,指针参数为返回的卡系列号(4 bytes)
{
	uchar cStatus;
	uchar cnt,i;
	uchar cp1[10];

	for(cnt=0;cnt<3;cnt++)
	{
		 cStatus =IicSendHY502(ComSearchCard);	  // 发送寻卡命令
	//	delay(1);// delay_10ms(2);					          // 延时等待模块执行命令
		 cStatus =IicReadHY502(cp1);		  // 读取卡号并存入到cp1
		 if(cStatus==SUCCESS)
		 {
		    if(cp1[1]==CARD_SN)       // 读卡号成功
		    {
		    	for(i=0;i<4;i++)	// 提取4字节卡号
		       {
		        	cardno[i]=cp1[i+2];
		       }  
		                       
		     
				return 0; 							
		    }
		  }
		  else cStatus=1;

	}   	
	
	return 1;
}
  */
// buzz freq

unsigned int g_cBeepDiv0;
unsigned char sp_freq=0xdc;
void timer0(void) interrupt 1 using 2	 //0.01s
{
	TH0=sp_freq;
	if(g_cBeepDiv0)   //beep
	{
		g_cBeepDiv0--;
	//	BUZ=(g_cBeepDiv0&0x0010);
	}
	 
}


///////////////////////////////////////////////////////////////////////
// 串口命令处理函数
///////////////////////////////////////////////////////////////////////
uchar uart_process(void)
{
	uchar cmd;	  
	uchar cStatus;	 
   
	cmd = g_cReceBuf[1];
	switch(cmd)
	{  

		case 0x11:	  // 软件掉电模式 ,1字节数据,非0退出软件掉电模式,0x00进入软件掉电
		                //任何一条对卡的操作命令或自动寻卡被设置后都将终止掉电模式
			if(g_cReceBuf[2]==0x01)
			{
		    
		            cStatus =IicSendHY502(AntOn);	  // 发送命令			         
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==SOFTDOWN))
					{
				    	SendBuffer[0]=0x02;
						SendBuffer[1]=cmd;
				
					}
			
			}

⌨️ 快捷键说明

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