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