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