📄 main.lst
字号:
C51 COMPILER V7.50 MAIN 08/29/2005 14:36:53 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN Main.OBJ
COMPILER INVOKED BY: D:\Program Files\Keil\C51\BIN\C51.EXE Main.c BROWSE DEBUG OBJECTEXTEND
line level source
1
2 // MAIN.C
3
4 #include "main.h"
5
6 //--------------------------------------------------------------------
7
8 // 读写卡片操作数据: 发送 接收
9
10 idata BYTE tmpData[32],CommArray[64];
11
12 //--------------------------------------------------------------------
13
14 void main(void)
15 {
16 1 BYTE i,rt;
17 1 BYTE tmpKeyType,tmpBlockNo,tmpKey[6];
18 1 BYTE tmpValue[4],tmpBalance[4];
19 1 idata BYTE psData[16];
20 1
21 1 // Initialize SoftUART
22 1 Init_SoftUART();
23 1
24 1 Delay_10ms(); // 根据具体硬件调整延时,确保读卡模块可靠复位后,系统开始工作 !!!
25 1
26 1 tmpKeyType = 0; // KEY A
27 1 tmpBlockNo = 4; // Block 4
28 1 for(i = 0; i < 6; i++) tmpKey[i] = 0xFF;
29 1
30 1 for(;;)
31 1 {
32 2 rt = CardReady(psData);
33 2 if(rt != 0) rt = CardReady(psData);
34 2 if(rt != 0) rt = CardReady(psData);
35 2 if(rt == 0)
36 2 {
37 3
38 3 //--------------------------------------------------------------------
39 3 //write
40 3 for(i = 0; i < 16; i++) psData[i] = i; //卡数据
41 3 rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
42 3
43 3 //read
44 3 for(i = 0; i < 16; i++) psData[i] = 0x00;
45 3 rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
46 3
47 3 //--------------------------------------------------------------------
48 3 //write
49 3 for(i = 0; i < 16; i++) psData[i] = 0x00;
50 3 rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
51 3
52 3 //read
53 3 for(i = 0; i < 16; i++) psData[i] = 0xFF;
54 3 rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
55 3
C51 COMPILER V7.50 MAIN 08/29/2005 14:36:53 PAGE 2
56 3 //--------------------------------------------------------------------
57 3 //write
58 3 for(i = 0; i < 16; i++) psData[i] = 0xFF;
59 3 rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
60 3
61 3 //read
62 3 for(i = 0; i < 16; i++) psData[i] = 0x00;
63 3 rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);
64 3
65 3 //--------------------------------------------------------------------
66 3 // Purse
67 3 for(i = 0; i < 4; i++) tmpValue[i] = 0x00;
68 3 for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
69 3
70 3 // FormatPurse
71 3 tmpValue[0] = 100;
72 3 rt = FormatPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
73 3 for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
74 3 rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);
75 3
76 3 // Increase
77 3 tmpValue[0] = 10;
78 3 rt = Increase(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
79 3 for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
80 3 rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);
81 3
82 3 // Decrease
83 3 tmpValue[0] = 20;
84 3 rt = Decrease(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
85 3 for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
86 3 rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);
87 3
88 3 //--------------------------------------------------------------------
89 3
90 3 }
91 2 }
92 1 }
93
94 //------------------------------------------------------------------
95 //------------------------------------------------------------------
96 //------------------------------------------------------------------
97
98 void ChangeHexToDbb(BYTE *psData,BYTE *pdData,BYTE length)
99 {
100 1 BYTE i;
101 1 BYTE tmp,t1,t2;
102 1
103 1 for(i = 0; i < length; i++)
104 1 {
105 2 tmp = psData[i];
106 2
107 2 t1 = tmp >> 4;
108 2 if(t1 <= 9) t1 += '0';
109 2 else t1 += 'A' - 10;
110 2
111 2 t2 = tmp & 0x0F;
112 2 if(t2 <= 9) t2 += '0';
113 2 else t2 += 'A' - 10;
114 2
115 2 pdData[2 * i] = t1;
116 2 pdData[2 * i + 1] = t2;
117 2 }
C51 COMPILER V7.50 MAIN 08/29/2005 14:36:53 PAGE 3
118 1
119 1 pdData[2 * length] = 0x03;
120 1 }
121
122 void ChangeDbbToHex(BYTE *psData,BYTE *pdData,BYTE length)
123 {
124 1 BYTE i;
125 1 BYTE t1,t2;
126 1
127 1 for(i = 0; i < length; i += 2)
128 1 {
129 2 t1 = psData[i];
130 2 t2 = psData[i+1];
131 2
132 2 if (t1 >= 'A' && t1 <= 'F') t1 = t1 + 10 - 'A';
133 2 else if(t1 >= 'a' && t1 <= 'f') t1 = t1 + 10 -'a';
134 2 else if(t1 >= '0' && t1 <= '9') t1 = t1 - '0';
135 2
136 2 if (t2 >= 'A' && t2 <= 'F') t2 = t2 + 10 - 'A';
137 2 else if(t2 >= 'a' && t2 <= 'f') t2 = t2 + 10 - 'a';
138 2 else if(t2 >= '0' && t2 <= '9') t2 = t2 - '0';
139 2
140 2 pdData[i / 2] = (t1 << 4) + t2;
141 2 }
142 1 }
143
144 BYTE XORCheck(BYTE *psData,BYTE length)
145 {
146 1 BYTE i;
147 1 BYTE XORData;
148 1
149 1 XORData = 0;
150 1
151 1 for(i = 0; i < length; i++)
152 1 {
153 2 XORData ^= psData[i];
154 2 }
155 1
156 1 return XORData;
157 1 }
158
159 //------------------------------------------------------------------
160 //------------------------------------------------------------------
161 //------------------------------------------------------------------
162
163 void Delay_10ms(void)
164 {
165 1 BYTE i;
166 1
167 1 for(i = 0; i < 200; i++) Delay_52us_NOP();
168 1 }
169
170 void Delay_52us_NOP(void)
171 {
172 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
173 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
174 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
175 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
176 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
177 1 }
178
179 void Delay_26us_NOP(void)
C51 COMPILER V7.50 MAIN 08/29/2005 14:36:53 PAGE 4
180 {
181 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
182 1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
183 1 _nop_();_nop_();_nop_();
184 1 }
185
186 //------------------------------------------------------------------
187 //------------------------------------------------------------------
188 //------------------------------------------------------------------
189
190 void Init_SoftUART(void)
191 {
192 1 PCON = 0x80;
193 1 SCON = 0x50;
194 1
195 1 T2CON = 0x30; // Timer 2 16-bit auto reload
196 1
197 1 RCAP2L = 0xEE; // 0xEE for 19200
198 1 RCAP2H = 0xFF;
199 1 TL2 = 0xEE; // 0xEE for 19200
200 1 TH2 = 0xFF;
201 1
202 1 TR2 = TRUE; // Timer 2 run
203 1
204 1 // ES = TRUE; // Enable serial interrupt
205 1
206 1 }
207
208 void SoftUART_PutByte(BYTE psData)
209 {
210 1 SBUF = psData;
211 1
212 1 while(!TI);
213 1 TI = FALSE;
214 1 }
215
216 BYTE SoftUART_GetByte(void)
217 {
218 1 while(!RI);
219 1 RI = FALSE;
220 1
221 1 return SBUF;
222 1 }
223
224 //------------------------------------------------------------------
225 //------------------------------------------------------------------
226 //------------------------------------------------------------------
227
228 BYTE CardReady(BYTE *CardSNO)
229 {
230 1 BYTE i;
231 1
232 1 //--------------------------------------------------------------------
233 1 // Commamd: 0x60 0x04 0x01 0x61 0x00 0x00 BCC 0x03
234 1 // send 15 byte 7 * 2 + 1 = 15
235 1 //--------------------------------------------------------------------
236 1
237 1 tmpData[0] = 0x60;
238 1 tmpData[1] = 0x04;
239 1 tmpData[2] = 0x01;
240 1 tmpData[3] = 0x61;
241 1 tmpData[4] = 0x00;
C51 COMPILER V7.50 MAIN 08/29/2005 14:36:53 PAGE 5
242 1 tmpData[5] = 0x00;
243 1 tmpData[6] = XORCheck(tmpData,6);
244 1
245 1 ChangeHexToDbb(tmpData,CommArray,7);
246 1
247 1 for(i = 0; i < 15; i++)
248 1 {
249 2 SoftUART_PutByte(CommArray[i]);
250 2 }
251 1
252 1 //--------------------------------------------------------------------
253 1 // Right: 0x60 0x05 SNR0 …SNR3 0x00 BCC 0x03
254 1 // recieve 17 byte 8 * 2 + 1 = 17
255 1 // Error: 0x60 0x01 0x01 BCC 0x03
256 1 // recieve 9 byte 4 * 2 + 1 = 9
257 1 //--------------------------------------------------------------------
258 1
259 1 for(i = 0; i < 17; i++)
260 1 {
261 2 CommArray[i] = SoftUART_GetByte();
262 2 if(CommArray[i] == 0x03) break;
263 2 }
264 1
265 1 if(CommArray[0] != '6') return 1;
266 1 if(i < 10) return 1;
267 1
268 1 ChangeDbbToHex(&CommArray[4],tmpData,8);
269 1
270 1 for(i = 0; i < 4; i++) CardSNO[i] = tmpData[i];
271 1
272 1 //--------------------------------------------------------------------
273 1
274 1 return 0;
275 1
276 1 }
277
278 BYTE ReadCard(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *CardData)
279 {
280 1 BYTE i;
281 1
282 1 //--------------------------------------------------------------------
283 1 // Command: 0x60 0x0A 0x01 0x62 KEYTYPE BlockNo KEY0 … KEY5 BCC 0x03
284 1 // send 27 byte 13 * 2 + 1 = 27
285 1 //--------------------------------------------------------------------
286 1
287 1 tmpData[0] = 0x60;
288 1 tmpData[1] = 0x0A;
289 1 tmpData[2] = 0x01;
290 1 tmpData[3] = 0x62;
291 1 tmpData[4] = KeyType;
292 1 tmpData[5] = BlockNo;
293 1 for(i = 0; i < 6; i++) tmpData[6 + i] = CardKey[i];
294 1 tmpData[12] = XORCheck(tmpData,12);
295 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -