📄 readcardtest.bas
字号:
$regfile = "m64def.dat"
$crystal = 1000000
Declare Sub Readcardsn
Declare Sub Outbit
Declare Sub Inbit
'定义spi脚的状态
Config Portg.0 = Input
Config Portg.1 = Output
Config Portg.2 = Output
Config Portg.3 = Output
Config Portg.4 = Output
Config Portc = Output
Config Portb = Output
Config Portb.6 = Output
Config Portb.5 = Input
Config Portd.0 = Input
Config Portd.1 = Input
Config Portd.2 = Input
Config Portd.3 = Input
Config Portd.4 = Input
Config Portd.5 = Input
Config Porte.4 = Input
Config Porte.5 = Input
Config Porte.6 = Input
Config Porte.7 = Input
Config Portf.0 = Input
Config Portf.1 = Input
Config Portf.2 = Output
Config Portf.3 = Output
Config Portf.4 = Output
Config Portf.5 = Output
Config Portf.6 = Output
Config Lcdpin = Pin , Db4 = Porte.3 , Db5 = Porte.2 , Db6 = Porte.1 , Db7 = Porte.0 , E = Portd.7 , Rs = Portd.6
Config Lcd = 16 * 2
Config Kbd = Porta
Portc = 0
Portb = 0
'输出端口初始化全部为0
Portb.5 = 1
'钥匙报警输入端口
Portd = 15
'd口的pd0-pd3 设置为高电平
Porte = 240
'e口的pe4-pe7 设置为高电平用来监测中断输入的
Portf.0 = 1
'注册卡
Portf.1 = 1
'注册密码的输入
Portb.6 = 1
'给光电输出供电
Portd.4 = 1
Portd.5 = 1
'左右光电的输入端口,如果为0的话那就是光电处于闭合状态的
'初始化数据
Dim Spi_outdate As Byte , Spi_indate As Byte , Spi_temp As Byte , Spi_tempi As Byte , Spi_tempk As Byte , Spi_i As Byte , Spi_ii As Byte
Dim Music_s As Integer , Music_f As Integer , Music_i As Byte , Music_j As Byte
Dim Card_savedsn(4) As Byte , Card_tempsn(4) As Byte , Card_i As Byte , Card_outdat As Byte , Card_total As Byte
Dim Kmfs As Byte , Outbit As Byte , Changtai As Byte , Alarm As Byte
Dim Sta As Byte
Dim Sta0_i As Byte , Sta0_j As Byte , Sta0_temp As Integer , Sta0_tempi As Integer , Sta0_tempj As Integer , Sta0_tempk As Byte , Sta0_templ As Integer
Dim Sta1_temp As Byte , Sta1_pf0 As Byte , Sta1_tempi As Integer , Sta1_tempj As Integer , Sta1_i As Byte , Sta_j As Byte , Sta1_tempk As Integer
Dim Sta2_temp As Byte , Sta2_tempi As Integer , Sta2_tempk As Integer , Sta2_tempj As Integer , Sta2_i As Byte , Sta2_j As Byte , Sta2_savedpassword(12) As Byte , Sta2_temppassword(12) As Byte , Sta2_tempcont As Byte
Dim Sta3_temp As Byte , Sta3_i As Byte , Sta3_j As Byte , Sta3_keydate As Byte , Sta3_number(22) As Byte , Sta3_static As Bit , Sta3_cont As Byte , Sta3_tempi As Byte , Sta3_tempk As Byte , Sta3_tempj As Integer , Sta3_tempm As Integer , Sta3_fenzu As Byte , Sta3_templ As Byte
Dim Sta4_temp As Byte , Sta4_i As Byte , Sta4_j As Byte , Sta4_keydate As Byte , Sta4_needpassword(4) As Byte , Sta4_static As Bit , Sta4_cont As Byte
Dim Sta5_i As Byte
Dim Keybd_date As Byte , Keybd_sta As Bit
Dim Need_password(4) As Byte , Need_temp As Byte
Dim Main_i As Byte
Dim Delcard_i As Byte , Delcard_tempi As Integer , Delcard_tempj As Integer , Delcard_tempk As Byte , Delcard_j As Byte , Delcard_k As Byte , Delcard_add As Integer
Dim Timer0_i As Integer , Timer0_j As Integer , Timer0_alarm As Integer , Timer0_leds As Integer , Timer0_doorclose As Byte
Dim Int5_i As Byte , Int5_j As Byte
Dim Adc_val As Word , Fifo_lengh As Byte , Readcardsn_temp(4) As Byte , Xx As Byte
Dim Int_i As Byte , Int0_p As Byte , Int1_p As Byte , Int2_p As Byte , Int3_p As Byte
Dim Tempi As Byte , Tempj As Byte , Temp As Byte , Tempk As Byte
Dim A As Byte , B(4) As Byte , Bbb As Byte , I As Byte , Aaa As Byte
Dim C(4) As Byte , First As Byte , Int_cont As Bit , Sta2_tempii As Integer , Sta2_ii As Byte , Sta2_tempjj As Integer
Dim Password_entertimes As Byte , Int4_i As Byte ,
Dim Jiance As Byte , T_jiance As Integer
Dim Moto_time As Integer
Dim Int_ie As Bit , Int2_cont As Byte
Dim Staa As Bit
Sound Pinc.7 , 100 , 200
Do
B(1) = 1
B(2) = 1
B(3) = 1
B(4) = 1
Waitms 20
'开始读卡
Portg.2 = 1
Waitus 200
Portg.2 = 0
Waitus 200
'每次循环前先复位FM1702
Do
Portg.3 = 0
'接FM1702片选,低电平有效,表示开始要发送数据流
A = &H82
'&h82是读command寄存器的地址
Outbit
'发送该地址
Inbit
'取回a地址的数据
Bbb = Temp
Portg.3 = 1
'发送数据流结束
nop
If Bbb = 0 Then
'如果command寄存器里面是空的表明硬件复位完成,开始初始化芯片设置
' Portc.1 = 1
Exit Do
End If
Loop
nop
Portg.3 = 0
' Waitus 5
A = &H0
'选择总线的地址
Outbit
A = &H0
'选择总线类型
Outbit
Portg.3 = 1
nop
Portg.3 = 0
'Waitus 5
A = &H22
Outbit
A = &H5B
Outbit
'&h22是txcontrol(tx1,tx2)的写入地址,&H5b是数据,功能是开启tx1,tx2
Portg.3 = 1
nop
Portg.3 = 0
'Waitus 5
A = &H1E
Outbit
A = &H7
Outbit
'发送7bit ,bitFrame = 7
Portg.3 = 1
nop
Portg.3 = 0
'Waitus 5
A = &H4C
Outbit
A = &H2
Outbit
'设定发送结束开定时器,接收开始关定时器
Portg.3 = 1
nop
Portg.3 = 0
'Waitus 5
A = &H44
Outbit
A = &H3
Outbit
'关闭RCR
Portg.3 = 1
nop
Portg.3 = 0
'Waitus 5
A = 18
Outbit
A = 1
Outbit
'屏蔽crypto的第一位,将fifo寄存器清零
Portg.3 = 1
nop
Portg.3 = 0
' Waitus 5
A = &H62
Outbit
A = &H0
Outbit
'选择mifare卡!
Portg.3 = 1
'以上是对fm1702的初始设置
nop
Portg.3 = 0
' Waitus 5
A = &H4
Outbit
A = &H52
Outbit
'往fifo寄存器里写&h52 ,是对卡的rquest命令,选择卡片范围内的所有卡
Portg.3 = 1
nop
Portg.3 = 0
' Waitus 5
A = &H88
Outbit
Inbit
Bbb = Temp
If Bbb = 1 Then
' Portb.4 = 1
' Waitms 500
End If
Portg.3 = 1
nop
Portg.3 = 0
' Waitus 5
A = &H2
Outbit
A = &H1E
Outbit
'往fm1702的command寄存器里面写&H1E指令,用来把刚才写的&h52数据发送给卡
Portg.3 = 1
nop
' For I = 1 To 11
Waitus 100
'等待接收卡发过来的数据的时间
'100ms时间很长了,可以改小,做10次循环,如果1秒钟内没取到数据就重新循环
'因发送完&h52,如果有卡的话会返回&h4,&h0两个数据
Portg.3 = 0
' Waitus 5
A = &H88
Outbit
Inbit
Bbb = Temp
'读取fifo寄存器里面的数据长度
If I = 10 Then
Portg.3 = 1
' Exit Do
'退出循环重新开始读卡
End If
If Bbb = 2 Then
Portg.3 = 1
' Exit For
End If
Portg.3 = 1
' Next I
'如果能运行到这里表明fifo寄存器有数据
Portg.3 = 0
A = &H84
Outbit
Inbit
C(1) = Temp
A = &H4
Outbit
Inbit
C(2) = Temp
'取fifo里的两个数据,将他们放在c(1),c(2)里面
Portg.3 = 1
If C(1) = 4 And C(2) = 0 Then
'如果取到的两个数据是4,0的话,就表明数据正确,开始读取射频卡id
Portg.3 = 0
A = &H4
Outbit
A = &H93
Outbit
A = &H20
Outbit
'往fifo寄存器里面写&H92,&h20,这两个数据 ,是给卡的指令 用于让卡把卡号发过来
Portg.3 = 1
nop
Portg.3 = 0
A = &H2
Outbit
A = &H1E
Outbit
' 往command寄存器里写&h1e,把fifo寄存器的卡指令发给卡
Portg.3 = 1
Waitus 100
'等待卡把卡号发过来 100ms时间足够了
Portg.3 = 0
'Waitus 5
A = &H84
Outbit
Inbit
B(1) = Temp
A = &H4
Outbit
Inbit
B(2) = Temp
A = &H4
Outbit
Inbit
B(3) = Temp
A = &H4
Outbit
Inbit
B(4) = Temp
'连续读取fifo四个数据,因为返回的卡号只有4个byte
'存放在b(1)b(2)b(3)b(4)
Portg.3 = 1
If B(1) <> B(2) And B(1) <> B(3) And B(1) <> B(4)then
' If B(2) <> B(3) And B(3) <> B(4) Then
Card_tempsn(1) = B(1)
Card_tempsn(2) = B(2)
Card_tempsn(3) = B(3)
Card_tempsn(4) = B(4)
Portc.0 = 1
Ddrb = &HF
Portb = &HF
Sound Pinc.7 , 100 , 200
Else
Ddrb = &HF
Portb = 0
End If
Else
Ddrb = &HF
Portb = 0
End If
Loop
End
Sub Outbit
For I = 7 To 0 Step -1
Waitus 1
Portg.1 = 0
'clk
Tempi = 2 ^ I
Tempk = A And Tempi
If Tempk = 0 Then
Portg.4 = 0
Else
Portg.4 = 1
'mosi
End If
Portg.1 = 1
'clk
nop
Next I
Portg.1 = 0
End Sub
Sub Inbit
Temp = 0
For I = 7 To 0 Step -1
Portg.1 = 1
'clk
If Ping.0 = 1 Then
'miso
Tempi = 2 ^ I
Temp = Temp + Tempi
End If
Portg.1 = 0
nop
nop
Next I
Portg.1 = 0
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -