我是靠谱客的博主 机智红牛,这篇文章主要介绍RFID,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**********************RC522实验开发板例程************************ * CPU:STC89C52RC * 晶振:11.0592MHZ ------------------------------------------------*/ #include <reg52.h> #include <STDIO.H> #include <string.h> #include "main.h" #include "mfrc522.h" #include "lcd.h" /**********Key**********/ sbit K1=P1^4; //切换模式 sbit K2=P1^5; sbit K3=P1^6; sbit K4=P1^7; sbit K5=P3^5; //退出键 /***********RC522**************/ unsigned char code data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE}; //M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令 //4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反 unsigned char code data2[4] = {0,0,0,0x01}; unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; unsigned char g_ucTempbuf[20]; void Delay10ms( ); //延时10ms void delay1(unsigned int z) { unsigned int x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } /******************************************************************************* * 函 数 名 : main * 函数功能 : 主函数 * 输 入 : 无 * 输 出 : 无 *******************************************************************************/ void main() { unsigned char status,i,j; unsigned int temp; int mode=0; //LCD1602初始化 InitLcd1602(); //初始化系统 InitializeSystem(); //复位RC522 PcdReset(); //关闭天线 PcdAntennaOff(); //开启天线 PcdAntennaOn(); //LcdShowStr(0,0," Mode: "); while(1) { status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡 if (status != MI_OK) { InitializeSystem( ); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); continue; }else{ printf("寻卡成功!"); } printf("n"); printf("卡的类型:"); LcdShowStr(0,0,"Mode:"); for(i=0;i<2;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } status = PcdAnticoll(g_ucTempbuf);//防冲撞 if (status != MI_OK) { continue; } 以下为超级终端打印出的内容 printf("n"); printf("卡序列号:"); //超级终端显示 for(i=0;i<4;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } /// status = PcdSelect(g_ucTempbuf);//选定卡片 if (status != MI_OK) { continue; } status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码 if (status != MI_OK) { continue; } status = PcdWrite(1, data1);//写块 if (status != MI_OK) { continue; } while(1) { status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡 if (status != MI_OK) { InitializeSystem( ); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); continue; } status = PcdAnticoll(g_ucTempbuf);//防冲撞 if (status != MI_OK) { continue; } status = PcdSelect(g_ucTempbuf);//选定卡片 if (status != MI_OK) { continue; } status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码 if (status != MI_OK) { continue; } status = PcdValue(PICC_DECREMENT,1,data2);//扣款 if (status != MI_OK) { continue; } status = PcdBakValue(1, 2);//块备份 if (status != MI_OK) { continue; } status = PcdRead(2, g_ucTempbuf);//读块 if (status != MI_OK) { continue; } printf("n"); printf("卡读块:"); //超级终端显示, for(i=0;i<16;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } printf("n"); LED_GREEN = 0; for(i=0;i<250;i++)//先输出一种频率的声音 { for(j=0;j<80;j++); Speaker=~Speaker; } delay1(100); LED_GREEN = 1; delay1(100); LED_GREEN = 0; delay1(200); LED_GREEN = 1; PcdHalt(); } } } / //系统初始化 / void InitializeSystem() { /* P0M1 = 0x0; P0M2 = 0x0; P1M1 = 0x0; P1M2 = 0x0; P3M1 = 0x0; P3M2 = 0xFF; */ P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF; TMOD=0x21; //设T0为方式1,GATE=1; SCON=0x50; TH1=0xF3; //波特率为2400bps TL1=0xF3; TH0=0; TL0=0; TR0=1; ET0=1; //允许T0中断 TR1=1; //开启定时器 TI=1; EA=1; //开启总中断 ES = 1; RI = 1; } /******************************************************************************* * 延时10ms *******************************************************************************/ void Delay10ms(void) { unsigned char a,b,c; for(c=1;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--); }

最后

以上就是机智红牛最近收集整理的关于RFID的全部内容,更多相关RFID内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部