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

📄 usbuart0.lst

📁 基于MODBUS协议的串口通信下位机软件的设计
💻 LST
字号:
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE USBUART0
OBJECT MODULE PLACED IN USBUART0.OBJ
COMPILER INVOKED BY: E:\软件\Keil\C51\BIN\C51.EXE USBUART0.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          
   2           
   3          
   4          
   5          
   6          //使用F330,串口为P0.4,P0.5;操作方法:插上短路环J11,拔掉J12,连上USB线。
   7          //-----------------------------------------------------------------------------
   8          // UART.c
   9          //-----------------------------------------------------------------------------
  10          
  11          
  12          //-----------------------------------------------------------------------------
  13          // 所包含的头文件
  14          //------------------------------------------------------------------------
  15          
  16          #include <c8051f330.h>                 
  17          #include <stdio.h>
  18          #include <lcddriver.h>
  19          #define ADDR 0X01
  20          //-----------------------------------------------------------------------------
  21          // 常量定义
  22          //-----------------------------------------------------------------------------
  23          
  24          #define SYSCLK          24500000             // 系统工作频率
  25          #define BAUDRATE        9600               // 串口工作波特率
  26          #define TIMER2_RATE     1000                 // 定时器2溢出标志位频率
  27          #define uchar unsigned char
  28          #define uint unsigned int
  29           
  30          //-----------------------------------------------------------------------------
  31          // F33x’定时器2的16位SFR
  32          //-----------------------------------------------------------------------------
  33          
  34          sfr16 TMR2RL   = 0xca;                 
  35          sfr16 TMR2     = 0xcc;                 
  36          
  37          //-----------------------------------------------------------------------------
  38          
  39          // 功能函数
  40          //-----------------------------------------------------------------------------
  41          
  42          void SYSCLK_Init (void);
  43          void PORT_Init (void);
  44          void UART0_Init (void);
  45          void Timer2_Init (int);
  46          bit ReceiveBit;
  47          bit recieve_flag=0;
  48          unsigned char temp;
  49          unsigned char xdata  table[18] ={"welcome 231"}; 
  50          uchar i,point,aba;
  51          uint ctr1,crc;
  52          uchar data sbufda,tx_count,sent,kcc1,kcc2,aaa,bbb;
  53          uchar data rbuf[8];
  54          uchar data tbuf[18];
  55          /*void delay(unsigned int m)
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 2   

  56          {
  57          unsigned int n;
  58          n=0;
  59          while(n<m)
  60          {n++;}
  61          return;
  62          }*/
  63          uint crc16(uchar *str,uint num);
  64          //-----------------------------------------------------------------------------
  65          // 主程序
  66          //-----------------------------------------------------------------------------
  67          
  68           void main (void) {
  69   1      
  70   1         char i=0;
  71   1         PCA0MD &= ~0x40;                    // 禁止看门狗
  72   1                                             
  73   1         PORT_Init();                        // 初始化端口I/O
  74   1         
  75   1         SYSCLK_Init ();                     //  初始化系统时钟
  76   1         
  77   1         Timer2_Init(SYSCLK/TIMER2_RATE);    //  初始化 Timer 2 
  78   1         
  79   1         UART0_Init();                       //  串口初始化
  80   1         Initialize_LCD();
  81   1        EA=1;
  82   1        ES0=1;
  83   1        while(1)      
  84   1        {
  85   2      
  86   2        if(recieve_flag)
  87   2        {
  88   3          WriteCommand(0x80,1);   //写地址
  89   3         for( i=0;i<16 ; i++)
  90   3         {
  91   4                
  92   4               
  93   4                          WriteData(tbuf[i]);
  94   4      
  95   4      
  96   4         }
  97   3               WriteCommand(0xc0,1);   //写地址
  98   3               WriteData(tbuf[16]);
  99   3               WriteData(tbuf[17]);
 100   3              recieve_flag=0;
 101   3         }      
 102   2        }                     
 103   1        
 104   1        /*
 105   1        while(ReceiveBit==1)
 106   1        {
 107   1        ES0=0;
 108   1        EA=0;
 109   1        ReceiveBit=0;
 110   1        //delay(60000);
 111   1       //delay(60000);
 112   1       //delay(60000);
 113   1       SBUF0=temp;
 114   1       while(TI0==0);
 115   1       TI0=0;
 116   1      //delay(60000);
 117   1      // delay(60000);
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 3   

 118   1       //delay(60000);
 119   1       ES0=1;
 120   1       EA=1;
 121   1        }
 122   1        }
 123   1        //printf (" KEN :) Happy new year!  \n ");                //  输出字符                         */
 124   1       
 125   1      }
 126          
 127            
 128          
 129          //-----------------------------------------------------------------------------
 130          // 端口初始化
 131          //-----------------------------------------------------------------------------
 132          //
 133          // Configure the Crossbar and GPIO ports.
 134          //
 135          // P0.4 - UART TX
 136          // P0.5 - UART RX
 137          // P3.3 - LED
 138          
 139          void PORT_Init (void)
 140          {
 141   1         //P0SKIP  |= 0x01;                    
 142   1        // P0MDIN  |= 0x01;                    // 配置P0.0为模拟出入
 143   1         P0MDOUT |= 0x10;                    // 使能 UTX 为推挽输出
 144   1       //  P1MDOUT |= 0x08;                    // 使能 LED 为推挽输出
 145   1         XBR0    = 0x01;                     // 使能 UART on P0.4(TX) and P0.5
 146   1                           
 147   1         XBR1    = 0x40;                  
 148   1      }
 149          //-----------------------------------------------------------------------------
 150          // 系统时钟初始化
 151          //-----------------------------------------------------------------------------
 152          
 153          
 154          void SYSCLK_Init (void)
 155          {
 156   1         OSCICN |= 0x03;                     // 配置内部振荡器的的最大频率
 157   1                                              
 158   1         RSTSRC  = 0x04;                     // 使能始终丢失检测寄存器
 159   1      
 160   1      }
 161          
 162          //-----------------------------------------------------------------------------
 163          // 串口初始化
 164          //-----------------------------------------------------------------------------
 165          
 166          
 167          void UART0_Init (void)
 168          {
 169   1         SCON0 = 0x10;                       
 170   1         if (SYSCLK/BAUDRATE/2/256 < 1) {
 171   2      
 172   2            TH1 = -(SYSCLK/BAUDRATE/2);
 173   2            CKCON &= ~0x0B;                  
 174   2            CKCON |=  0x08;
 175   2         } else if (SYSCLK/BAUDRATE/2/256 < 4) {
 176   2            TH1 = -(SYSCLK/BAUDRATE/2/4);
 177   2            CKCON &= ~0x0B;                                    
 178   2            CKCON |=  0x09;
 179   2         } else if (SYSCLK/BAUDRATE/2/256 < 12) {
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 4   

 180   2            TH1 = -(SYSCLK/BAUDRATE/2/12);
 181   2            CKCON &= ~0x0B;                  
 182   2         } else {
 183   2            TH1 = -(SYSCLK/BAUDRATE/2/48);
 184   2            CKCON &= ~0x0B;                   
 185   2            CKCON |=  0x02;
 186   2         }
 187   1      
 188   1         TL1 = TH1;                          
 189   1         TMOD &= ~0xf0;                      
 190   1         TMOD |=  0x20;                       
 191   1         TR1 = 1;                            
 192   1        // TI0 = 1;                            
 193   1      }
 194          //-----------------------------------------------------------------------------
 195          // 时钟2中断
 196          //-----------------------------------------------------------------------------
 197          
 198          void Timer2_Init (int counts)
 199          {
 200   1         TMR2CN = 0x00;                      // 停止时钟2;清除 TF2H 和TF2L;
 201   1                                             
 202   1         CKCON |= 0x10;                      
 203   1         TMR2RL  = -counts;                  
 204   1         TMR2    = TMR2RL;                   
 205   1         ET2 = 0;                            
 206   1         TR2 = 1;                            
 207   1      }
 208          void UATR0_ISR(void)  interrupt 4
 209          {
 210   1      if(TI0)
 211   1      {
 212   2        
 213   2      TI0=0;
 214   2       sbufda++;
 215   2       if(sbufda<=tx_count)
 216   2       {
 217   3        ctr1=0;         //发送
 218   3        SBUF0=tbuf[sbufda];
 219   3        }
 220   2        else
 221   2        {
 222   3         ctr1=1;              //接收
 223   3         sbufda=0;
 224   3         recieve_flag=1;
 225   3         }
 226   2      }
 227   1      //temp=SBUF0;
 228   1      //ReceiveBit=1;
 229   1      
 230   1      
 231   1      else
 232   1      { 
 233   2      RI0=0;
 234   2      rbuf[0]=rbuf[1];
 235   2      rbuf[1]=rbuf[2];
 236   2      rbuf[2]=rbuf[3];
 237   2      rbuf[3]=rbuf[4];
 238   2      rbuf[4]=rbuf[5];
 239   2      rbuf[5]=rbuf[6];
 240   2      rbuf[6]=rbuf[7];
 241   2      rbuf[7]=SBUF0;
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 5   

 242   2      if(rbuf[0]==ADDR)
 243   2      {
 244   3       crc=crc16(rbuf,8);
 245   3       if(crc==0)
 246   3       {
 247   4        switch(rbuf[1])
 248   4        {
 249   5        case 0x03       :
 250   5              tbuf[0]=rbuf[0];
 251   5                      tbuf[1]=rbuf[1];
 252   5                      tbuf[2]=(rbuf[5])<<1;
 253   5                      point=tbuf[2];
 254   5                      sent=3;
 255   5                      aba=rbuf[3];
 256   5                      for(i=0;i<point;i++)
 257   5                      {
 258   6                       tbuf[sent]=table[aba];
 259   6                       aba++;
 260   6                       sent++;
 261   6                       }
 262   5                       crc=crc16(tbuf,sent);
 263   5                       sent++;
 264   5                       tbuf[sent]=crc/256;
 265   5                       sent--;
 266   5                       tbuf[sent]=crc%256;
 267   5                       ctr1=0;
 268   5                       SBUF0=tbuf[0];
 269   5                       ES0=1;
 270   5                       sent++;
 271   5                       tx_count=sent;
 272   5                       sbufda=0;
 273   5                      
 274   5                       break;
 275   5         case 0x04 :
 276   5              tbuf[0]=rbuf[0];
 277   5                      tbuf[1]=rbuf[1];
 278   5                      tbuf[2]=2;
 279   5                      tbuf[3]=kcc1;
 280   5                      tbuf[4]=kcc2;
 281   5                      sent=5;
 282   5                      crc=crc16(tbuf,sent);
 283   5                      sent++;
 284   5                      tbuf[sent]=crc/256;
 285   5                      sent--;
 286   5                      tbuf[sent]=crc%256;
 287   5                      ctr1=0;
 288   5                      SBUF0=tbuf[0];
 289   5                      ES0=1;
 290   5                      sent++;
 291   5                      tx_count=sent;
 292   5                      sbufda=0;
 293   5                      break;
 294   5          case 0x06 :
 295   5              tbuf[0]=rbuf[0];
 296   5                      tbuf[1]=rbuf[1];
 297   5                      tbuf[2]=rbuf[2];
 298   5                      tbuf[3]=rbuf[3];
 299   5                      tbuf[4]=rbuf[4];
 300   5                      tbuf[5]=rbuf[5];
 301   5                      tbuf[6]=rbuf[6];
 302   5                      tbuf[7]=rbuf[7];
 303   5                      aaa=rbuf[4];
C51 COMPILER V8.08   USBUART0                                                              05/11/2008 16:41:24 PAGE 6   

 304   5                      bbb=rbuf[5];
 305   5                      ctr1=0;
 306   5                      SBUF0=tbuf[0];
 307   5                      ES0=1;
 308   5                      sent++;
 309   5                      tx_count=sent;
 310   5                      sbufda=0;
 311   5                      break;
 312   5              default:
 313   5                  break;
 314   5                      }
 315   4                            }
 316   3                      }
 317   2                      }
 318   1      }
 319          
 320          
 321          uint crc16(uchar *str,uint num)
 322          { 
 323   1       uint i1,j1,c1,crc1;
 324   1       crc1=0xffff;
 325   1       for(i1=0;i1<num;i1++)
 326   1       {
 327   2        c1=str[i1]&0x00ff;
 328   2        crc1^=c1;
 329   2        for(j1=0;j1<8;j1++)
 330   2        {
 331   3         if(crc1&0x0001)
 332   3         {
 333   4          crc1>>=1;
 334   4              crc1^=0xa001;
 335   4          }
 336   3              else
 337   3              crc1>>=1;
 338   3         }
 339   2        }
 340   1        return(crc1);
 341   1        }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    595    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =     18    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     41       8
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      2    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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