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

📄 24c02srtest.lst

📁 AT24C02页写模式存取源码
💻 LST
📖 第 1 页 / 共 2 页
字号:
 313   1              if(WriteData_ulint(ptr_ulint,0x20,4))
 314   1              {
 315   2                      return 1;
 316   2              }
 317   1              temp = CalCRC_ulint(ptr_ulint,4);
 318   1              if(WriteData_ulint(ptr_ulint,0x24,4))
 319   1              {
 320   2                      return 1;
 321   2              }
 322   1      
 323   1              temp = data_guanfa;                             //关阀量
 324   1              if(WriteData_ulint(ptr_ulint,0x28,4))
 325   1              {
 326   2                      return 1;
 327   2              }
 328   1              temp = CalCRC_ulint(ptr_ulint,4);
 329   1              if(WriteData_ulint(ptr_ulint,0x2c,4))
 330   1              {
 331   2                      return 1;
 332   2              }
 333   1      
 334   1              temp = data_ciganrao;                   //磁扰数
 335   1              if(WriteData_ulint(ptr_ulint,0x30,4))
 336   1              {
 337   2                      return 1;
 338   2              }
 339   1              temp = CalCRC_ulint(ptr_ulint,4);
 340   1              if(WriteData_ulint(ptr_ulint,0x34,4))
 341   1              {
 342   2                      return 1;
 343   2              }
 344   1      
 345   1              temp = data_kaigai;                             //开盖次数
 346   1              if(WriteData_ulint(ptr_ulint,0x38,4))
 347   1              {
 348   2                      return 1;
 349   2              }
 350   1              temp = CalCRC_ulint(ptr_ulint,4);
 351   1              if(WriteData_ulint(ptr_ulint,0x3c,4))
 352   1              {
 353   2                      return 1;
 354   2              }
 355   1      
 356   1              return 0;
 357   1      }
 358          
 359          /********************************************************
 360          * 延时函数                                              *
 361          ********************************************************/
 362          void Delayms(uchar ms) 
