📄 main.c
字号:
/****************************************************************************
* File : main.c *
* COPYRIGHT BY HUOYAN LTD.COMPANY *
* Version: V2.0 *
* *
* 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"
// SPI 总线状态定义
#define SPI_RDY 0xF0; // 准备
#define spibusy 0xaa // 忙
#define spiread 0xbb // 读
#define spiwrite 0xcc // 写
/////////////////////////////////////////////////////////////////////////////////////////////////////
// 延时函数
void delay(unsigned int x)
{
while(x)
{
x--;
}
}
///////////////////////////////////////////////////////////////////////
// Delay 50us
///////////////////////////////////////////////////////////////////////
void delay_50us(uchar _50us)
{
RCAP2LH = RCAP2_50us;
T2LH = RCAP2_50us;
ET2 = 0; // Disable timer2 interrupt
T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
while (_50us--)
{
while (!TF2);
TF2 = FALSE;
}
TR2 = FALSE;
}
///////////////////////////////////////////////////////////////////////
// Delay 1ms
///////////////////////////////////////////////////////////////////////
void delay_1ms (uint _1ms)
{
RCAP2LH = RCAP2_1ms;
T2LH = RCAP2_1ms;
ET2 = 0; // Disable timer2 interrupt
T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
while (_1ms--)
{
while (!TF2);
TF2 = FALSE;
}
TR2 = FALSE;
}
///////////////////////////////////////////////////////////////////////
// Delay 10ms
///////////////////////////////////////////////////////////////////////
void delay_10ms(unsigned int _10ms)
{
RCAP2LH = RCAP2_10ms;
T2LH = RCAP2_10ms;
ET2 = 0; // Disable timer2 interrupt
T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
while (_10ms--)
{
while (!TF2)
{
if(g_bReceCommandOk) // change by lin 2006-08-21
{
TR2 = FALSE;
TF2 = FALSE;
return;
}
}
TF2 = FALSE;
}
TR2 = FALSE;
}
/////////////////////////////////////////////////////////////////////////
//beep(n);n为鸣叫的声音次数
/////////////////////////////////////////////////////////////////////////
void beep(uchar n)
{
uchar i;
for(i=0;i<n;i++)
{
BUZ=0; //产生周期为1s的方波
delay_10ms(50); //delay 0.5s
BUZ=1;
delay_10ms(50);
BUZ=0;
}
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//LED闪烁次数
////////////////////////////////////////////////////////////////////////
void splash(uchar n)
{
uchar i;
for(i=0;i<n;i++)
{
LED_YELLOW=1;
delay_10ms(50); //delay 0.5s
LED_YELLOW=0;
delay_10ms(50);
LED_YELLOW=1;
}
}
/******************* SPI FUNCTION DEFINE ************************************/
/*****************************************************************************
*function: send a byte over SPI bus
发送一个字节
*****************************************************************************/
unsigned char SPIRWByte(unsigned char cSendByte)
{
unsigned char data i = 8;
unsigned char cRecByte;
while (i--)
{
cRecByte += cRecByte;
SCL = 0;
MOSI = (bit)(cSendByte & 0x80);
cSendByte += cSendByte;
cRecByte |= (unsigned char) MISO ;
SCL = 1;
}
SCL = 1;
return cRecByte;
}
/***** 查询SPI总线状态 *********/
unsigned char spi_cStatus(void)
{
unsigned char cStatus;
NSS=0;
cStatus=SPIRWByte(spibusy);
cStatus=SPIRWByte(0xFF);
NSS=1;
return cStatus;
}
/***** 读SPI总线数据 *********/
unsigned char SPI_Read(unsigned char *cP)
{
unsigned char cCnt,cStatus;
unsigned char cCheckSum = 0;
for (cCnt=0; cCnt<100; cCnt++)
{
cStatus=spi_cStatus(); //查询SPI总线状态
if(cStatus==0xF0)
{
cCnt=253;
}
delay(10);
}
if(cCnt==254) //卡操作结束,可以回传数据
{
NSS=0;
cCnt=SPIRWByte(spiread);
cP[0]=2;
for (cCnt=0; cCnt<cP[0]; cCnt++)
{
cP[cCnt] = SPIRWByte(0xFF);
cCheckSum ^= cP[cCnt];
if(cP[0]>32)
{
NSS=1;
return FAILURE;
}
}
cP[cCnt] = SPIRWByte(0xFF);
NSS=1;
if (cCheckSum == cP[cCnt])
{
return SUCCESS;
}
}
return FAILURE;
}
/***** 写SPI总线数据 *********/
unsigned char SPI_Write(unsigned char *cP)
{
unsigned char i,cStatus;
unsigned char cCheckSum = 0;
NSS=0;
cStatus=SPIRWByte(spiwrite);
for(i=0; i<cP[0]; i++) // cP[0] 表示数据包的长度
{
cCheckSum ^= cP[i];
cStatus=SPIRWByte(cP[i]);
}
cStatus=SPIRWByte(cCheckSum);
NSS=1;
return cStatus;
}
/******************* serial function ****************************************/
/*****************************************************************************
*send command to Master over UART
*****************************************************************************/
void UartSend(unsigned char *cg_cReceBuf)
{
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<cg_cReceBuf[0]; i++)
{
cCheckSum ^= cg_cReceBuf[i];
TI = 0;
SBUF = cg_cReceBuf[i];
while (!TI);
if (cg_cReceBuf[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 uart_process(void)
{
uchar cmd;
uchar cStatus;
uchar temp;
cmd = g_cReceBuf[1];
switch(cmd)
{
case 0x01: // 读设备型号 8字节
cStatus =SPI_Write(g_cReceBuf); // 发送命令
delay_1ms(2); // 延时等待模块执行命令
cStatus =SPI_Read(cp); // 读取并存入到cP
if((cStatus==SUCCESS)&&(cp[1]==PCD_PN))
{
g_cReceBuf[0]=0x0a;
g_cReceBuf[1]=cmd;
memcpy(&g_cReceBuf[2],&cp[2],8);
}
else
{
g_cReceBuf[0]=0x02;
g_cReceBuf[1]=~cmd;
cStatus=1;
}
break;
case 0x02: // 读设备序列号 4字节
cStatus =SPI_Write(g_cReceBuf); // 发送命令
delay_1ms(2); // 延时等待模块执行命令
cStatus =SPI_Read(cp); // 读取并存入到cP
if((cStatus==SUCCESS)&&(cp[1]==PCD_SN))
{
g_cReceBuf[0]=6;
g_cReceBuf[1]=cmd;
memcpy(&g_cReceBuf[2],&cp[2],4);
}
else
{
g_cReceBuf[0]=0x02;
g_cReceBuf[1]=~cmd;
cStatus=1;
}
break;
case 0x10: // 读PCD固件版本号 4字节
cStatus =SPI_Write(g_cReceBuf); // 发送命令
delay_1ms(2); // 延时等待模块执行命令
cStatus =SPI_Read(cp); // 读取并存入到cP
if((cStatus==SUCCESS)&&(cp[1]==FW_REL))
{
g_cReceBuf[0]=6;
g_cReceBuf[1]=cmd;
memcpy(&g_cReceBuf[2],&cp[2],4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -