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

📄 232tocan.lst

📁 RS232/CAN转换器 硬件电路需自己设计(电路很简单)
💻 LST
📖 第 1 页 / 共 5 页
字号:
C51 COMPILER V8.17   232TOCAN                                                              03/22/2009 15:12:45 PAGE 6   

 303   2                      break;
 304   2                      
 305   2                      case 5://38400 bps
 306   2                              RCAP2H = BAUD38400H;
 307   2                              RCAP2L = BAUD38400L;
 308   2                      break;
 309   2                      
 310   2                      case 6://57600 bps
 311   2                              RCAP2H = BAUD57600H;
 312   2                              RCAP2L = BAUD57600L;
 313   2                      break;
 314   2                      
 315   2                      case 7://115200 bps
 316   2                              RCAP2H = BAUD115200H;
 317   2                              RCAP2L = BAUD115200L;
 318   2                      break;
 319   2              }
 320   1      }
 321          //初始化MCU各寄存器参数
 322          void Mcu_init(void)
 323          {
 324   1              TMOD = 0x21;    
 325   1              TH0  = TIMER50ms_TH0;
 326   1              TL0  = TIMER50ms_TL0;   
 327   1          IE   = 0x96;
 328   1              IP   = 0x10;
 329   1              TCON = 0x10;
 330   1              Baudrate(3);     //初始化波特率9600bps 
 331   1      }
 332          /*------------------------延时1ms子程序--------------------------*/
 333          void Delay(uchar t_a)
 334          {
 335   1          uchar t_b;
 336   1          uchar t_c;
 337   1             
 338   1          for (t_c=0; t_c<t_a; t_c++)
 339   1          {
 340   2              for (t_b=0; t_b<240; t_b++)
 341   2                      {
 342   3                              _nop_();
 343   3                              _nop_();
 344   3                              _nop_();
 345   3                      }
 346   2                      Dog();
 347   2          }
 348   1      }
 349          //喂狗
 350          void Dog(void)
 351          {
 352   1              SPI_CS = 1;     
 353   1              SPI_CS = 0;
 354   1              _nop_();
 355   1              _nop_();        
 356   1              SPI_CS = 1;
 357   1      }
 358          //Write one byte 
 359          void W_byte(uchar Data)
 360          {
 361   1              uchar i;
 362   1      
 363   1              SPI_SI = 1;
 364   1              SCLK   = 0;
C51 COMPILER V8.17   232TOCAN                                                              03/22/2009 15:12:45 PAGE 7   

 365   1              _nop_();
 366   1              _nop_();
 367   1              
 368   1              for (i=0; i<8; i++)
 369   1              {       
 370   2                  SPI_SI = (bit)(Data&0x80);
 371   2                      SCLK   = 1;
 372   2                      _nop_();
 373   2                      _nop_();
 374   2                      Data   <<= 1;
 375   2                      SCLK   = 0;
 376   2                      _nop_();
 377   2                      _nop_();
 378   2              }
 379   1      }
 380          uchar R_byte(void)
 381          {
 382   1              uchar i;
 383   1              uchar result;
 384   1      
 385   1              result = 0;
 386   1              SPI_SO = 1;
 387   1              
 388   1              for (i=0; i<8; i++)
 389   1              {
 390   2                      SCLK = 1;
 391   2                      
 392   2                      result <<= 1;
 393   2                      
 394   2                      if (SPI_SO)
 395   2                      {
 396   3                              result |= 0x01;
 397   3                      }
 398   2                      
 399   2                      SCLK = 0;
 400   2                      _nop_();
 401   2                      _nop_();
 402   2              }
 403   1              
 404   1              return result;
 405   1      }
 406          //----------------------------------------------------------------
 407          void Write_status(uchar status)
 408          {
 409   1              SPI_CS = 0;
 410   1              W_byte(status); 
 411   1              SPI_CS = 1;
 412   1      }
 413          //----------------------------------------------------------------
 414          void Write_reg(uchar tcode)     //      write status register
 415          {
 416   1              WP     = 1;
 417   1              Write_status(WREN);
 418   1              
 419   1              SPI_CS = 0;     
 420   1              W_byte(WRSR);
 421   1              W_byte(tcode);
 422   1      
 423   1              SPI_CS = 1;
 424   1              
 425   1              Delay(10);      
 426   1              
C51 COMPILER V8.17   232TOCAN                                                              03/22/2009 15:12:45 PAGE 8   

 427   1              WP     = 0;
 428   1      }
 429          //----------------------------------------------------------------
 430          uchar Read_byte(uint address)   //read one byte from x5045
 431          {
 432   1              uchar result;
 433   1      
 434   1              SPI_CS = 0;
 435   1              
 436   1              if (address>255)
 437   1              {
 438   2                      W_byte(READ|0x08);
 439   2              }
 440   1              else
 441   1              {
 442   2                      W_byte(READ);
 443   2              }
 444   1      
 445   1              W_byte((uchar)(address & 0xff));
 446   1              result = R_byte();
 447   1                      
 448   1              SPI_CS = 1;
 449   1              
 450   1              return (result);
 451   1      }
 452          //往X5045具体单元中写入一个字节的数据----------------------------------------------------------------
 453          void Write_byte(uint address, uchar Data)               //write one byte to x5045
 454          {
 455   1              WP = 1;
 456   1              Write_status(WREN);     
 457   1      
 458   1              SPI_CS = 0;
 459   1              
 460   1              if (address>255)
 461   1              {
 462   2                      W_byte(WRITE|0x08);
 463   2              }
 464   1              else
 465   1              {
 466   2                      W_byte(WRITE);
 467   2              }
 468   1      
 469   1              W_byte((uchar)(address & 0xff));
 470   1              W_byte(Data);
 471   1                      
 472   1              SPI_CS = 1;
 473   1                      
 474   1              Delay(10);
 475   1              
 476   1              WP = 0;
 477   1      }
 478          //------------定时器0中断程序----------
 479          void Server_T0(void) interrupt 1 
 480          {
 481   1              //25ms中断一次
 482   1              TH0 = TIMER25ms_TH0;
 483   1              TL0 = TIMER25ms_TL0;
 484   1              
 485   1              //50ms定时
 486   1              if (++Timer50ms < 2)
 487   1              {
 488   2                  return;
C51 COMPILER V8.17   232TOCAN                                                              03/22/2009 15:12:45 PAGE 9   

 489   2              }
 490   1          
 491   1          Timer50ms = 0;
 492   1          //关闭串口通讯指示灯(通过定时时间可以改变闪烁频率)
 493   1              REC_LED = 1;
 494   1              SEND_LED = 1;
 495   1          
 496   1              if (Com_rec_timer > 0)  //接收一帧数据的最大延时时间
 497   1              {
 498   2                      if (--Com_rec_timer == 0)
 499   2                      {
 500   3                          Can_send_ready = 1;
 501   3                      }
 502   2              }
 503   1              
 504   1              if (Wait_timer > 0)
 505   1              {
 506   2                  Wait_timer--;
 507   2              }
 508   1      }
 509          //CAN通讯中断
 510          void Server_INT1(void) interrupt 2
 511          {
 512   1          if (Com_send_ready)
 513   1          {
 514   2              //释放接收缓冲区
 515   2              PBYTE[1] = RRB_CMD;
 516   2              return;
 517   2          }
 518   1          CANInterrupt = PBYTE[3];
 519   1          
 520   1          //错误警告中断
 521   1          if (_testbit_(EI_Bit))
 522   1          {
 523   2              //重新配置SJA1000
 524   2              Can_err = 1;
 525   2          }
 526   1          //数据超载中断
 527   1          if (_testbit_(DOI_Bit))
 528   1          {
 529   2              //清除数据
 530   2              PBYTE[1] = CDO_CMD;
 531   2              //数据清除不成功
 532   2              if ((PBYTE[2] & 0x02) != 0)
 533   2              {
 534   3                 //重新配置SJA1000
 535   3                 Can_err = 1;
 536   3              }
 537   2          }
 538   1          //接收中断
 539   1          if (_testbit_(RI_Bit))
 540   1          {
 541   2              REC_LED = 0;
 542   2              CanRec_prg();
 543   2          }
 544   1      }
 545          //-------------通讯中断程序------------
 546          void Server_UART(void) interrupt 4 
 547          {
 548   1              uchar t_c;
 549   1              
 550   1              if (_testbit_(RI))
C51 COMPILER V8.17   232TOCAN                                                              03/22/2009 15:12:45 PAGE 10  

 551   1              {
 552   2                      t_c = SBUF;
 553   2                      
 554   2                      //接收缓冲区是否溢出
 555   2                      if (Com_rxcount < MAX_LENGHT && !Can_send_ready)
 556   2                      {
 557   3                          Com_rxbuf[Com_rxcount++] = t_c;
 558   3                          Com_rec_timer = Rec_delay;
 559   3                          //REC_LED = 0;
 560   3                      }
 561   2              }       
 562   1              
 563   1              if (_testbit_(TI))      //发送数据包
 564   1              {
 565   2              //SEND_LED = 0;
 566   2              
 567   2                      if (Com_send_now)
 568   2                      {
 569   3                              Com_txcount--;
 570   3                              
 571   3                              if (Com_txcount == 0)
 572   3                              {
 573   4                                      Com_send_now = 0;
 574   4                              }
 575   3                              else
 576   3                              {
 577   4                                      Telcom++;
 578   4                                      SBUF = *Telcom;
 579   4                              }
 580   3                      }
 581   2              }
 582   1      }
 583          //往EEROM中写入一个short int型数据
 584          void Write_int(uint address, uint Data)
 585          {
 586   1              uchar m;
 587   1              
 588   1              union 
 589   1              {
 590   1                      uint crc;
 591   1                      uchar t[2];
 592   1              }var;
 593   1              
 594   1              var.crc = Data;
 595   1              
 596   1              for (m=0; m<2; m++)
 597   1              {
 598   2                      Write_byte((address+m), var.t[m]);
 599   2              }
 600   1      }
 601          //从EEROM中读出一个short int型数据
 602          uint Read_int(uint address)
 603          {
 604   1              union 
 605   1              {
 606   1                      uint crc;
 607   1                      uchar t[2];
 608   1              }var;
 609   1              
 610   1              var.t[0] = Read_byte(address);
 611   1              var.t[1] = Read_byte(address + 1);
 612   1              

⌨️ 快捷键说明

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