⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at88scxx.lst

📁 Atmel公司推出了最新的加密芯片AT88SC0104C~25616C系列
💻 LST
📖 第 1 页 / 共 4 页
字号:
 284          //*****************************************************************
 285          void IIC_Initial(void)
 286          {SDA=1;_Nop();_Nop();_Nop();
 287   1       SCL=1;_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();_Nop();
 288   1       SCL=1;_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();_Nop();
 289   1       SCL=1;_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();_Nop();
 290   1       SCL=1;_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();_Nop();
 291   1       SCL=1;_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();_Nop();//先打出5个CLK
 292   1      }
 293          
 294          //***********************SMSTART***********************************
C51 COMPILER V7.50   AT88SCXX                                                              02/19/2008 15:50:58 PAGE 6   

 295          //功能:启动I2C
 296          //*****************************************************************
 297          void SMSTART(void)
 298          {SCL=1;_Nop();_Nop();_Nop();
 299   1       SDA=1;_Nop();_Nop();_Nop();
 300   1       SDA=0;_Nop();_Nop();_Nop();
 301   1       SCL=0;_Nop();_Nop();_Nop();
 302   1      }
 303          
 304          //************************SMSTOP***********************************
 305          //功能:停止I2C
 306          //*****************************************************************
 307          void SMSTOP(void)
 308          {
 309   1        SCL=1;_Nop();_Nop();_Nop();
 310   1        SDA=0;_Nop();_Nop();_Nop();
 311   1        SDA=1;_Nop();_Nop();_Nop();
 312   1        SCL=0;_Nop();_Nop();_Nop();
 313   1      }
 314          
 315          //************************AckPolling*******************************
 316          //功能:检查所发送的命令是否已被执行
 317          //只有当AckPolling_return=1或bytecounter=0时,该程序才退出
 318          //*****************************************************************
 319          void Wait_For_AckPolling(void)
 320          {
 321   1      unsigned char  AckPolling_return;
 322   1      unsigned char  temp, bitcounter;
 323   1      unsigned char  bytecounter;
 324   1      
 325   1      SMSTART();
 326   1      temp=0xb6;
 327   1      bytecounter=0xff;//如CPU速度过快须加大其值
 328   1      AckPolling_return=0;
 329   1      
 330   1      do{
 331   2        for(bitcounter=0;bitcounter<8;bitcounter++)
 332   2           {
 333   3           if ((temp&0x80)==0x80)
 334   3              {SDA=1;}
 335   3           else
 336   3              {SDA=0;}
 337   3            _Nop();_Nop();_Nop();
 338   3             SCL=1;_Nop();_Nop();
 339   3             SCL=0;_Nop();_Nop();
 340   3             temp=temp<<1;
 341   3           }
 342   2      
 343   2        SDA=1;_Nop();_Nop();
 344   2        SCL=1;_Nop();_Nop();
 345   2        bytecounter--;
 346   2      
 347   2        if(SDA==0)
 348   2          {SCL=0;AckPolling_return=1;}
 349   2        else
 350   2          {SCL=0;AckPolling_return=0;SMSTART();temp=0xb6;}
 351   2      
 352   2        if(AckPolling_return==1)
 353   2          {break;}
 354   2      
 355   2        }while(bytecounter>0);
 356   1      
C51 COMPILER V7.50   AT88SCXX                                                              02/19/2008 15:50:58 PAGE 7   

 357   1      SMSTOP();
 358   1      }
 359          
 360          //*********************GPA_CLOCK************************************
 361          //GPA函数
 362          //入口:Data_in
 363          //出口:GPA[0]:GPA_byte
 364          //参数:times  GPA函数计算的次数
 365          //*****************************************************************
 366          void GPA_CLOCK(unsigned char Datain,unsigned char times)
 367          {
 368   1      unsigned char t;
 369   1      unsigned char d;
 370   1      unsigned char din_gpa;
 371   1      unsigned char Ri;
 372   1      unsigned char R_Sum;
 373   1      unsigned char Si;
 374   1      unsigned char S_Sum;
 375   1      unsigned char Ti;
 376   1      unsigned char T_Sum;
 377   1      
 378   1       for(t=0x00;t<times;t++)
 379   1          {
 380   2           din_gpa=Datain ^ GPA[0];
 381   2           Ri= din_gpa&0x1f;
 382   2           Si= ((din_gpa&0x0f)<<3)+((din_gpa&0xe0)>>5);
 383   2           Ti=(din_gpa&0xf8)>>3;
 384   2      //r元素
 385   2           if(((GPA[4])+((GPA[1]&0x0f)<<1)+((GPA[1]&0x10)>>4))>31)
 386   2             {R_Sum=((GPA[4])+((GPA[1]&0x0f)<<1)+((GPA[1]&0x10)>>4))-31;}
 387   2           else
 388   2             {R_Sum=((GPA[4])+((GPA[1]&0x0f)<<1)+((GPA[1]&0x10)>>4));}
 389   2      
 390   2           GPA[1]=GPA[2];
 391   2           GPA[2]=GPA[3];
 392   2           GPA[3]=GPA[4];
 393   2           GPA[4]=GPA[5]^Ri;
 394   2           GPA[5]=GPA[6];
 395   2           GPA[6]=GPA[7];
 396   2           GPA[7]=R_Sum;
 397   2      //s元素
 398   2           if ((GPA[9]+((GPA[8]&0x3f)<<1)+((GPA[8]&0x40)>>6) )>127)
 399   2              {S_Sum=( (GPA[9]) + ((GPA[8]&0x3f)<<1)+((GPA[8]&0x40)>>6) )-127;}
 400   2           else
 401   2              {S_Sum= (GPA[9]) + ((GPA[8]&0x3f)<<1)+((GPA[8]&0x40)>>6) ;}
 402   2      
 403   2           GPA[8]=GPA[9];
 404   2           GPA[9]=Si^GPA[10];
 405   2           GPA[10]=GPA[11];
 406   2           GPA[11]=GPA[12];
 407   2           GPA[12]=GPA[13];
 408   2           GPA[13]=GPA[14];
 409   2           GPA[14]=S_Sum;
 410   2      //t元素
 411   2           if ((GPA[15]+GPA[17])> 31)
 412   2              {T_Sum=GPA[15]+GPA[17]-31;}
 413   2           else
 414   2              {T_Sum=GPA[15]+GPA[17];}
 415   2      
 416   2           GPA[15]=GPA[16];
 417   2           GPA[16]=GPA[17];
 418   2           GPA[17]=GPA[18]^Ti;
C51 COMPILER V7.50   AT88SCXX                                                              02/19/2008 15:50:58 PAGE 8   

 419   2           GPA[18]=GPA[19];
 420   2           GPA[19]=T_Sum;
 421   2      ///Output
 422   2           if((GPA[14]&0x01)==0)
 423   2             {d=((GPA[7]^GPA[3])&0x01);}
 424   2           else
 425   2             {d=((GPA[19]^GPA[16])&0x01);}
 426   2      
 427   2           if((GPA[14]&0x02)==0)
 428   2             {d=d+((GPA[7]^GPA[3])&0x02);}
 429   2           else
 430   2             {d=d+((GPA[19]^GPA[16])&0x02);}
 431   2      
 432   2           if((GPA[14]&0x04)==0)
 433   2             {d=d+((GPA[7]^GPA[3])&0x04);}
 434   2           else
 435   2             {d=d+((GPA[19]^GPA[16])&0x04);}
 436   2      
 437   2           if((GPA[14]&0x08)==0)
 438   2             {d=d+((GPA[7]^GPA[3])&0x08);}
 439   2           else
 440   2             {d=d+((GPA[19]^GPA[16])&0x08);}
 441   2      
 442   2          GPA[0]= ( (((GPA[0])&0x0f)<<4) +d); //GPA出口
 443   2          }
 444   1      }
 445          //***********************read*************************
 446          //功能:对at88scxx读操作函数
 447          //本程序中以read(rwdata)调用;
 448          //入口:rwdata数组
 449          //出口:rwdata数组
 450          //rd数组长度可改
 451          //cmd_send_counter发送命令次数,超出8次,本函数退出,并认为I2C线路上无器件
 452          //*****************************************************************
 453          void read(rd)//
 454          unsigned char rd[0x20];
 455          {
*** WARNING C235 IN LINE 455 OF D:\AT88SCXX\出售的~1\SC-RWP~2\C程序~1\AT88SCXX.C: parameter 1: different types
 456   1      unsigned char bitcounter;
 457   1      unsigned char temp;
 458   1      unsigned char cmd_send_counter;
 459   1      unsigned char bytecounter;
 460   1      temp=rd[0];
 461   1      bytecounter=0;
 462   1      cmd_send_counter=0;
 463   1      SMSTART();
 464   1      
 465   1      do{
 466   2        for(bitcounter=0;bitcounter<8;bitcounter++)
 467   2           {
 468   3           if ((temp&0x80)==0x80)
 469   3              {SDA=1;}
 470   3           else
 471   3              {SDA=0;}
 472   3                  _Nop();_Nop();
 473   3            SCL=1;_Nop();_Nop();
 474   3            SCL=0;temp=temp<<1;
 475   3           }
 476   2      
 477   2        SDA=1;_Nop();_Nop();_Nop();
 478   2        SCL=1;_Nop();_Nop();_Nop();
 479   2      
C51 COMPILER V7.50   AT88SCXX                                                              02/19/2008 15:50:58 PAGE 9   

 480   2        if(SDA==0)
 481   2          {SCL=0;bytecounter++;temp=rd[bytecounter];}
 482   2        else
 483   2          {SCL=0;SMSTART();temp=rd[0];bytecounter=0;cmd_send_counter++;}
 484   2      
 485   2        if(cmd_send_counter>8)
 486   2          {bytecounter=4;}
 487   2      
 488   2        }while(bytecounter<4);
 489   1      
 490   1      //rev byte
 491   1      SDA=1;
 492   1        for(bytecounter=0;bytecounter<rd[3];bytecounter++)
 493   1           {
 494   2           for(bitcounter=0;bitcounter<8;bitcounter++)
 495   2              {
 496   3                  SCL=1;_Nop();_Nop();
 497   3      
 498   3                  if (SDA==1)
 499   3                     {temp=temp|0x01;}
 500   3                  else
 501   3                     {temp=(temp&0xfe);}
 502   3      
 503   3                  if(bitcounter<7)
 504   3                    {temp=temp<<1;}
 505   3                  else
 506   3                    {_Nop();}
 507   3      
 508   3                  SCL=0;_Nop();_Nop();
 509   3              }
 510   2           SDA=0;_Nop();_Nop();
 511   2           SCL=1;_Nop();_Nop();
 512   2           SCL=0;_Nop();_Nop();
 513   2           SDA=1;_Nop();
 514   2           rd[bytecounter+4]=temp;
 515   2           }
 516   1      
 517   1      SMSTOP();
 518   1      }
 519          //************************write***************************************
 520          //功能:对at88scxx写操作函数
 521          //本程序中以write(rwdata)调用;
 522          //入口:rwdata数组
 523          //出口:rwdata数组
 524          //rd数组长度可改
 525          //cmd_send_counter发送命令次数,超出8次,本函数退出,并认为I2C线路上无器件
 526          //*****************************************************************
 527          void write(SDATA)
 528          unsigned char SDATA[0X14];
 529          {
*** WARNING C235 IN LINE 529 OF D:\AT88SCXX\出售的~1\SC-RWP~2\C程序~1\AT88SCXX.C: parameter 1: different types
 530   1      unsigned char bitcounter;
 531   1      unsigned char temp;
 532   1      unsigned char bytecounter;
 533   1      unsigned char cmd_send_counter;
 534   1      temp=SDATA[0];
 535   1      bytecounter=0;
 536   1      cmd_send_counter=0;
 537   1      SMSTART();
 538   1      
 539   1      do{
 540   2        for(bitcounter=0;bitcounter<8;bitcounter++)
C51 COMPILER V7.50   AT88SCXX                                                              02/19/2008 15:50:58 PAGE 10  

 541   2           {
 542   3           if ((temp&0x80)==0x80)
 543   3              {SDA=1;}
 544   3           else
 545   3              {SDA=0;}
 546   3      
 547   3             _Nop();_Nop();_Nop();
 548   3              SCL=1;_Nop();_Nop();
 549   3              SCL=0;_Nop();_Nop();
 550   3              temp=temp<<1;
 551   3          }
 552   2      
 553   2        SDA=1;_Nop();_Nop();
 554   2        SCL=1;_Nop();_Nop();
 555   2      
 556   2        if(SDA==0)
 557   2          {SCL=0;bytecounter++;temp=SDATA[bytecounter];}
 558   2        else
 559   2          {SCL=0;SMSTART();temp=SDATA[0];bytecounter=0;cmd_send_counter++;}
 560   2      
 561   2        if(cmd_send_counter>8)
 562   2          {bytecounter=SDATA[3]+4;}
 563   2      
 564   2        }while(bytecounter<SDATA[3]+4);
 565   1      
 566   1      SMSTOP();
 567   1      }
 568          //*****************************AUTHENTICATION*****************************
 569          //双向认证函数
 570          //入口:GC_select:选择密钥套数
 571          //出口:AAC:      认证结果
 572          //过程:
 573          //1.从器件读出CIx,产生随机数Q0,计算密钥;计算出Q1,将Q0、Q1发送给器件,计算出新的CIx,SKx
 574          //2.用读回的CIx与计算出的CIx比较
 575          //3.用计算出的SKx替代GCx,重复1过程
 576          //AAC:认证错误计数器.AAC!=0xff表示双向认证未通过或无器件
 577          //需产生随机数则应在程序里修改
 578          //************************************************************************
 579          unsigned char AUTHENTICATION(unsigned char GC_select)
 580          {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -