main.LST
资源名称:RFID.rar [点击查看]
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:16k
源码类别:
RFID编程
开发平台:
C/C++
- C51 COMPILER V8.08 MAIN 03/14/2010 21:10:24 PAGE 1
- C51 COMPILER V8.08, COMPILATION OF MODULE MAIN
- OBJECT MODULE PLACED IN main.OBJ
- COMPILER INVOKED BY: C:KeilC51BINC51.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/14/2010 21:10:24 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/14/2010 21:10:24 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 cP[0]=0x01;
- 134 2 for (cCnt=0; cCnt<cP[0]; cCnt++)
- 135 2 {
- 136 3 cP[cCnt] = SPIRWByte(0xFF);
- 137 3 cCheckSum ^= cP[cCnt];
- 138 3 if(cP[0]>32)
- 139 3 {
- 140 4 NSS=1;
- 141 4 return FAILURE;
- 142 4 }
- 143 3 }
- 144 2 delay_1ms(10);
- 145 2 cP[cCnt] = SPIRWByte(0xFF);
- 146 2 delay_1ms(10);
- 147 2
- 148 2 NSS=1;
- 149 2 if (cCheckSum == cP[cCnt]) return SUCCESS;
- 150 2 else return FAILURE;
- 151 2
- 152 2
- 153 2 }
- 154 1 else return FAILURE;
- 155 1 }
- 156
- 157 /***** 写SPI总线数据 *********/
- 158 unsigned char SPI_Write(unsigned char *cP)
- 159 {
- 160 1 unsigned char i,cStatus;
- 161 1 unsigned char cCheckSum = 0;
- 162 1
- 163 1 NSS=0;
- 164 1 cStatus=SPIRWByte(spiwrite);
- 165 1
- 166 1 for(i=0; i<cP[0]; i++) // cP[0] 表示数据包的长度
- 167 1 {
- 168 2 cCheckSum ^= cP[i];
- 169 2 cStatus=SPIRWByte(cP[i]);
- 170 2 }
- 171 1 cStatus=SPIRWByte(cCheckSum);
- 172 1 NSS=1;
- 173 1 return cStatus;
- 174 1 }
- 175
- 176
- 177 //-----Reset rf module -------//
- 178 void Reset_HY502(void)
- 179 {
- C51 COMPILER V8.08 MAIN 03/14/2010 21:10:24 PAGE 4
- 180 1 RST_HY=0; //Reset HY502 复位模块
- 181 1 delay_10ms(50);
- 182 1 RST_HY=1;
- 183 1 }
- 184
- 185 /************************** main function start here *******************************/
- 186 uchar main()
- 187 {
- 188 1
- 189 1
- 190 1
- 191 1 uchar idata str1[32];
- 192 1 uchar idata cStatus;
- 193 1 uchar *cPa;
- 194 1 int idata flag = 0;
- 195 1
- 196 1 float idata money=210.5; // 赋初值
- 197 1
- 198 1 InitializeSystem(); // 初始化系统
- 199 1 Reset_HY502();
- 200 1 delay_1ms(500);
- 201 1 LCD_Init();
- 202 1 Display_full_screen_color (BLACK);
- 203 1 disintlcd();
- 204 1
- 205 1
- 206 1
- 207 1
- 208 1 while(sig) ;
- 209 1
- 210 1 memcpy(&ComWriteBlock4[10],&money,4);
- 211 1
- 212 1
- 213 1
- 214 1 cPa = ComWriteBlock4;
- 215 1 SPI_Write(cPa);
- 216 1 delay_1ms(100); // delay for module execution
- 217 1 cStatus = SPI_Read(g_cReceBuf);
- 218 1
- 219 1 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 220 1 {
- 221 2 cPa =ComWriteBlock4;
- 222 2 SPI_Write(cPa);
- 223 2 delay_1ms(100);
- 224 2 cStatus = SPI_Read(g_cReceBuf);
- 225 2 delay_1ms(100);
- 226 2
- 227 2
- 228 2 }
- 229 1
- 230 1
- 231 1
- 232 1 while (1)
- 233 1 {
- 234 2
- 235 2 if (flag == 1) break ;
- 236 2 while (sig) ;
- 237 2
- 238 2 delay_1ms(80); //预留卡靠近的时间
- 239 2
- 240 2 while (!sig)
- 241 2 {
- C51 COMPILER V8.08 MAIN 03/14/2010 21:10:24 PAGE 5
- 242 3
- 243 3 cPa =ComReadBlock4; // 读取钱包
- 244 3 SPI_Write(cPa);
- 245 3 delay_1ms(100);
- 246 3
- 247 3 cStatus = SPI_Read(g_cReceBuf);
- 248 3 if ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 249 3 {
- 250 4 cStatus = SPI_Write(ComHaltCard);
- 251 4 DrawString (45,150,"读卡失败,请重刷",BLUE,GREEN,NORMAL) ;
- 252 4 delay_1ms(500);
- 253 4 DrawString (45,150," ",GREEN,GREEN,NORMAL) ;
- 254 4 continue;
- 255 4
- 256 4
- 257 4 }
- 258 3
- 259 3 memcpy(&money,&g_cReceBuf[2],4);
- 260 3 money = money - 1.5;
- 261 3 if (money <0) //余额不够,重新初始化赋值
- 262 3 {
- 263 4 cStatus = SPI_Write(ComHaltCard);
- 264 4 DrawString (45,150,"请重初始化",BLUE,GREEN,NORMAL) ;
- 265 4 delay_1ms(500);
- 266 4 DrawString (45,150," ",GREEN,GREEN,NORMAL) ;
- 267 4 flag = 1;
- 268 4 break;
- 269 4
- 270 4 }
- 271 3
- 272 3
- 273 3 memcpy(&ComWriteBlock4[10],&money,4);
- 274 3
- 275 3 cPa =ComWriteBlock4;
- 276 3 SPI_Write(cPa);
- 277 3 delay_1ms(100);
- 278 3
- 279 3 cStatus = SPI_Read(g_cReceBuf);
- 280 3
- 281 3 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 282 3 {
- 283 4 cPa =ComWriteBlock4;
- 284 4 SPI_Write(cPa);
- 285 4 delay_1ms(100);
- 286 4 cStatus = SPI_Read(g_cReceBuf);
- 287 4 delay_1ms(100);
- 288 4
- 289 4
- 290 4 }
- 291 3
- 292 3 ///////////////////////
- 293 3
- 294 3
- 295 3
- 296 3
- 297 3
- 298 3 sprintf(str1, "%0.1f", money);
- 299 3
- 300 3 cStatus = SPI_Write(ComHaltCard); //设置为休眠状态
- 301 3 delay_1ms(100);
- 302 3
- 303 3 DrawString(125, 208,"AC7A3E74",BLACK, WHITE, NORMAL); //获取卡号可参考资料手册,这里不作说明
- C51 COMPILER V8.08 MAIN 03/14/2010 21:10:24 PAGE 6
- 304 3 DrawString(125, 244,str1,BLACK, WHITE, NORMAL);
- 305 3 DrawString(180, 244,"元",BLACK,WHITE, NORMAL);
- 306 3
- 307 3 delay_1ms(2500);
- 308 3
- 309 3 DispOneColor(120,200,230,231,WHITE) ;
- 310 3 DispOneColor(120,236,230,267,WHITE) ;
- 311 3 DispOneColor(120,200,230,231,WHITE) ;//baibiao
- 312 3
- 313 3 }
- 314 2
- 315 2
- 316 2 }
- 317 1
- 318 1
- 319 1
- 320 1 }
- *** WARNING C173 IN LINE 320 OF MAIN.C: missing return-expression
- 321 /*****************************************************************************
- 322 *system initialize
- 323 *UART baudrate = 19200bps, baud generator is T1
- 324 *****************************************************************************/
- 325 void InitializeSystem()
- 326 {
- 327 1
- 328 1 ET2 = 0;
- 329 1 T2CON = 0x04;
- 330 1 PCON = 0x80;
- 331 1 SCON = 0x70;
- 332 1 TMOD = 0x21; //TMOD = 0x22;
- 333 1
- 334 1 TH1 = BAUD_19200; //默认波特率
- 335 1 TL1 = TH1;
- 336 1 TR1 = TRUE; // 波特率发生器
- 337 1
- 338 1 ET1=0;
- 339 1 EA=1;
- 340 1 EX0=0;;
- 341 1 IT0 = 1;
- 342 1 TR2=0;
- 343 1 ES = TRUE;
- 344 1 g_bReceCommandOk=0;
- 345 1
- 346 1 }
- MODULE INFORMATION: STATIC OVERLAYABLE
- CODE SIZE = 4840 ----
- CONSTANT SIZE = 49691 ----
- XDATA SIZE = 21 127
- PDATA SIZE = ---- ----
- DATA SIZE = ---- 1
- IDATA SIZE = 90 39
- BIT SIZE = 3 ----
- END OF MODULE INFORMATION.
- C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)