#include "main.h"
#include "lcd.h"
//SPI 总线状态定义
#define SPI_RDY 0xF0; // 准备
#define spibusy 0xaa // 忙
#define spiread 0xbb // 读
#define spiwrite 0xcc // 写
sbit SCL = P2^3; // SCK
sbit MISO = P2^4; // MISO
sbit MOSI = P2^5; // MOSI
sbit sig = P2^6; // sig 为 1表示有卡
sbit NSS = P2^7; // 接HY502 NSS 低电平表示可用
sbit led_work = P0^0; //工作闪光灯的接口
/////////////////////////////////////////////////////////////////////////////////////////////////////
// 延时函数
void delay(unsigned int x)
{
while(x)
{
x--;
}
}
///////////////////////////////////////////////////////////////////////
// Delay 1ms
///////////////////////////////////////////////////////////////////////
void delay_1ms (uint x)
{
uint i,j;
for (i=0; i<x; i++)
for(j=0;j<=110;j++);
}
void blink_led() //闪光灯,亮10ms
{
led_work = 0;
delay_1ms(10);
led_work = 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]=0x01;
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;
}
/************************** main function start here *******************************/
void main()
{
uchar str0[]={"SN="};
uchar cStatus;
uchar *cPa;
uchar i;
uchar SelectedSn[4]; //卡序列号
uchar CardSnAscii[8]; //用于存储读取的ASCII码卡号
uchar ascii[16] = "0123456789ABCDEF"; //用于显示的ASCII码
lcd_init(); //初始化LCD显示屏
while (1)
{
while(sig);
delay_1ms(70); //预留卡靠近的时间
while(!sig)
{
blink_led();
delay_1ms(100);
cPa = ComGetCardSn; // 读卡号
SPI_Write(cPa);
delay_1ms(100);
cStatus = SPI_Read(g_cReceBuf);
if ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
{
cStatus = SPI_Write(ComHaltCard);
delay_1ms(500);
continue;
}
//存放到数组里面的是16进制的卡号
//从 g_cReceBuf里面的第三个字符开始,读取4个字符
memcpy(SelectedSn , &g_cReceBuf[2], 4);
for(i = 0 ; i < 4 ; i++){
//将SelectedSn 中的16进制数据转换为可以显示和发送的ASCII码
CardSnAscii[2*i] = ascii[(SelectedSn[i]&0xf0)>>4]; //取出高位后右移4位
CardSnAscii[2*i+1] = ascii[SelectedSn[i]&0x0f];
}
SPI_Write(ComHaltCard); //设置为休眠状态
lcd_init();
lcd_display(str0 , CardSnAscii);
blink_led();
}
}
}
- 1
- 2
前往页