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

📄 modbus.lst

📁 8051F_Modbus.rar是在C8051F020单片机上编写的CRC16校验的MODBUS协议.
💻 LST
📖 第 1 页 / 共 4 页
字号:
 568   3      
 569   3                      RX1_BufClear();
 570   3                      RX1_Ready = 0;
 571   3              }
 572   2       ///////////////////////////////////////////////////////////////////////////////////////////////////      
             -    
 573   2                          
 574   2                              
 575   2       //将打包的数据发往总控中心
 576   2      
 577   2             if((addend==1 || addrnd==1))
 578   2            {
 579   3                     
 580   3                              while (!TX0_Ready) ;              // wait for transmitter to be available
 581   3                  TX0_Ready = 0;                    // claim transmitter
 582   3                  TX0_ptr = ardata;                // set TX buffer pointer to point to 
 583   3                          SENDNUM0=packge;
 584   3                          TI0 = 1;
 585   3      
 586   3                  packge=0; 
 587   3                  
 588   3                              //设置相应标志位
 589   3                  //下一次打包开始
 590   3                      addend=0;
 591   3                              addrnd=0;
 592   3      
 593   3                 } 
 594   2                              
 595   2        
 596   2        
 597   2      }
 598   1      
 599   1      
 600   1      }
 601          
 602          
 603          
 604          
 605          
 606          //-----------------------------------------------------------------------------
 607          // Initialization Subroutines
 608          //-----------------------------------------------------------------------------
 609          
 610          //-----------------------------------------------------------------------------
C51 COMPILER V7.50   MODBUS                                                                02/26/2006 22:47:28 PAGE 11  

 611          // SYSCLK_Init
 612          //-----------------------------------------------------------------------------
 613          //
 614          // This routine initializes the system clock to use an 22.1184MHz crystal
 615          // as its clock source.
 616          //
 617          void SYSCLK_Init (void)
 618          {
 619   1         int i;                              // delay counter
 620   1      
 621   1         OSCXCN = 0x67;                      // start external oscillator with
 622   1                                             // 22.1184MHz crystal
 623   1      
 624   1         for (i=0; i < 256; i++) ;           // wait for crystal osc. to start up
 625   1      
 626   1         while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
 627   1      
 628   1         OSCICN = 0x88;                      // select external oscillator as SYSCLK
 629   1                                             // source and enable missing clock
 630   1                                             // detector
 631   1      }
 632          
 633          //-----------------------------------------------------------------------------
 634          // PORT_Init
 635          //-----------------------------------------------------------------------------
 636          //
 637          // Configure the Crossbar and GPIO ports
 638          //
 639          void PORT_Init (void)
 640          {
 641   1         
 642   1         XBR0    = 0x05;                     // Enable UART0
 643   1         XBR1    = 0x00;
 644   1         XBR2    = 0x44;                     // Enable crossbar and weak pull-ups
 645   1         P0MDOUT |= 0x11;                    // enable TX0 as a push-pull output
 646   1         P2MDOUT |= 0x03;                    // enable P2 as push-pull output
 647   1         
 648   1         P74OUT  |=0Xf4;
 649   1      }
 650          
 651          //-----------------------------------------------------------------------------
 652          // UART1_Init
 653          //-----------------------------------------------------------------------------
 654          //
 655          // Configure UART1 using Timer1, for <baudrate> and 8-N-1.
 656          //
 657          void modbusUART1_Init (void)
 658          {
 659   1         SCON1   |= 0xd0;                     // SCON1: mode 1, 8-bit UART, enable RX
 660   1         T4CON   |=0X30;                       // Stop Timer; clear int flags; enable
 661   1         RCAP4H=0XFF;
 662   1         RCAP4L=0XB8;
 663   1         T4CON   |=0X04;                      //
 664   1      
 665   1         PCON  |= 0x10;                      // SMOD1 = 1
 666   1         EIE2  |= 0x40;                      // enable UART1 interrupts
 667   1      
 668   1         TX1_Ready = 1;                       // indicate TX ready for transmit
 669   1         RX1_Ready = 0;                       // indicate RX string not ready
 670   1         TX1_ptr = NULL;
 671   1        
 672   1      }