C51 COMPILER V8.08   24C02SRTEST                                                           02/28/2008 14:19:16 PAGE 7   

 363          {
 364   1              uchar k;
 365   1              while(ms--)
 366   1              {
 367   2                      for(k = 0; k < 120; k++);
 368   2              }
 369   1      }
 370          
 371          
 372          /********************************************************
 373          *  短延时                                                                                   *
 374          ********************************************************/
 375          void Delaynop(void)
 376          {
 377   1              _nop_();
 378   1              _nop_();
 379   1              _nop_();
 380   1              _nop_();
 381   1              _nop_();
 382   1      }
 383          
 384          /********************************************************
 385          *  开始                                                                                     *
 386          ********************************************************/
 387          void Start(void)
 388          {
 389   1              SCL = 0;
 390   1              Delaynop();
 391   1              SDA = 1;
 392   1              Delaynop();
 393   1              SCL = 1;
 394   1              Delaynop();
 395   1              SDA = 0;
 396   1              Delaynop();
 397   1              SCL = 0;
 398   1              Delaynop();
 399   1      }
 400          
 401          /********************************************************
 402          *  停止                                                                                     *
 403          ********************************************************/
 404          void Stop(void) 
 405          {
 406   1              SCL = 0;
 407   1              Delaynop();
 408   1              SDA = 0;
 409   1              Delaynop();
 410   1              SCL = 1;
 411   1              Delaynop();
 412   1              SDA = 1;
 413   1              Delaynop();
 414   1      }
 415          
 416          /********************************************************
 417          *  接收器应答                                                                  ==     *
 418          ********************************************************/
 419          void Ack(void)       //读数据时接收器应答
 420          {
 421   1              SDA = 0;   //8bit后生成应答信号
 422   1              SCL = 1;
 423   1              Delaynop();
 424   1              SCL = 0;
C51 COMPILER V8.08   24C02SRTEST                                                           02/28/2008 14:19:16 PAGE 8   

 425   1              SDA = 1;   //释放SDA
 426   1      }
 427          
 428          /********************************************************
 429          *  接收器不应答                                                                 ==    *
 430          ********************************************************/
 431          void NoAck(void)     //读数据最后接收器不进行确认应答
 432          {
 433   1              SDA = 1;
 434   1              SCL = 1;
 435   1              Delaynop();
 436   1              SCL = 0;
 437   1      }
 438          
 439          
 440          /********************************************************
 441          *  写8Bit                                                                                   *
 442          ********************************************************/
 443          bit Write8Bit(unsigned char input) 
 444          {
 445   1              unsigned char temp;
 446   1              bit ErrorBit = 1;
 447   1              uchar i = 255;            //因故障超时255
 448   1      
 449   1              for(temp = 8;temp != 0;temp--) 
 450   1          {
 451   2                      SDA = (bit)(input&0x80);
 452   2                      Delaynop();
 453   2                      SCL = 1;
 454   2                      Delaynop();
 455   2                      SCL = 0;
 456   2                      Delaynop();
 457   2                      input = input<<1;
 458   2              }
 459   1      
 460   1              SDA = 1;
 461   1              Delaynop();
 462   1              SCL = 1;
 463   1              while(i > 0 & ErrorBit)  //等待从器件应答,超时返回错误
 464   1              {
 465   2                      ErrorBit = SDA;
 466   2                      i--;
 467   2              }
 468   1              SCL = 0;
 469   1              Delaynop();
 470   1              return(ErrorBit);
 471   1      
 472   1      }
 473          
 474          /********************************************************
 475          *  写数据ulint                                                                      *
 476          ********************************************************/
 477          bit WriteData_ulint(uchar *Wdata,unsigned char RomAddress,unsigned char number) 
 478          {
 479   1              Start();
 480   1              if(Write8Bit(OP_Write))       //判断写操作是否写成功
 481   1          {
 482   2                      Stop();
 483   2                      return 1;
 484   2              }
 485   1              if(Write8Bit(RomAddress))
 486   1          {
C51 COMPILER V8.08   24C02SRTEST                                                           02/28/2008 14:19:16 PAGE 9   

 487   2                      Stop();
 488   2                      return 1;
 489   2              }
 490   1              for(;number != 0;number--) 
 491   1          {
 492   2                      if(Write8Bit(*Wdata))
 493   2                  {
 494   3                              Stop();
 495   3                              return 1;
 496   3                      }
 497   2                      Wdata++;
 498   2              }
 499   1              Stop();
 500   1              Delayms(10);    //24Cxx系列写周期限制要求10ms
 501   1              return 0;               //返回值为0表示本次操作成功,1操作失败
 502   1      }
 503          
 504          
 505          /********************************************************
 506          *  读8Bit                                                                                   *
 507          ********************************************************/
 508          unsigned char Read8Bit(void) 
 509          {
 510   1              unsigned char temp,rbyte=0;
 511   1      
 512   1              for(temp = 8;temp != 0;temp--) 
 513   1          {
 514   2                      SCL = 1;
 515   2                      rbyte = rbyte << 1;
 516   2                      rbyte = rbyte|((unsigned char)(SDA));
 517   2                      SCL = 0;
 518   2                      Delaynop();
 519   2              }
 520   1              return(rbyte);
 521   1      }
 522          
 523          /********************************************************
 524          *  读数据ulint                                                                      *
 525          ********************************************************/
 526          bit ReadData_ulint(uchar *RamAddress,unsigned char RomAddress,unsigned char bytes) 
 527          {
 528   1              Start();
 529   1              if(Write8Bit(OP_Write))                 //判断写操作是否写成功
 530   1          {
 531   2                      Stop();
 532   2                      return 1;
 533   2              }
 534   1              if(Write8Bit(RomAddress))
 535   1          {
 536   2                      Stop();
 537   2                      return 1;
 538   2              }
 539   1              Start();
 540   1              if(Write8Bit(OP_Read))
 541   1          {
 542   2                      Stop();
 543   2                      return 1;
 544   2              }
 545   1              while(bytes != 1) 
 546   1          {
 547   2                      *RamAddress = Read8Bit();
 548   2                      Ack();                  //应答
C51 COMPILER V8.08   24C02SRTEST                                                           02/28/2008 14:19:16 PAGE 10  

 549   2                      RamAddress++;
 550   2                      bytes--;
 551   2              }
 552   1              *RamAddress = Read8Bit();
 553   1              NoAck();                        //不应答
 554   1              Stop();
 555   1              return 0;
 556   1      }
 557          
 558          
 559          
 560          /**********************************************************
 561            数据转换函数
 562          **********************************************************/
 563          void datasw()
 564          {
 565   1         display[7]=temp1/10000000;
 566   1         temp1=temp1%10000000;
 567   1         display[6]=temp1/1000000;
 568   1         temp1=temp1%1000000;
 569   1         display[5]=temp1/100000;
 570   1         temp1=temp1%100000;
 571   1         display[4]=temp1/10000;
 572   1         temp1=temp1%10000;
 573   1         display[3]=temp1/1000;
 574   1         temp1=temp1%1000;
 575   1         display[2]=temp1/100;
 576   1         temp1=temp1%100;
 577   1         display[1]=temp1/10;
 578   1         display[0]=temp1%10;
 579   1      }
 580          
 581          
 582          
 583          
 584          /**********************************************************
 585            数据显示函数
 586          **********************************************************/
 587          void  ledplay()
 588          {
 589   1      
 590   1         P0 = ledcode[display[0]];     //显示 
 591   1         P2 = 0x7f;
 592   1         Delayms(2);
 593   1         P0 = ledcode[display[1]];    //显示   
 594   1         P2 = 0xbf;
 595   1         Delayms(2);
 596   1         P0 = ledcode[display[2]] ;   //显示小数位
 597   1         P2 = 0xdf;
 598   1         Delayms(2);
 599   1         P0 = ledcode[display[3]];     //显示 
 600   1         P2 = 0xef;
 601   1         Delayms(2);
 602   1         P0 = ledcode[display[4]];     //显示 
 603   1         P2 = 0xf7;
 604   1         Delayms(2);
 605   1         P0 = ledcode[display[5]];     //显示 
 606   1         P2 = 0xfb;
 607   1         Delayms(2);
 608   1         P0 = ledcode[display[6]];     //显示 
 609   1         P2 = 0xfd;
 610   1         Delayms(2);
C51 COMPILER V8.08   24C02SRTEST                                                           02/28/2008 14:19:16 PAGE 11  

 611   1         P0 = ledcode[display[7]];     //显示 
 612   1         P2 = 0xfe;
 613   1         Delayms(2);
 614   1      }
 615          
 616          
 617          /**********************************************************
 618            CRC   ulint
 619          **********************************************************/
 620          ulint CalCRC_ulint(uchar *ptr, unsigned char len)
 621          {                                                                                
 622   1              uint crc;
 623   1              ulint data_crc;
 624   1              uchar da;
 625   1              uint data crc_ta[16]={  0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
 626   1                                                              0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef };
 627   1      
 628   1              crc = 0;
 629   1              while(len--> 0) 
 630   1              {
 631   2                      da=((uchar)(crc/256))/16;       // 暂存CRC 的高四位
 632   2                      crc<<=4;        //CRC 右移4 位,相当于取CRC 的低12 位)
 633   2                      crc^=crc_ta[da^(*ptr/16)];      // CRC 的高4 位和本字节的前半字节相加后查表计算CRC,然后加上上一次CRC 的余数
 634   2                      da=((uchar)(crc/256))/16;       // 暂存CRC 的高4 位
 635   2                      crc<<=4;        //CRC 右移4 位, 相当于CRC 的低12 位)
 636   2                      crc^=crc_ta[da^(*ptr&0x0f)];    // CRC 的高4 位和本字节的后半字节相加后查表计算CRC,然后再加上上一次CRC 的

⌨️ 快捷键说明

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