您当前的位置:五五电子网电子知识单片机-工控设备AVR单片机AVR单片机IC卡程序 正文
AVR单片机IC卡程序

AVR单片机IC卡程序

点击数:7878 次   录入时间:03-04 11:33:32   整理:http://www.55dianzi.com   AVR单片机
     if((temp[0]&0x07)==0x06)  i=0x04;
     if((temp[0]&0x07)==0x04)  i=0x00;
     WriteSafeMemory(0,i);
     for(i=1;i<4;i++,p++)
         {Command(0x33,i,*p);
         Process();}
     WriteSafeMemory(0,0xff);
     ReadSafeMemory(temp);
     if((temp[0]&0x07)==0x07) return(0x1);
     
 }
 return(0);
}
//*************SLE4442函数结束*****************//

//*************数据变换**********//
void Change(uchar *Source,uchar *Destination,uchar N)
{uchar i,temp;
 for(i=0;i<N;i++)
 {temp=Source[i];
  Destination[i]=temp>>4;
  Destination[2*i+1]=temp&0x0f;}
}

//***********密码错误报警***********************//
void Buzzle_Password(void)
{uchar i;
 for(i=0;i<2;i++)
    {Voice(0);
     DelayMs(1000);
     Voice(1);
     DelayMs(1000);}
}
 
//**********非法卡错误报警*************************// 
void Buzzle_Card(void)
{uchar i;
 for(i=0;i<2;i++)
    {Voice(0);
     DelayMs(3000);}
}

//*************余额不足报警**********************//
void Buzzle_Money(void)
{uchar i;
 for(i=0;i<1;i++)
  {Buzzle_Password();
   Buzzle_Card();}
}
  
//*********************读卡函数********************//
//说明:
//函数使用的数组参数用来存放读出的余额值;
//返回值信息:
//0:卡坏!
//1:非法卡(特征值不正确)
//2:非法卡(特征值正确,帐号不正确)
//3:读卡成功!
uchar Read_Card(uchar *p)
{uchar i,tag=0,temp[4];
 ReadSafeMemory(temp);
 if(temp[0]==0x07)
  {ReadMainMemory(32,p,14);//读主存储器14字节:32-35特征码;36-3A帐号;3B-3F余额
   if(p[0]==0x00&&p[1]==0x0f&&p[2]==0xf0&&p[3]==0xff)//特征码:0x00,0x0f,0xf0,0xff
     {for(i=0;i<10;i++)
        if((p[i+4]>=0&&p[i+4]<=9)) tag=tag+1;
     if(tag!=10) return(2);
     else return(3);
    }
   else
    return(1);
  }
 else return(0);
}

//*********************卡初始化函数********************//
//说明:
//函数使用的数组参数用来存放写入的的ID值;
//返回值信息:
//2:初始化失败!
//3:初始化成功!
uchar Initial_Card(uchar *p)
{uchar Psw[3]={0xff,0xff,0xff};
 uchar i,j,temp=0;
 uchar tp[20];
// ResetIC(ICcode);//IC卡复位,读出复位后的厂家信息代码A2131091;
  j=VerifyPassword(Psw);
  WriteMainMemory(32,0x00);//写特征码:
  WriteMainMemory(33,0x0f);
  WriteMainMemory(34,0xf0);
  WriteMainMemory(35,0xff);
  for(i=0;i<5;i++)//写帐号
  WriteMainMemory(36+i,p[i]);//从32+i地址开始写5字节帐号;
  for(i=0;i<5;i++)
  WriteMainMemory(41+i,0);//从32+i地址开始写5字节初始化金额0000.0
  j=Read_Card(tp);
  if(j==3)
   {for(i=0;i<10;i++)
     if(p[i]==tp[i+4]) temp=temp+1;
  }
  if(temp==10)  return(3);
  else return(2);
}

//***************卡修复函数********************//
//说明:
//返回值信息:
//0:修复失败!
//1:修复成功!                                                 
uchar Repair_Card(void)
{uchar Psw[3]={0xff,0xff,0xff};
 uchar i,j,temp;
 i=VerifyPassword(Psw);
 return(i);
}

//********************加卡函数***********//
void Add_Card(uchar *p)
{uchar i;
 uchar temp[14];
 i=Read_Card(temp);
 if(i==3)
     {temp[13]=temp[13]+p[4];
     if(temp[13]>9) {temp[13]=temp[13]-10;temp[12]=temp[12]+1;}
     temp[12]=temp[12]+p[3];
     if(temp[12]>9) {temp[12]=temp[12]-10;temp[11]=temp[11]+1;}
     temp[11]=temp[11]+p[2];
     if(temp[11]>9) {temp[11]=temp[11]-10;temp[10]=temp[10]+1;}
     temp[10]=temp[10]+p[1];
     if(temp[10]>9) {temp[10]=temp[10]-10;temp[9]=temp[9]+1;}
     
     WriteMainMemory(41,temp[9]);
     WriteMainMemory(42,temp[10]);
     WriteMainMemory(43,temp[11]);
     WriteMainMemory(44,temp[12]);
     WriteMainMemory(45,temp[13]);
    }
}

//********************减卡函数***********//
void Sub_Card(uchar *p)
{uchar i,B_Flag;
 uchar temp[14];
 i=Read_Card(temp);
 if((i==3)&&(!(temp[9]<p[0])))
     {if(temp[13]<p[4]) {temp[13]=temp[13]+10-p[4];B_Flag=1;}
     else temp[13]=temp[13]-p[4];
     //以上处理小数点右边的数字;
     if(B_Flag==1)
      {if(temp[12]==0) {temp[12]=9;B_Flag=0;}
       else temp[12]=temp[12]-1;}
     //以上对存在借位情况时对小数点左边第一位进行预处理;
     if(temp[12]<p[3]) {temp[12]=temp[12]+10-p[3];B_Flag=1;}
     else temp[12]=temp[12]-p[3];
     //以上处理小数点小数点左边第一位数字; 
      if(B_Flag==1)
      {if(temp[11]==0) {temp[11]=9;B_Flag=0;}
       else temp[11]=temp[11]-1;}
     //以上对存在借位情况时对小数点左边第二位进行预处理;
      if(temp[11]<p[2]) {temp[11]=temp[11]+10-p[2];B_Flag=1;}
      else temp[11]=temp[11]-p[2];
     //以上处理小数点小数点左边第二位数字; 
      if(B_Flag==1)
      {if(temp[10]==0) {temp[10]=9;B_Flag=0;}
       else temp[10]=temp[10]-1;}
     //以上对存在借位情况时对小数点左边第三位进行预处理;
      if(temp[10]<p[1]) {temp[10]=temp[10]+10-p[1];B_Flag=1;}
      else temp[10]=temp[10]-p[1];

上一页  [1] [2] [3]  下一页


本文关键字:单片机  程序  AVR单片机单片机-工控设备 - AVR单片机