C51 COMPILER V7.50   MODBUS                                                                02/26/2006 22:47:28 PAGE 12  

 673          
 674          
 675          //
 676          void UART1_ISR (void) interrupt 20 using 3
 677          {
 678   1         
 679   1         static unsigned char TX1_index = 0;    // send buffer index
 680   1         unsigned char the_char;
 681   1         
 682   1         if ((SCON1 & 0x01) == 0x01)
 683   1         {                                    // handle receive function
 684   2            SCON1 &= ~0x01;                   // RI1 = 0; clear RX complete 
 685   2            if (RX1_Ready == 0)                //可以接收
 686   2                {   the_char = SBUF1; 
 687   3                     
 688   3                        if(RX1_index == 0)
 689   3                        {
 690   4                            if(the_char==address)
 691   4                                {
 692   5                                    RX1_Buf[RX1_index] = the_char;
 693   5                                RX1_index++;
 694   5                                        RECEIVENUM1=1;
 695   5                                }
 696   4                                else
 697   4                                {
 698   5                                     RX1_index=0;
 699   5                                }
 700   4                        }
 701   3                        else if(RX1_index == 1)
 702   3                        {   if(the_char==0x03)
 703   4                                {
 704   5                                     RX1_Buf[RX1_index] = the_char;
 705   5                                 RX1_index++;
 706   5                                         RECEIVENUM1=2;
 707   5                                }
 708   4                                else
 709   4                                {
 710   5                                     RX1_index=0;
 711   5                                }
 712   4                        }
 713   3                        else if(RX1_index == 2)
 714   3                        {      
 715   4                               RX1_Buf[RX1_index] = the_char;
 716   4                               RX1_index++;
 717   4                                       RECEIVENUM1=3;
 718   4                        }
 719   3                        else if(RX1_index > 2)
 720   3                        {
 721   4                               if(RX1_index==4+ RX1_Buf[2])
 722   4                                       {
 723   5                                          RX1_Buf[RX1_index] = the_char;
 724   5                                              RECEIVENUM1++;
 725   5                                          RX1_index=0;
 726   5                                              RX1_Ready = 1;  
 727   5                                       }
 728   4                                       else
 729   4                                       {
 730   5                                         RX1_Buf[RX1_index] = the_char;
 731   5                                 RX1_index++;
 732   5                                         RECEIVENUM1++; 
 733   5                                       }
 734   4                        }
C51 COMPILER V7.50   MODBUS                                                                02/26/2006 22:47:28 PAGE 13  

 735   3                       
 736   3                       }
 737   2      
 738   2           
 739   2         } 
 740   1         else if ((SCON1 & 0x02) == 0x02) 
 741   1         {
 742   2            SCON1 &= ~0x02;                     // TI1 = 0; clear TX complete 
 743   2                                                // indicator
 744   2            the_char = *TX1_ptr;                 // read next character in string
 745   2             if(TX1_index<SENDNUM1)
 746   2                 {
 747   3                     TX1_ptr++;
 748   3                         TX1_index++;                     // get ready for next character
 749   3                         if(even[the_char]==0)
 750   3                           SCON1 &= ~0x08;
 751   3                         else
 752   3                   SCON1|= 0x08;    
 753   3                       SBUF1 = the_char;               // transmit it
 754   3                    
 755   3                 }          
 756   2                 else
 757   2                 {    
 758   3                      TX1_Ready = 1;                 // indicate ready for next TX
 759   3                  TX1_index=0;
 760   3                              RE485=0;
 761   3                 }
 762   2         }
 763   1      }
 764          
 765          
 766          
 767             
 768          
 769          //定时器0初始化
 770          
 771          void Timer0_Init (void)
 772          {
 773   1         TMOD |=0X01;
 774   1         TH0=0XB8;
 775   1         TL0=0X00;
 776   1         ET0=1;
 777   1        
 778   1      }
 779          
 780          
 781          //---------------------------------------------------------------------------------------
 782          //定时器T0溢出中断处理子程序
 783          void Timer0_ISR(void) interrupt 1       
 784          {
 785   1                tend=temp;
 786   1                 if(time>=tend)     
 787   1                {
 788   2                    TL0=0X00;        //11.0592MHZ T0低8位初值:定时10ms
 789   2                        TH0=0XB8;        //T0高8位初值
 790   2                    TR0=0;           //终止定时器T0
 791   2                    sendinterval=1;
 792   2                    time=0;
 793   2                 }
 794   1                 else
 795   1      
 796   1                 {
C51 COMPILER V7.50   MODBUS                                                                02/26/2006 22:47:28 PAGE 14  

 797   2                 
 798   2                    TL0=0X00;        //T0低8位初值:定时50ms
 799   2                        TH0=0XB8;        //T0高8位初值
 800   2                        time=time+1;
 801   2                        sendinterval=0;
 802   2                  }
 803   1      }
 804          //---------------------------------------------------------------------------------------
 805          
 806          
 807          
 808          //-----------------------------------------------------------------------------
 809          // UART0_Init
 810          //-----------------------------------------------------------------------------
 811          //
 812          // Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
 813          //
 814          void UART0_Init (void)
 815          {
 816   1         SCON0   = 0x50;                     // SCON0: mode 1, 8-bit UART, enable RX
 817   1         TMOD   |= 0x20;                     // TMOD: timer 1, mode 2, 8-bit reload
 818   1         TH1    = 0x6F;                      // set Timer1 reload value for baudrate
 819   1         CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
 820   1         PCON  |= 0x90;                      // SMOD0 = 1
 821   1         ES0    = 1;                         // enable UART0 interrupts
 822   1         TI0    = 0;                         // Indicate TX0 ready
 823   1         TR1    = 1;                         // start Timer1
 824   1      
 825   1         TX0_Ready = 1;                       // indicate TX ready for transmit
 826   1         RX0_Ready = 0;                       // indicate RX string not ready
 827   1         TX0_ptr = NULL;
 828   1      }
 829          
 830          
 831          
 832          void UART0_ISR (void) interrupt 4 
 833          {
 834   1         
 835   1         static unsigned int TX0_index = 0;    // send buffer index
 836   1         unsigned char the_char0;
 837   1         
 838   1         if (RI0 == 1) 
 839   1         {                    
 840   2            RI0 = 0; 
 841   2                if (RX0_Ready == 0)                        //可以接收
 842   2                {   the_char0 = SBUF0;                      // check to see if message pending
 843   3                if(RX0_index >=RX0_LENGTH)
 844   3                        {
 845   4                           if (the_char0 == RX0_END)             //是结束符
 846   4                               {
 847   5                                      RX0_Buf[RX0_index] = the_char0;   // NULL-terminate message
 848   5                          RX0_Ready = 1;              // post message ready
 849   5                          RX0_index = 0;              // reset RX message index
 850   5                               }
 851   4                               else                              //不是结束符
 852   4                               {      
 853   5                                      RX0_Ready =0;              // post message ready
 854   5                          RX0_index = 0;              // reset RX message index
 855   5                                      
 856   5                               }

⌨️ 快捷键说明

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