main.LST
资源名称:RFID.rar [点击查看]
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:14k
源码类别:
RFID编程
开发平台:
C/C++
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 1
- C51 COMPILER V8.08, COMPILATION OF MODULE MAIN
- OBJECT MODULE PLACED IN main.OBJ
- COMPILER INVOKED BY: C:Program FilesKeilC51BINC51.EXE main.c LARGE BROWSE DEBUG OBJECTEXTEND
- line level source
- 1 #include <stdio.h>
- 2 #include <intrins.h>
- 3 #include "main.h"
- 4 #include "hy502.h"
- 5 #include "lcdd.h"
- 6
- 7
- 8 // SPI 总线状态定义
- 9 #define SPI_RDY 0xF0; // 准备
- 10 #define spibusy 0xaa // 忙
- 11 #define spiread 0xbb // 读
- 12 #define spiwrite 0xcc // 写
- 13
- 14
- 15
- 16 /////////////////////////////////////////////////////////////////////////////////////////////////////
- 17 // 延时函数
- 18 void delay(unsigned int x)
- 19 {
- 20 1 while(x)
- 21 1 {
- 22 2 x--;
- 23 2 }
- 24 1 }
- 25
- 26
- 27
- 28 ///////////////////////////////////////////////////////////////////////
- 29 // Delay 1ms
- 30 ///////////////////////////////////////////////////////////////////////
- 31 void delay_1ms (uint _1ms)
- 32 {
- 33 1
- 34 1 RCAP2LH = RCAP2_1ms;
- 35 1 T2LH = RCAP2_1ms;
- 36 1 ET2 = 0; // Disable timer2 interrupt
- 37 1 T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
- 38 1
- 39 1 while (_1ms--)
- 40 1 {
- 41 2 while (!TF2);
- 42 2 TF2 = FALSE;
- 43 2 }
- 44 1 TR2 = FALSE;
- 45 1
- 46 1 }
- 47
- 48 ///////////////////////////////////////////////////////////////////////
- 49 // Delay 10ms
- 50 ///////////////////////////////////////////////////////////////////////
- 51 void delay_10ms(unsigned int _10ms)
- 52 {
- 53 1 RCAP2LH = RCAP2_10ms;
- 54 1 T2LH = RCAP2_10ms;
- 55 1 ET2 = 0; // Disable timer2 interrupt
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 2
- 56 1 T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
- 57 1
- 58 1 while (_10ms--)
- 59 1 {
- 60 2 while (!TF2)
- 61 2 {
- 62 3 if(g_bReceCommandOk) // change by lin 2006-08-21
- 63 3 {
- 64 4 TR2 = FALSE;
- 65 4 TF2 = FALSE;
- 66 4 return;
- 67 4 }
- 68 3 }
- 69 2 TF2 = FALSE;
- 70 2 }
- 71 1 TR2 = FALSE;
- 72 1
- 73 1 }
- 74
- 75
- 76 /******************* SPI FUNCTION DEFINE ************************************/
- 77 /*****************************************************************************
- 78 *function: send a byte over SPI bus
- 79 发送一个字节
- 80 *****************************************************************************/
- 81 unsigned char SPIRWByte(unsigned char cSendByte)
- 82 {
- 83 1 unsigned char data i = 8;
- 84 1 unsigned char cRecByte;
- 85 1 while (i--)
- 86 1 {
- 87 2 cRecByte += cRecByte;
- 88 2 SCL = 0;
- 89 2 MOSI = (bit)(cSendByte & 0x80);
- 90 2 cSendByte += cSendByte;
- 91 2 cRecByte |= (unsigned char) MISO ;
- 92 2 SCL = 1;
- 93 2 }
- 94 1 SCL = 1;
- 95 1 return cRecByte;
- 96 1 }
- 97
- 98
- 99 /***** 查询SPI总线状态 *********/
- 100 unsigned char spi_cStatus(void)
- 101 {
- 102 1 unsigned char cStatus;
- 103 1 NSS=0;
- 104 1 cStatus=SPIRWByte(spibusy);
- 105 1
- 106 1 cStatus=SPIRWByte(0xFF);
- 107 1 NSS=1;
- 108 1
- 109 1 return cStatus;
- 110 1 }
- 111
- 112 /***** 读SPI总线数据********/
- 113 unsigned char SPI_Read(unsigned char *cP)
- 114 {
- 115 1 unsigned char cCnt,cStatus;
- 116 1 unsigned char cCheckSum = 0;
- 117 1
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 3
- 118 1 for (cCnt=0; cCnt<100; cCnt++)
- 119 1 {
- 120 2 cStatus=spi_cStatus(); //查询SPI总线状态
- 121 2 if(cStatus==0xF0)
- 122 2 {
- 123 3 cCnt=253;
- 124 3 }
- 125 2 delay(10);
- 126 2 }
- 127 1
- 128 1 if(cCnt==254) //卡操作结束,可以回传数据
- 129 1 {
- 130 2 NSS=0;
- 131 2 cCnt=SPIRWByte(spiread);
- 132 2
- 133 2
- 134 2 cP[0]=0x01;
- 135 2 for (cCnt=0; cCnt<cP[0]; cCnt++)
- 136 2 {
- 137 3 cP[cCnt] = SPIRWByte(0xFF);
- 138 3 cCheckSum ^= cP[cCnt];
- 139 3 if(cP[0]>32)
- 140 3 {
- 141 4 NSS=1;
- 142 4 return FAILURE;
- 143 4 }
- 144 3 }
- 145 2 delay_1ms (10);
- 146 2 cP[cCnt] = SPIRWByte(0xFF);
- 147 2 delay_1ms (10) ;
- 148 2 NSS=1;
- 149 2 if (cCheckSum == cP[cCnt])
- 150 2 {
- 151 3
- 152 3 return SUCCESS;
- 153 3
- 154 3 }
- 155 2 }
- 156 1
- 157 1 return FAILURE;
- 158 1 }
- 159
- 160 /***** 写SPI总线数据 *********/
- 161 unsigned char SPI_Write(unsigned char *cP)
- 162 {
- 163 1 unsigned char i,cStatus;
- 164 1 unsigned char cCheckSum = 0;
- 165 1
- 166 1 NSS=0;
- 167 1 cStatus=SPIRWByte(spiwrite);
- 168 1
- 169 1 for(i=0; i<cP[0]; i++) // cP[0] 表示数据包的长度
- 170 1 {
- 171 2 cCheckSum ^= cP[i];
- 172 2 cStatus=SPIRWByte(cP[i]);
- 173 2 }
- 174 1 cStatus=SPIRWByte(cCheckSum);
- 175 1 NSS=1;
- 176 1 return cStatus;
- 177 1 }
- 178
- 179
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 4
- 180 //-----Reset rf module -------//
- 181 void Reset_HY502(void)
- 182 {
- 183 1 RST_HY=0; //Reset HY502 复位模块
- 184 1 delay_10ms(50);
- 185 1 RST_HY=1;
- 186 1 }
- 187
- 188 /************************** main function start here *******************************/
- 189 void main()
- 190 {
- 191 1
- 192 1 uchar idata str1[32];
- 193 1 uchar idata cStatus;
- 194 1 uchar *cPa;
- 195 1 int idata flag = 0;
- 196 1
- 197 1 float idata money=210.5; // 赋初值
- 198 1
- 199 1 InitializeSystem(); // 初始化系统
- 200 1 Reset_HY502();
- 201 1 delay_1ms(500);
- 202 1 LCD_Init();
- 203 1 Display_full_screen_color (BLACK);
- 204 1 disintlcd();
- 205 1
- 206 1
- 207 1
- 208 1
- 209 1
- 210 1 while(sig) ;
- 211 1
- 212 1 memcpy(&ComWriteBlock4[10],&money,4);
- 213 1
- 214 1 cPa =ComWriteBlock4;
- 215 1 SPI_Write(ComWriteBlock4);
- 216 1 delay_1ms(100); // delay for module execution
- 217 1 cStatus = SPI_Read(g_cReceBuf);
- 218 1 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 219 1 {
- 220 2 cPa =ComWriteBlock4;
- 221 2 SPI_Write(cPa);
- 222 2 delay_1ms(100);
- 223 2 cStatus = SPI_Read(g_cReceBuf);
- 224 2 delay_1ms(100);
- 225 2
- 226 2
- 227 2 }
- 228 1
- 229 1 while (1)
- 230 1 {
- 231 2 if (flag == 1) break;
- 232 2 while( sig);
- 233 2 delay_1ms(70); //预留卡靠近的时间
- 234 2 while (!sig)
- 235 2 {
- 236 3
- 237 3 cPa =ComReadBlock4; // 读取钱包
- 238 3 SPI_Write(ComReadBlock4);
- 239 3 delay_1ms(100);
- 240 3 cStatus = SPI_Read(g_cReceBuf);
- 241 3 delay_1ms(300);
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 5
- 242 3
- 243 3
- 244 3 memcpy(&money,&g_cReceBuf[2],4);
- 245 3 money = money - 1.5;
- 246 3 if (money<0 && money>210.5)
- 247 3 {
- 248 4 flag =1;
- 249 4 break;
- 250 4 }
- 251 3
- 252 3
- 253 3 memcpy(&ComWriteBlock4[10],&money,4);
- 254 3 cPa =ComWriteBlock4;
- 255 3 SPI_Write(cPa);
- 256 3 delay_1ms(100);
- 257 3 cStatus = SPI_Read(g_cReceBuf);
- 258 3 delay_1ms(200);
- 259 3
- 260 3
- 261 3
- 262 3
- 263 3 sprintf(str1, "%0.1f", money);
- 264 3
- 265 3 SPI_Write(ComHaltCard); //设置为休眠状态
- 266 3 DrawString(125, 208,"AC7A3E74",BLACK, WHITE, NORMAL); //获取卡号可参考资料手册,这里不作说明
- 267 3 DrawString(125, 244,str1,BLACK, WHITE, NORMAL);
- 268 3 DrawString(180, 244,"元",BLACK,WHITE, NORMAL);
- 269 3
- 270 3 delay_1ms(2000);
- 271 3
- 272 3 DispOneColor(120,200,230,231,WHITE) ;
- 273 3 DispOneColor(120,236,230,267,WHITE) ;
- 274 3 DispOneColor(120,200,230,231,WHITE) ;//baibiao
- 275 3
- 276 3
- 277 3 }
- 278 2
- 279 2 }
- 280 1
- 281 1
- 282 1
- 283 1 }
- 284 /*****************************************************************************
- 285 *system initialize
- 286 *UART baudrate = 19200bps, baud generator is T1
- 287 *****************************************************************************/
- 288 void InitializeSystem()
- 289 {
- 290 1
- 291 1 ET2 = 0;
- 292 1 T2CON = 0x04;
- 293 1 PCON = 0x80;
- 294 1 SCON = 0x70;
- 295 1 TMOD = 0x21; //TMOD = 0x22;
- 296 1
- 297 1 TH1 = BAUD_19200; //默认波特率
- 298 1 TL1 = TH1;
- 299 1 TR1 = TRUE; // 波特率发生器
- 300 1
- 301 1 ET1=0;
- 302 1 EA=1;
- 303 1 EX0=0;;
- C51 COMPILER V8.08 MAIN 03/03/2010 00:40:31 PAGE 6
- 304 1 IT0 = 1;
- 305 1 TR2=0;
- 306 1 ES = TRUE;
- 307 1 g_bReceCommandOk=0;
- 308 1
- 309 1 }
- MODULE INFORMATION: STATIC OVERLAYABLE
- CODE SIZE = 4726 ----
- CONSTANT SIZE = 49260 ----
- XDATA SIZE = 21 127
- PDATA SIZE = ---- ----
- DATA SIZE = ---- 1
- IDATA SIZE = 90 39
- BIT SIZE = 3 ----
- END OF MODULE INFORMATION.
- C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)