#include <REG52.H> #include <intrins.h> #include <SLE4442.h>
void Delay5us(void) { _nop_(); _nop_(); _nop_(); }
void Delay10us(void) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }
void WriteByte(unsigned char ch) //写一个字符 { unsigned char i; for(i = 8; i > 0; i--) { CARD_CLK = 0; //时钟信号为低 CARD_DATA = (bit)(ch & 0x01); //得到写字符的最右边一位 Delay5us(); //延时 CARD_CLK = 1; //时钟信号为高 ,发出数据 Delay10us(); //延时 ch = ch >> 1; //右移一位 } }
void StartComm(void) //开始命令 { CARD_CLK = 0; //时钟信号为低 CARD_DATA = 1; //数据 1 Delay5us(); //延时 CARD_CLK = 1; //时钟信号为高 Delay5us(); //延时 CARD_DATA = 0; //数据 0 Delay5us(); //延时 }
void StopComm(void) //停止命令 { CARD_CLK = 0; //时钟信号为低 CARD_DATA = 0; //数据 0 Delay5us(); //延时 CARD_CLK = 1; //CLK: H Delay5us(); //延时 CARD_DATA = 1; //IO : H Delay10us(); //延时 }
void SendComm(unsigned char a,unsigned char b,unsigned char c) //发送命令 { StartComm(); //开始发送命令 WriteByte(a); //a: 发命令字 WriteByte(b); //b: 发地址 WriteByte(c); //c: 发数据 StopComm(); //结束发送命令 }
unsigned char ReadByte(void) //读取一个字符 { unsigned char i,ch; ch = 0; for (i = 8; i > 0; i--) { CARD_CLK = 0; ch = ch >> 1; //从低位读起 if((unsigned char)CARD_DATA) ch |= 0x80; Delay5us(); CARD_CLK = 1; Delay5us(); } return ch; }
void ReadMode(unsigned char *pt,unsigned char count) //读模式 { CARD_CLK=0; Delay5us(); do { *pt = ReadByte(); //读入一个字节 pt++; //指针加一 }while(--count); //计数器减一,判断 }
void BreakOperate(void) //断开操作 { CARD_CLK = 0; //时钟信号为低 CARD_RST = 0; //卡复位信号 CARD_DATA = 0; //数据 Delay5us(); //延时 CARD_RST = 1; //卡复位信号 CARD_DATA = 1; //数据 Delay5us(); //延时 CARD_RST = 0; //卡复位信号 Delay5us(); //延时 }
void ReadMainMem(unsigned char addr,unsigned char *pt,unsigned char count) //读IC卡主存 {
SendComm(RMM_COMM,addr,0xff); //发送命令 ReadMode(pt,count); //Delay5us(); BreakOperate(); //IC_Read_LED = ON; }
假设卡里存储的数据 为: 0x01 0x20 0x30 0x40
那么发读的命令后 CARD_DATA 的值是什么呢?
|