📄 ic_card.c
字号:
#include <stc89c51.h>
#include "source.h"
/* IC卡初始化 */
void ic_init(void)
{ ic_error=0; //密码校验判断
psc1=0; //密码第一个字节
psc2=0; //密码第二个字节
}
/* 延时25us */
void delay(void)
{ uchar i;
for(i=25;--i;);
}
/* 延时10ms */
void ms10(void)
{ uchar i,j;
for(i=40;i--;)
{ for(j=250;j--;);
}
}
/* 单个脉冲 */
void pulse_one(void)
{ clk=1;delay();
clk=0;delay();
}
/* 多脉冲 */
void pulse(uchar num)
{ uchar i;
for(i=num;i--;) pulse_one();
}
/* IC卡复位 */
void card_reset(void)
{ uchar i;
ic_error=0;
rst=1;
delay();
pulse_one();
rst=0;
delay();
for(i=32;i--;) pulse_one();
io=1;
}
/* 发送命令字符 */
void send_char(uchar d)
{ uchar i;
for(i=8;i--;)
{ if(d&0x01) io=1;
else io=0;
delay();
clk=1;
delay();
clk=0;
d>>=1;
}
}
/* 发送命令 */
void command(uchar one,uchar two,uchar three)
{ rst=0;
clk=0;
rst=1;
delay();
send_char(one);
send_char(two);
send_char(three);
delay();
rst=0;
delay();
}
/* 读卡内容 */
void read(uint add,uchar len,uchar *cdata)
{ uchar i,temp,d;
temp=(uchar)add;
add>>=2;
i=(uchar)add;
i&=0xc0;
i|=0x0e;
command(i,temp,0);
io=1;
for(i=len;i--;)
{ for(temp=8;temp--;)
{ d>>=1;
pulse_one();
if(io) d|=0x80;
else d&=0x7f;
}
*cdata++=d;
}
rst=1;
delay();
rst=0;
delay();
}
/* 不带保护位写卡 */
void wr_unpro(uint add,uchar len,uchar *d)
{ uchar i,j,temp;
temp=(uchar)add;
add>>=2;
i=(uchar)add;
i&=0xc0;
i|=0x33;
for(j=len;j--;)
{ command(i,temp,*d++);
pulse(103);
if(temp==0xff) temp=0,i+=0x40;
else temp++;
}
pulse(100);
}
/* 带保护位写卡 */
void wr_pro(uint add,uchar len,uchar *d)
{ uchar i,j,temp;
temp=(uchar)add;
add>>=2;
i=(uchar)add;
i&=0xc0;
i|=0x31;
for(j=len;j--;)
{ command(i,temp,*d++);
pulse(103);
if(temp==0xff) temp=0,i+=0x40;
else temp++;
}
pulse(100);
}
/* 擦除卡内容 */
void erase(uint add,uchar len)
{ uchar i,j,temp;
temp=(uchar)add;
add>>=2;
i=(uchar)add;
i&=0xc0;
i|=0x33;
for(j=len;j--;)
{ command(i,temp,0xff);
pulse(103);
if(temp==0xff) temp=0,i+=0x40;
else temp++;
}
pulse(100);
}
/* 擦写连续操作 */
void erase_and_wr(uint add,uchar len,uchar *d)
{ uchar i,j,temp;
temp=(uchar)add;
add>>=2;
i=(uchar)add;
i&=0xc0;
i|=0x33;
for(j=len;j--;)
{ command(i,temp,*d++);
pulse(203);
if(temp==0xff) temp=0,i+=0x40;
else temp++;
}
}
/* 密码校验 */
void check_secret(void)
{ uchar i,*d;
read(0x03fd,1,d);
if(!*d) ic_error=1;
else
{ ic_error=0;
i=*d;
i<<=1;
command(0xf2,0xfd,i);
pulse(203);
command(0xcd,0xfe,psc1);
pulse(203);
command(0xcd,0xff,psc2);
pulse(203);
*d=0xff;
wr_unpro(0x03fd,1,d);
read(0x03fd,1,d);
if(*d!=0xff) ic_error=1;
else ic_error=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -