?? main.c
字號:
/****************************************************************************
* 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 + -