/*******************************************************************************
UART接口一帧的数据格式为 1个起始位,8个数据位,1个地址/数据标志位,1个停止位。
波特率:19200,方式3,11位格式。
发送数据封包格式:(注:模块地址为地址帧第9位数据为1,其他的为数据帧第9位数据为0)
数据包内容:模块地址(2B) 长度字(1B) 命令字(1B) 数据域(nB) 校验字(1B)
模块地址:对于单独使用的模块来说固定为0x00;
对网络版模块来说为0x01~0XFE;
0xFF为广播(不回答)。
注:模块地址为完全相同的2字节.
长度字:指明从长度字到数据域最后一字节的字节数(包括长度字)
命令字:本条命令的含义
数据域:该条命令的内容,此项可以为空
校验字:从长度字到数据域最后一字节的逐字节异或值(最后一字节)。
返回数据封包格式:(注:所有字节的第9位数据为0)
数据包内容:命令头 长度字 命令字 执行结果 数据域 校验字
命令头: 0xAA 0x55,若后续数据中包含0xAA则随后补充一字节0x00以区分命令头但长度字不增加
长度字: 指明从长度字到数据域最后一字节的字节数
命令字: 执行正确为接收到的命令字;
执行错误为接收到的命令字逐位取反;
数据域: 该条命令返回的内容
校验字:从长度字到数据域最后一字节的逐字节异或值(最后一字节)。
*******************************************************************************/
#include "main.h"
unsigned char g_cReceBuf[43]; //接收缓冲区
unsigned char g_cReceNum; //接收字节计数
bit g_bReceCommandOk; //接收命令正确标志
bit g_bReceAA; //接收的数为 "0xaa", 记下标志
bit g_bCard; //卡片进入射频场标志
unsigned char code ComHaltMCU[]
= {0x02,
COMM_SET_MCU_IDLE,
};
unsigned char ComSet[]
= {0x03,
COMM_CONTROL_ANTENNA,
0x01};
//设置打开天线和关闭自动循卡
unsigned char code ComSearchCard[]
= {0x03,
COMM_MIFARE_SEARCH_CARD,
0x00};
//循卡后读出卡号
unsigned char cReadBlock[13]
= {0x0A,
COMM_READ_BLOCK,
0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
//读块No.4
unsigned char code ComReadBlock4[]
= {0x0A,
COMM_READ_BLOCK,
0x00, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
//读块No.6
unsigned char code ComReadBlock6[]
= {0x0A,
COMM_READ_BLOCK,
0x00, 0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
//写块No.4
unsigned char code ComWriteBlock4[]
= {0X1A,
COMM_WRITE_BLOCK,
0x00, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
//初始化钱包No.5 的值为0x12345678
unsigned char code ComIntiPurse5[]
= {0x0E,
COMM_INIT_PURSE,
0x00, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff , 0x78, 0x56, 0x34, 0x12};
//读钱包的值No.6
unsigned char code ComReadPurse6[]
= {0x0A,
COMM_READ_PURSE,
0x00, 0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
//钱包值加2
unsigned char code ComIncrPurse5[]
= {0x0E,
COMM_INC_VALUE,
0x00, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00};
//钱包值减1
unsigned char code ComDecrPurse5[]
= {0x0E,
COMM_DEC_VALUE,
0x00, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00};
// restore purse in block No.5 to block No.6
unsigned char code ComBakPurse56[]
= {0x0B,
COMM_BAK_PURSE,
0x00, 0x05, 0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
// 卡片休眠
unsigned char code ComHaltCard[]
= {0x02,
COMM_CARD_HALT,
};
// 钱包在 INC & DEC 后的结果
unsigned char code pursevalue[]
= {0x79,0x56,0x34, 0x12};
unsigned char code ComClrPA[]
= {0X03 ,
COMM_PA_OUT_CLR ,
0XFF};
unsigned char code ComSetPA[]
= {0X03 ,
COMM_PA_OUT_SET ,
0XFF};
unsigned char code ComClrPB[]
= {0X03 ,
COMM_PB_OUT_CLR ,
0X1F};
unsigned char code ComSetPB[]
= {0X03 ,
COMM_PB_OUT_SET ,
0X1F};
void Delay100uS(unsigned int i)
{
unsigned char m;
while(i--)
{
for(m = 0; m < 13; m++)
{
LED_zhishi = CARDPIN;
}
}
}
void main()
{
unsigned char i;
unsigned char cStatus;
InitializeSystem();
i=16;
BEEP = 0;
P0 = 0XFF;
P1 = 0XFF;
P2 = 0XFF;
P3 = 0XFF;
while(i--)
{
LED_ok = ~LED_ok;
LED_UART = ~LED_UART;
LED_error = ~LED_error;
LED_zhishi = ~LED_zhishi;
Delay100uS(500);
}
while (1)
{
ES = 1;
SM2 = 0;
cStatus = UartTesting();
if (SUCCESS != cStatus)
{
Err(); //测试失败
g_bCard = !CARDPIN;
LED_zhishi = CARDPIN;
ShowError(cStatus);
continue;
}
LED_ok = 0; //测试成功
Delay100uS(500);
LED_ok = 1;
g_bCard = !CARDPIN;
LED_zhishi = CARDPIN;
i++;
}
}
unsigned char UartTesting()
{
unsigned char cReadBlock[10] = {0x0a, 0x21, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char *cPa, *cPb;
unsigned char i;
unsigned char cCnt;
cPa = ComSet;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 1;
}
cPa = ComSearchCard;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 2;
}
//依次读出s50卡片中的16个扇区,64块的数据
for(i=0; i<64 ;i++)
{
cReadBlock[3] = i;
cPa = cReadBlock;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(3);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 3;
}
Delay100uS(50);
}
//写块
cPa = ComWriteBlock4;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 4;
}
//读块
cPa = ComReadBlock4;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 5;
}
//比较写入的和读出的数据
cPa = &ComWriteBlock4[10];
cPb = &g_cReceBuf[2];
if (memcmp(cPa, cPb, 16) != 0)
{
return 6;
}
//初始化钱包的值为0x12345678
cPa = ComIntiPurse5;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 7;
}
//增值
cPa = ComIncrPurse5;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 8;
}
//减值
cPa=ComDecrPurse5;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 9;
}
//restore the purse in block No.5 to block No.6
cPa = ComBakPurse56;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 10;
}
//读块6
cPa = ComReadPurse6;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 11;
}
//比较操作钱包的结果
cPa = pursevalue;
cPb = &g_cReceBuf[2];
if (memcmp(cPa, cPb, 4) != 0)
{
return 12;
}
cPa=ComHaltMCU;
SendUart(cPa);
for (cCnt=200; (cCnt > 0) && !g_bReceCommandOk; cCnt--)
{
Delay100uS(1);
}
if ((g_cReceBuf[1] != cPa[1]) || (0 == cCnt))
{
return 13;
}
return SUCCESS;
}
/*****************************************************************************
* 发光管错误指示
*****************************************************************************/
void ShowError(unsigned char cTimes)
{
Delay100uS(5000);
while (cTimes--)
{
LED_error = 0;
Delay100uS(1000);
LED_error = 1;
Delay100uS(1000);
}
}
/*****************************************************************************
错误,蜂鸣器1s
*****************************************************************************
评论3
最新资源