main.c
资源名称:RFID.rar [点击查看]
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:6k
源码类别:
RFID编程
开发平台:
C/C++
- #include <stdio.h>
- #include <intrins.h>
- #include "main.h"
- #include "hy502.h"
- #include "lcdd.h"
- // SPI 总线状态定义
- #define SPI_RDY 0xF0; // 准备
- #define spibusy 0xaa // 忙
- #define spiread 0xbb // 读
- #define spiwrite 0xcc // 写
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- // 延时函数
- void delay(unsigned int x)
- {
- while(x)
- {
- x--;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- // 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;
- }
- /******************* 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;
- }
- }
- delay_1ms (10);
- cP[cCnt] = SPIRWByte(0xFF);
- delay_1ms (10) ;
- 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;
- }
- //-----Reset rf module -------//
- void Reset_HY502(void)
- {
- RST_HY=0; //Reset HY502 复位模块
- delay_10ms(50);
- RST_HY=1;
- }
- /************************** main function start here *******************************/
- void main()
- {
- uchar idata str1[32];
- uchar idata cStatus;
- uchar *cPa;
- int idata flag = 0;
- float idata money=210.5; // 赋初值
- InitializeSystem(); // 初始化系统
- Reset_HY502();
- delay_1ms(500);
- LCD_Init();
- Display_full_screen_color (BLACK);
- disintlcd();
- while(sig) ;
- memcpy(&ComWriteBlock4[10],&money,4);
- cPa =ComWriteBlock4;
- SPI_Write(ComWriteBlock4);
- delay_1ms(100); // delay for module execution
- cStatus = SPI_Read(g_cReceBuf);
- while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- {
- cPa =ComWriteBlock4;
- SPI_Write(cPa);
- delay_1ms(100);
- cStatus = SPI_Read(g_cReceBuf);
- delay_1ms(100);
- }
- while (1)
- {
- if (flag == 1) break;
- while( sig);
- delay_1ms(70); //预留卡靠近的时间
- while (!sig)
- {
- cPa =ComReadBlock4; // 读取钱包
- SPI_Write(ComReadBlock4);
- delay_1ms(100);
- cStatus = SPI_Read(g_cReceBuf);
- delay_1ms(300);
- memcpy(&money,&g_cReceBuf[2],4);
- money = money - 1.5;
- if (money<0 && money>210.5)
- {
- flag =1;
- break;
- }
- memcpy(&ComWriteBlock4[10],&money,4);
- cPa =ComWriteBlock4;
- SPI_Write(cPa);
- delay_1ms(100);
- cStatus = SPI_Read(g_cReceBuf);
- delay_1ms(200);
- sprintf(str1, "%0.1f", money);
- SPI_Write(ComHaltCard); //设置为休眠状态
- DrawString(125, 208,"AC7A3E74",BLACK, WHITE, NORMAL); //获取卡号可参考资料手册,这里不作说明
- DrawString(125, 244,str1,BLACK, WHITE, NORMAL);
- DrawString(180, 244,"元",BLACK,WHITE, NORMAL);
- delay_1ms(2000);
- DispOneColor(120,200,230,231,WHITE) ;
- DispOneColor(120,236,230,267,WHITE) ;
- DispOneColor(120,200,230,231,WHITE) ;//baibiao
- }
- }
- }
- /*****************************************************************************
- *system initialize
- *UART baudrate = 19200bps, baud generator is T1
- *****************************************************************************/
- void InitializeSystem()
- {
- ET2 = 0;
- T2CON = 0x04;
- PCON = 0x80;
- SCON = 0x70;
- TMOD = 0x21; //TMOD = 0x22;
- TH1 = BAUD_19200; //默认波特率
- TL1 = TH1;
- TR1 = TRUE; // 波特率发生器
- ET1=0;
- EA=1;
- EX0=0;;
- IT0 = 1;
- TR2=0;
- ES = TRUE;
- g_bReceCommandOk=0;
- }