《I2C总线接口和简易IC卡读写器.doc》由会员分享,可在线阅读,更多相关《I2C总线接口和简易IC卡读写器.doc(8页珍藏版)》请在金锄头文库上搜索。
1、$regfile = M16def.dat$crystal = 11059200$baud = 9600Dim I As Byte , Temp As ByteDim Data1(8) As Byte , Data2(8) As ByteDim Card_ok As BitConfig Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portd.5 , Rs = Portd.3Config Lcd = 16 * 4 定义LCD显示屏接口Config Scl = Portc.0
2、定义PPortc.0为I2C总线的SclConfig Sda = Portc.1 定义PPortc.1为I2C总线的DdaConst Adresw = &HA0 定义IC卡的写地址指令字Const Adresr = &HA1 定义IC卡的读地址指令字Config Pinc.0 = Input , Pinc.1 = Input , Pinc.2 = InputPortc.0 = 1 : Portc.1 = 1 : Portc.2 = 1 定义3个I/O口为输入开关接口Cursor Off Noblink 清LCD显示Do Cls Locate 1 , 1 : Lcd Demo for IC_Ca
3、rd 显示提示字符 If Pinc.2 = 1 Then 检测有无IC卡插入 Locate 2 , 1 : Lcd No IC_Card 无IC卡插入 Wait 1 Else 有IC卡插入 If Pinc.0 = 0 Then 检测系统设置为写卡方式? Cls Locate 1 , 1 : Lcd Enter Password: 写卡方式,要求输入密码字 I = 1 Locate 2 , 1 Do Temp = Inkey() 由RS-232口接收PC输入 If Temp 0 Then 的密码字符, Data1(i) = Temp : I = I + 1 长度为8个 Lcd Chr(temp)
4、 同时在LCD上显示密码 Print Chr(temp); End If Loop Until I 8 Print For I = 1 To 8 将8个密码字写入IC卡中 I2cstart 写入地址为1-8 I2cwbyte Adresw I2cwbyte I I2cwbyte Data1(i) I2cstop Waitms 10 Next If Pinc.1 = 0 Then For I = 1 To 8 如果系统设置为修改系统密码时 Writeeeprom Data1(i) , I 将密码写入AVR的EEPROM中 Next 写入地址为1-8 End IfFor I = 1 To 8 读I
5、C卡刚写入的密码 I2cstart I2cwbyte Adresw I2cwbyte I I2cstart I2cwbyte Adresr I2crbyte Data2(i) , Nack I2cstop NextCard_ok = 0 For I = 1 To 8 判别写入密码同用户输入的密码 If Data1(i) Data2(i) Then 是否相同 Card_ok = 1 Exit For End If NextLocate 2 , 1 If Card_ok = 0 Then Lcd Write Card ok! 相同,写卡完成 Else Lcd No or Bad Card!? 不同
6、,写卡操作有误或坏的IC卡 End If Do Loop Until Pinc.2 = 1 等待用户抽出IC卡 Else 系统为读卡方式 Cls : Locate 1 , 1For I = 1 To 8 I2cstart I2cwbyte Adresw I2cwbyte I I2cstart I2cwbyte Adresr I2crbyte Data1(i) , NackI2cstop 读IC卡上的密码字8个 Lcd Chr(data1(i) 显示在LCD上 Next For I = 1 To 8 Readeeprom Data2(i) , I 读系统密码字8个 NextCard_ok = 0 For I = 1 To 8 将系统密码和IC卡上的密码 If Data1(i) Data2(i) Then 比较 Card_ok = 1 Exit For End If Next Locate 2 , 1 If Card_ok = 1 Then Lcd Password not ok! 密码不符 Else Lcd Password Ok! 密码相符 End If Do Loop Until Pinc.2 = 1 等待用户将IC抽出 End If End IfLoop 返回循环End