我现在在做SAM卡驱动,用的普通IO口,非智能卡接口,谁做过类似的,求技术交流QQ810837178啊 ,对于普通的IO口向PSAM卡如何发送一个字节函数?下面是参考51的
#include <REG54.H>
#include "PSAM.h"
#include "intrins.h"
//#include "UARTUART.h"
#define openclock PSAMClock=0
#define closeclock PSAMClock=1
//#define PSAMPowerEnable PSAMPOWER=0;
//#define PSAMPowerUEnable PSAMPOWER=1;
#define HALF 0
#define FULL 1
typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;
sbit PSAMClock=P1^3;
sbit PSAMData=P1^4;
sbit PSAMRst=P1^7;
//sbit  
SAMPOWER=P2^6;
void etutime(bit time) //etu=(372/f)=5376.3440860215053763440860215054=10752.688172043010752688172043011
{ //2688.1720430107526881720430107527
unsigned short i;
if(time)
{
for(i=0;i<42;i++);
_nop_();_nop_();_nop_();
}
else
{
for(i=0;i<18;i++);
_nop_();_nop_();//_nop_();_nop_();
}
}
void SafeEtu()
{
unsigned char i;
for(i=0;i<17;i++)
{
etutime(FULL);
}
}
//read one byte
bit PSAMReadOneByte(unsigned char *p)
{
union{
unsigned char a;
struct{
unsigned char a0 :1;
unsigned char a1 :1;
unsigned char a2 :1;
unsigned char a3 :1;
unsigned char a4 :1;
unsigned char a5 :1;
unsigned char a6 :1;
unsigned char a7 :1;
}b1;
}c1;
unsigned short n;
bit start,stop;
n=50000;
while(PSAMData&&(--n));
if(n)
{
etutime(HALF);start=(byte)PSAMData;//0
etutime(FULL);c1.b1.a0=(byte)PSAMData;//0
etutime(FULL);c1.b1.a1=(byte)PSAMData;//0
etutime(FULL);c1.b1.a2=(byte)PSAMData;//1
etutime(FULL);c1.b1.a3=(byte)PSAMData;//1
etutime(FULL);c1.b1.a4=(byte)PSAMData;//1
etutime(FULL);c1.b1.a5=(byte)PSAMData;//1
etutime(FULL);c1.b1.a6=(byte)PSAMData;//0
etutime(FULL);c1.b1.a7=(byte)PSAMData;//0
etutime(FULL);stop=(byte)PSAMData;
etutime(FULL);stop=(byte)PSAMData;
*p=c1.a;
while(!PSAMData);
return 1;
}
else return 0;
return 1;
}
//·???????×???
void PSAMSendOneByte(unsigned char dat)
{
union{
byte a;
struct{
byte a0 :1;
byte a1 :1;
byte a2 :1;
byte a3 :1;
byte a4 :1;
byte a5 :1;
byte a6 :1;
byte a7 :1;
}b1;
}c1;
bit parity;
c1.a=dat;
parity=0;
if(c1.b1.a0)parity=~parity;
if(c1.b1.a1)parity=~parity;
if(c1.b1.a2)parity=~parity;
if(c1.b1.a3)parity=~parity;
if(c1.b1.a4)parity=~parity;
if(c1.b1.a5)parity=~parity;
if(c1.b1.a6)parity=~parity;
if(c1.b1.a7)parity=~parity;
// EA = 0 ;
PSAMData=0;etutime(FULL);
PSAMData=c1.b1.a0;etutime(FULL);
PSAMData=c1.b1.a1;etutime(FULL);
PSAMData=c1.b1.a2;etutime(FULL);
PSAMData=c1.b1.a3;etutime(FULL);
PSAMData=c1.b1.a4;etutime(FULL);
PSAMData=c1.b1.a5;etutime(FULL);
PSAMData=c1.b1.a6;etutime(FULL);
PSAMData=c1.b1.a7;etutime(FULL);
PSAMData=parity;etutime(FULL);
PSAMData=1;etutime(FULL);
// EA = 1;
}
unsigned char ReadData(unsigned char *p,unsigned char len)
{
unsigned char i;
// EA = 0 ;
for(i=0;i<len;i++)
{
if(PSAMReadOneByte(&p
)==0)return i;
}
// EA = 1 ;
return i;
}
bit ReadATR()
{
unsigned char idata aa[20];
unsigned char i;
EA = 0 ;
if(ReadData(aa,4)<4)
{
EA = 1;
return 0;
}
i = aa[1]&0x0f;
if(ReadData(&aa[4],i)<i)
{
EA = 1;
return 0;
}
EA = 1 ;
return 1;
}
unsigned short SendCmd( unsigned char CLA,
unsigned char INS,
unsigned char P1,
unsigned char P2,
unsigned char Lc,
unsigned char Le,
unsigned char *DATA,
unsigned char RLen
)
{
unsigned char i;
unsigned char a;
EA = 0 ;
PSAMSendOneByte(CLA);
PSAMSendOneByte(INS);
PSAMSendOneByte(P1);
PSAMSendOneByte(P2);
if(Lc!=0)
{
PSAMSendOneByte(Lc);
if(ReadData(&a,1)!=1)
{
EA = 1;
return 0;
}
SafeEtu();
for( i = 0 ; i < Lc ; i++ )
{
PSAMSendOneByte(DATA);
}
if(Le==0)
{
i = ReadData(DATA,RLen);
EA = 1;
return (unsigned short)DATA[i-2]*0x100+DATA[i-1];
}
}
if(Le!=0)
{
PSAMSendOneByte(Le);
if(Lc==0)
{
if(ReadData(&a,1)!=1)
{
EA = 1;
return 0;
}
}
i = ReadData(DATA,RLen+2);
// SendStr(DATA,i);
EA = 1;
return (unsigned short)DATA[i-2]*0x100+DATA[i-1];
}
if((Lc==0)&&(Lc==0))
{
PSAMSendOneByte(0);
i = ReadData(DATA,3);
EA = 1;
return (unsigned short)DATA[i-2]*0x100+DATA[i-1];
}
EA = 1;
}
unsigned short SendCmd1( unsigned char CLA,
unsigned char INS,
unsigned char P1,
unsigned char P2,
unsigned char Lc,
unsigned char Le,
unsigned char *DATA,
unsigned char RLen
)
{
unsigned char i;
unsigned char a;
EA = 0 ;
SafeEtu();
PSAMSendOneByte(CLA);
PSAMSendOneByte(INS);
PSAMSendOneByte(P1);
PSAMSendOneByte(P2);
PSAMSendOneByte(Lc);
if(ReadData(&a,1)!=1)
{
EA = 1;
return 0;
}
SafeEtu();
for( i = 0 ; i < Lc ; i++ )
{
PSAMSendOneByte(DATA);
}
Le = Le;
// PSAMSendOneByte(Le);
i = ReadData(DATA,RLen+2);
// SendStr(DATA,i);
EA = 1 ;
return (unsigned short)DATA[i-2]*0x100+DATA[i-1];
}
unsigned short GetResponse(unsigned char *p,unsigned char len)
{
unsigned char i,a;
EA = 0 ;
SafeEtu();
PSAMSendOneByte(0x00);
PSAMSendOneByte(0xc0);
PSAMSendOneByte(0x00);
PSAMSendOneByte(0x00);
PSAMSendOneByte(len);
if(ReadData(&a,1)!=1)
{
EA = 1;
return 0;
}
EA = 1 ;
i = ReadData(p,len+2);
return (unsigned short)p[i-2]*0x100+p[i-1];
}
unsigned char PSAMInit()//
{
bit a=1;
unsigned char i;
closeclock;
PSAMRst=0;
PSAMData=1;
closeclock;
for(i=0;i<50;i++)
{
etutime(FULL);
}
openclock;
for(i=0;i<50;i++)
{
etutime(FULL);
}
PSAMRst = 1 ;
if(ReadATR())
{
return 0;
}
// i = ReadData(aa,16); //3b 6c 00 02 13 62 86 38 18 43 56 07 1e 10 2d 23
// SendStr(aa,i); //3B 6C 00 02 13 62 86 38 18 43 56 07 1E 10 2D 23
else
{
PSAMReadOneByte("1");
ReadData("1",1);
SendCmd(1,1,1,1,1,1,"1",1);
return 1;
}
}
一周热门 更多>