main.LST
资源名称:RFID.rar [点击查看]
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:13k
源码类别:
RFID编程
开发平台:
C/C++
- C51 COMPILER V8.08 MAIN 03/14/2010 21:16:39 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:16:39 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 cStatus=SPIRWByte(0xFF);
- 106 1 NSS=1;
- 107 1
- 108 1 return cStatus;
- 109 1 }
- 110
- 111 /***** 读SPI总线数据********/
- 112 unsigned char SPI_Read(unsigned char *cP)
- 113 {
- 114 1 unsigned char cCnt,cStatus;
- 115 1 unsigned char cCheckSum = 0;
- 116 1
- 117 1 for (cCnt=0; cCnt<100; cCnt++)
- C51 COMPILER V8.08 MAIN 03/14/2010 21:16:39 PAGE 3
- 118 1 {
- 119 2 cStatus=spi_cStatus(); //查询SPI总线状态
- 120 2 if(cStatus==0xF0)
- 121 2 {
- 122 3 cCnt=253;
- 123 3 }
- 124 2 delay(10);
- 125 2 }
- 126 1
- 127 1 if(cCnt==254) //卡操作结束,可以回传数据
- 128 1 {
- 129 2 NSS=0;
- 130 2 cCnt=SPIRWByte(spiread);
- 131 2
- 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
- 145 2 cP[cCnt] = SPIRWByte(0xFF);
- 146 2
- 147 2 NSS=1;
- 148 2 if (cCheckSum == cP[cCnt])
- 149 2 {
- 150 3 return SUCCESS;
- 151 3
- 152 3 }
- 153 2 }
- 154 1 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:16:39 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 void main()
- 187 {
- 188 1
- 189 1 uchar str0[]={"money ="} ;
- 190 1 uchar idata str3[]={"error"};
- 191 1 uchar str1[4];
- 192 1 uchar idata cStatus;
- 193 1 uchar *cPa;
- 194 1 int flag=0;
- 195 1 int money=120;
- 196 1
- 197 1 InitializeSystem(); // 初始化系统
- 198 1 Reset_HY502();
- 199 1 delay_1ms(500);
- 200 1 lcd_init() ;
- 201 1
- 202 1
- 203 1 while(sig) ;
- 204 1
- 205 1 cPa = ComIntiPurse5 ;
- 206 1 SPI_Write(cPa);
- 207 1 delay_1ms(100) ;
- 208 1 cStatus = SPI_Read(g_cReceBuf);
- 209 1 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 210 1 {
- 211 2 cPa = ComIntiPurse5 ;
- 212 2 SPI_Write(cPa);
- 213 2 delay_1ms(100) ;
- 214 2 cStatus = SPI_Read(g_cReceBuf);
- 215 2 }
- 216 1 ////////////
- 217 1
- 218 1 while (1)
- 219 1 {
- 220 2 if (flag == 1) break;
- 221 2
- 222 2
- 223 2 while(sig) ;
- 224 2
- 225 2
- 226 2 delay_1ms(70); //预留卡靠近的时间
- 227 2 while ( !sig)
- 228 2 {
- 229 3 if (money -10<0) //余额不够,重新初始化赋值
- 230 3 {
- 231 4 display(str3);
- 232 4 flag = 1;
- 233 4 break;
- 234 4
- 235 4 }
- 236 3
- 237 3 cPa =ComDecrPurse5;
- 238 3 SPI_Write(cPa);
- 239 3 delay_1ms(100);
- 240 3 cStatus = SPI_Read(g_cReceBuf);
- 241 3 while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- C51 COMPILER V8.08 MAIN 03/14/2010 21:16:39 PAGE 5
- 242 3 {
- 243 4 cPa =ComDecrPurse5;
- 244 4 SPI_Write(cPa);
- 245 4 delay_1ms(100);
- 246 4 cStatus = SPI_Read(g_cReceBuf);
- 247 4
- 248 4 }
- 249 3
- 250 3 delay_1ms(200);
- 251 3 cPa =ComReadPurse5; // 读取钱包
- 252 3 SPI_Write(cPa);
- 253 3 delay_1ms(100);
- 254 3 cStatus = SPI_Read(g_cReceBuf);
- 255 3 if ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
- 256 3 {
- 257 4 cStatus = SPI_Write(ComHaltCard);
- 258 4 display(str3);
- 259 4 delay_1ms(500);
- 260 4 continue;
- 261 4 }
- 262 3 ///////////////////////
- 263 3
- 264 3 money = g_cReceBuf[2];
- 265 3
- 266 3
- 267 3 sprintf(str1, "%d", money);
- 268 3
- 269 3 SPI_Write(ComHaltCard); //设置为休眠状态
- 270 3 delay_1ms(100);
- 271 3 lcd_display(str0,str1);
- 272 3
- 273 3 delay_1ms(2000);
- 274 3 lcd_init() ;
- 275 3
- 276 3 }
- 277 2 }
- 278 1
- 279 1
- 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/14/2010 21:16:39 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 = 954 ----
- CONSTANT SIZE = 94 ----
- XDATA SIZE = ---- ----
- PDATA SIZE = ---- ----
- DATA SIZE = 47 38
- IDATA SIZE = 60 7
- BIT SIZE = 3 ----
- END OF MODULE INFORMATION.
- C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)