main.LST
资源名称:RFID.rar [点击查看]
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:15k
源码类别:
RFID编程
开发平台:
C/C++
- C51 COMPILER V8.08 MAIN 03/14/2010 21:07:54 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:07:54 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:07:54 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:07:54 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 int idata money=120; // 赋初值
- 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 cPa = ComIntiPurse5 ;
- 210 1 SPI_Write(cPa);
- 211 1 delay_1ms(100) ;
- 212 1 cStatus = SPI_Read(g_cReceBuf);
- 213 1 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 214 1 {
- 215 2 cPa = ComIntiPurse5 ;
- 216 2 SPI_Write(cPa);
- 217 2 delay_1ms(100) ;
- 218 2 cStatus = SPI_Read(g_cReceBuf);
- 219 2 }
- 220 1
- 221 1
- 222 1 while (1)
- 223 1 {
- 224 2
- 225 2 if (flag == 1) break;
- 226 2 while (sig) ;
- 227 2
- 228 2 delay_1ms(80); //预留卡靠近的时间
- 229 2
- 230 2 while (!sig)
- 231 2 {
- 232 3
- 233 3 if (money -2<0) //余额不够,重新初始化赋值
- 234 3 {
- 235 4 cStatus = SPI_Write(ComHaltCard);
- 236 4 DrawString (45,150,"请重初始化",BLUE,GREEN,NORMAL) ;
- 237 4 delay_1ms(500);
- 238 4 DrawString (45,150," ",GREEN,GREEN,NORMAL) ;
- 239 4 flag = 1;
- 240 4 break;
- 241 4
- C51 COMPILER V8.08 MAIN 03/14/2010 21:07:54 PAGE 5
- 242 4 }
- 243 3
- 244 3
- 245 3 cPa =ComDecrPurse5; // 减去钱包特定值
- 246 3 SPI_Write(cPa);
- 247 3 delay_1ms(100);
- 248 3 cStatus = SPI_Read(g_cReceBuf);
- 249 3 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 250 3 {
- 251 4 cPa =ComDecrPurse5;
- 252 4 SPI_Write(cPa);
- 253 4 delay_1ms(100);
- 254 4 cStatus = SPI_Read(g_cReceBuf);
- 255 4
- 256 4 }
- 257 3
- 258 3
- 259 3
- 260 3 cPa =ComReadPurse5; //读取钱包
- 261 3 SPI_Write(cPa);
- 262 3 delay_1ms(100);
- 263 3
- 264 3 cStatus = SPI_Read(g_cReceBuf);
- 265 3
- 266 3 if ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 267 3 {
- 268 4 cStatus = SPI_Write(ComHaltCard);
- 269 4
- 270 4 delay_1ms(500);
- 271 4 continue;
- 272 4
- 273 4
- 274 4 }
- 275 3
- 276 3 money = g_cReceBuf[2];
- 277 3
- 278 3 ///////////////////////
- 279 3
- 280 3
- 281 3
- 282 3
- 283 3
- 284 3 sprintf(str1, "%d", money);
- 285 3
- 286 3 cStatus = SPI_Write(ComHaltCard); //设置为休眠状态
- 287 3 delay_1ms(100);
- 288 3
- 289 3 DrawString(125, 208,"AC7A3E74",BLACK, WHITE, NORMAL); //获取卡号可参考资料手册,这里不作说明
- 290 3 DrawString(125, 244,str1,BLACK, WHITE, NORMAL);
- 291 3 DrawString(180, 244,"元",BLACK,WHITE, NORMAL);
- 292 3
- 293 3 delay_1ms(2000);
- 294 3
- 295 3 DispOneColor(120,200,230,231,WHITE) ;
- 296 3 DispOneColor(120,236,230,267,WHITE) ;
- 297 3 DispOneColor(120,200,230,231,WHITE) ;//baibiao
- 298 3
- 299 3 }
- 300 2
- 301 2
- 302 2 }
- 303 1
- C51 COMPILER V8.08 MAIN 03/14/2010 21:07:54 PAGE 6
- 304 1
- 305 1
- 306 1 }
- *** WARNING C173 IN LINE 306 OF MAIN.C: missing return-expression
- 307 /*****************************************************************************
- 308 *system initialize
- 309 *UART baudrate = 19200bps, baud generator is T1
- 310 *****************************************************************************/
- 311 void InitializeSystem()
- 312 {
- 313 1
- 314 1 ET2 = 0;
- 315 1 T2CON = 0x04;
- 316 1 PCON = 0x80;
- 317 1 SCON = 0x70;
- 318 1 TMOD = 0x21; //TMOD = 0x22;
- 319 1
- 320 1 TH1 = BAUD_19200; //默认波特率
- 321 1 TL1 = TH1;
- 322 1 TR1 = TRUE; // 波特率发生器
- 323 1
- 324 1 ET1=0;
- 325 1 EA=1;
- 326 1 EX0=0;;
- 327 1 IT0 = 1;
- 328 1 TR2=0;
- 329 1 ES = TRUE;
- 330 1 g_bReceCommandOk=0;
- 331 1
- 332 1 }
- MODULE INFORMATION: STATIC OVERLAYABLE
- CODE SIZE = 4755 ----
- CONSTANT SIZE = 49654 ----
- XDATA SIZE = 21 127
- PDATA SIZE = ---- ----
- DATA SIZE = ---- 1
- IDATA SIZE = 90 37
- BIT SIZE = 3 ----
- END OF MODULE INFORMATION.
- C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)