📄 main.lst
字号:
C51 COMPILER V8.02 MAIN 01/26/2007 15:08:37 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.C COMPACT BROWSE DEBUG OBJECTEXTEND
line level source
1 /****************************************************************************
2 * File : main.c *
3 * COPYRIGHT BY HUOYAN LTD.COMPANY *
4 * Version: V1.2 *
5 * *
6 * Created: 07.Aug.2006 *
7 * Last Change: 25.DEC.2006 *
8 * *
9 * Author: sun,nil *
10 * *
11 * Compiler: KEIL C51 V7.10 *
12 * *
13 * Description: AT89S52 Firmware for HY502 Serial Reader *
14 * This project is designed for HY502XX *
15 * OSC use 11.0592MHz crystal. *
16 ****************************************************************************/
17
18 /******************* www.ehuoyan.com *************************************/
19 /*-------------------------------------------------------------------------*/
20 #include "main.h"
21 #include "hy502.h"
22
23 #define uchar unsigned char
24 #define uint unsigned int
25
26 bit Cardinner=1; // 卡在天线区状态 1 无卡,0有卡
27
28 bit ack; //应答标志
29 uchar code cStatus1[]="Ok!";
30
31
32 uchar keytype=0x60; // 密钥标识:60为PICC_AUTHENT1A(61为PICC_AUTHENT1B)
33 uchar idata g_cBeeps=0x10;
34
35 bit g_bReceCommandOk; //flag of command receive OK
36 bit g_bReceAA; //flag of last received byte is "0xaa", used in interrupt
37 bit g_bCard; //flag of card in
38
39
40 /////////////////////////////////////////////////////////////////////////////////////////////////////
41 // 延时函数
42 void delay(unsigned int x)
43 {
44 1 while(x)
45 1 {
46 2 x--;
47 2 }
48 1 }
49
50 //////////**********IIC FUNCTION*********////////////////
51
52 /*****************************************************************************
53 *function: IIC start condition
54 *****************************************************************************/
55 void I2CStart(void)
C51 COMPILER V8.02 MAIN 01/26/2007 15:08:37 PAGE 2
56 {
57 1 SDA = 1;
58 1 SCL = 1;
59 1 SDA = 0;
60 1 _nop_();
61 1 SCL = 0;
62 1 }
63 /*****************************************************************************
64 *function: IIC stop condition
65 *****************************************************************************/
66 void I2CStop(void)
67 {
68 1 SCL = 0;
69 1 SDA = 0;
70 1 _nop_();
71 1 SCL = 1;
72 1 SDA = 1;
73 1 }
74 /*****************************************************************************
75 *function: IIC wait ACK
76 *****************************************************************************/
77 bit I2CWaitAck(void)
78 {
79 1 unsigned char cErrTime = 255;
80 1 SDA = 1;
81 1 _nop_();
82 1 SCL = 1;
83 1 while(SDA)
84 1 {
85 2 cErrTime--;
86 2 delay(10);
87 2 if (0 == cErrTime)
88 2 {
89 3 I2CStop();
90 3 return FAILURE;
91 3 }
92 2 }
93 1 SCL = 0;
94 1 return SUCCESS;
95 1 }
96 /*****************************************************************************
97 *function: IIC send ACK
98 *****************************************************************************/
99 void I2CSendAck(void)
100 {
101 1 SDA = 0;
102 1 _nop_();
103 1 SCL = 1;
104 1 SCL = 0;
105 1 }
106 /*****************************************************************************
107 *function: IIC send Not ACK
108 *****************************************************************************/
109 void I2CSendNotAck(void)
110 {
111 1 SDA = 1;
112 1 _nop_();
113 1 SCL = 1;
114 1 SCL = 0;
115 1 }
116 /*****************************************************************************
117 *function: send a byte over IIC bus
C51 COMPILER V8.02 MAIN 01/26/2007 15:08:37 PAGE 3
118 *****************************************************************************/
119 void I2CSendByte(unsigned char cSendByte)
120 {
121 1 unsigned char data i = 8;
122 1 while (i--)
123 1 {
124 2 SCL = 0;
125 2 SDA = (bit)(cSendByte & 0x80);
126 2 cSendByte += cSendByte;
127 2 _nop_();
128 2 SCL = 1;
129 2 }
130 1 SCL = 0;
131 1 }
132 /*****************************************************************************
133 *function: receive a byte over IIC bus
134 *****************************************************************************/
135 unsigned char I2CReceiveByte(void)
136 {
137 1 unsigned char data i = 8;
138 1 unsigned char data cR_Byte = 0;
139 1 SDA = 1;
140 1 while (i--)
141 1 {
142 2 cR_Byte += cR_Byte;
143 2 SCL = 0;
144 2 _nop_();
145 2 _nop_();
146 2 SCL = 1;
147 2 cR_Byte |= (unsigned char)SDA;
148 2 }
149 1 SCL = 0;
150 1 return cR_Byte;
151 1 }
152 /*****************************************************************************
153 *function: read data from HY502 over IIC bus
154 *****************************************************************************/
155 unsigned char IicReadHY502(unsigned char *cP)
156 {
157 1 unsigned char cCnt;
158 1 unsigned char cCheckSum = 0;
159 1 for (cCnt=0; cCnt<0xFF; cCnt++) // wait HY502 working, while it's working, it will send "not ACK" to IIC
-master
160 1 {
161 2 delay(1);
162 2 I2CStart();
163 2 I2CSendByte(READ_HY502);
164 2 if (I2CWaitAck() == SUCCESS)
165 2 {
166 3 break;
167 3 }
168 2 }
169 1 if (0xFF == cCnt)
170 1 {
171 2 return FAILURE;
172 2 }
173 1 cP[0]=2;
174 1 for (cCnt=0; cCnt<cP[0]; cCnt++) // in the protocol, cP[0] is the length of this data package
175 1 {
176 2 cP[cCnt] = I2CReceiveByte();
177 2 I2CSendAck();
178 2 cCheckSum ^= cP[cCnt];
C51 COMPILER V8.02 MAIN 01/26/2007 15:08:37 PAGE 4
179 2 }
180 1 cP[cCnt] = I2CReceiveByte();
181 1 I2CSendNotAck();
182 1 I2CStop();
183 1 if (cCheckSum != cP[cCnt])
184 1 {
185 2 return FAILURE;
186 2 }
187 1 else
188 1 {
189 2 return SUCCESS;
190 2 }
191 1 }
192 /*****************************************************************************
193 *function: send data to HY502 over IIC bus
194 *****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -