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

📄 main.lst

📁 51 单片机上网
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.07   MAIN                                                                  06/30/2006 16:21:50 PAGE 1   


C51 COMPILER V7.07, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE main.c LARGE OPTIMIZE(SIZE) BROWSE

stmt level    source

   1          // 本程序的晶体      24MHz
   2          // 本程序的缺省IP    192.168.0.7
   3          // 本程序的缺省mask  255.255.255.0
   4          // 本程序的缺省mac       0x1234567890ab
   5          // 本程序的缺省gate  0.0.0.0(无网关)
   6          // 本程序的缺省端口号port  9000
   7          // 本程序的缺省remote_ip  192.168.0.1
   8          
   9          #include "w77e58.h"
  10          #include <absacc.h>
  11          #include <stdio.h>
  12          #include <stdlib.h>
  13          #include <string.h>
  14          #include <intrins.h>
  15          #include "ethernet.h"
  16          #include "ip.h"
  17          #include "udp.h"
  18          
  19          #define SENDDEV     1
  20          //#define RECVDEV     1
  21          
  22          #define FALSE       0
  23          #define TRUE        1
  24          
  25          //#define timer_5ms_value  0xDC00
  26          //#define timer_10ms_value 0xB800
  27          //#define timer_20ms_value 0x7000
  28          
  29          #define ARP_TX      1               /* Client state: sending ARP */
  30          #define ARP_RX      2               /*               received ARP */
  31          #define CLIENT_DONE 3               /*               completed */
  32          
  33          #define HIGH(x) (x &0xff00)>>8
  34          #define LOW(x) (x &0xff)
  35          #define BCASTADDR     0xff,0xff,0xff,0xff,0xff,0xff     // broadcast address
  36          BYTE bcast[MACLEN]={BCASTADDR};         //  该变量为源广播地址mac
  37          
  38          #ifdef SENDDEV
  39          BYTE code default_mac[MACLEN]={0x00,0x13,0x46,0x34,0x55,0x66}; 
  40          #endif
  41          
  42          #ifdef RECVDEV
              BYTE code default_mac[MACLEN]={0x00,0x13,0x46,0x35,0x44,0x55}; 
              #endif
  45          
  46          bit  uart_receive_enable;               // 串口接收允许标志
  47          bit  uart_receiving;                            // 串口正在接收标志
  48          bit  uart_received_finished;            // 串口接收完成标志
  49          bit  uart_trans_willing;                        // 串口将要发送标志
  50          bit  uart_trans_finished;                       // 串口发送完成标志,表示串口处于发送空闲状态
  51          
  52          unsigned char uart_receive_buf[1024];   // 接收缓冲区
  53          unsigned char uart_trans_buf[1024];             // 发送缓冲区
  54          unsigned char uart_trans_count;                 // 发送数据计数器
  55          unsigned char uart_receive_count;               // 接收的数据计数器
C51 COMPILER V7.07   MAIN                                                                  06/30/2006 16:21:50 PAGE 2   

  56          unsigned char uart_trans_length;                // 将要发送的数据长度
  57          
  58          unsigned char uart_char_space;                  // 串口帧内间隔计数器(10ms) 最大为60ms
  59          unsigned char uart_frame_space;                 // 串口帧间间隔计数器(10ms) 最小为80ms
  60          //unsigned char data dog;                               // 看门狗计数器(10ms)  最大为120 ms
  61          
  62          bit being_echo;                                             // 网络要求远端返回数据标志,表示要判断网络超时
  63          bit remote_echo;
  64          bit first_arp; 
  65          unsigned int   net_overtime_count;      // 网络发送超时计数器
  66          
  67          ETHERFRAME etherframe;
  68          NODE  locnode;                      /* My Ethernet and IP addresses */
  69          NODE  remnode;                      /* Remote node */
  70          
  71          extern BYTE mymac[MACLEN];                      //  该变量为ethernet.c 用源地址mac
  72          void system_init(void);
  73          void flush_gate_mac();
  74          void do_uart_process(void);
  75          int  do_net_process(ETHERFRAME *efp,int rxlen);
  76          int  udp_receive(ETHERFRAME *efp, int len);
  77          void send_ip(void);
  78          unsigned long runtime;
  79          
  80          extern void serial_init(void);
  81          extern void serial0_str(unsigned char *str);
  82          extern void serial0_bytes(unsigned char *dat, unsigned char len);
  83          extern void serial1_str(unsigned char *str);
  84          extern void serial1_bytes(unsigned char *dat, unsigned char len);
  85          //extern void memory_test(void);
  86          //extern void memory_sptest(void);
  87          //extern void videotest(void);
  88          
  89          extern void RS485_232(unsigned char xdata *dat, unsigned int len);
  90          extern unsigned int RS485_getAll(unsigned char xdata *dat);
  91          extern void resetWD(void);
  92          extern unsigned char check_rtl8019as(void);
  93          
  94          void system_init(void)
  95          {
  96   1              EA=0;                   
  97   1      //      SCON=0xD2;                                      // SM0,SM1,SM2,REN,TB,RB,TI,RI 
  98   1                                                                      // 9位数据位,一位停止位,允许串行中断 
  99   1      //      PCON=0x80;                      // SMOD=1, 波特率加倍
 100   1      //      TMOD=0x21;                      // GATE  C/T M1 M0 GATE C/T M1 M0 
 101   1                                              // 定时器1在自动装入方式,0 位16位方式
 102   1      //      TCON=0x50;                      // TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
 103   1                                                                      // 定时器运行状态,不允许外部中断
 104   1      //      TH1=BPS4800;
 105   1      //      TL1=BPS4800;
 106   1      //      TH0=HIGH(timer_10ms_value);     //16位10ms 定时器
 107   1      //      TL0=LOW(timer_10ms_value);
 108   1      //      IE=0x92;                                // EA ?  ? ES ET1 EX1 ET0 EX0   
 109   1      //      IP=0x02;
 110   1      //      clear_watchdog();
 111   1      }
 112          
 113          
 114          void main(void)
 115          {
 116   1              int rxlen;
 117   1              int ret;
C51 COMPILER V7.07   MAIN                                                                  06/30/2006 16:21:50 PAGE 3   

 118   1      //      unsigned char temp[6];
 119   1      //      clear_watchdog();
 120   1      //      delay_ms(2);
 121   1              system_init();                          //设置单片机的定时器 T0 (10ms),及 串口的波特率4800bps
 122   1              serial_init();
 123   1      //      for(ret=0; ret<32768; ret++){
 124   1                      serial0_str("rs232 port test at 57600bps,");
 125   1                      serial0_bytes("test passed.", 11);
 126   1      //      }
 127   1                      serial1_str("rs485 port test at 2400bps,");
 128   1                      serial1_bytes("test passed.", 11);
 129   1      
 130   1      //      memory_test();
 131   1      //      memory_sptest();
 132   1              uart_receiving=FALSE;                   // 串口正在接收标志 为 false
 133   1              uart_receive_enable=TRUE;               // 串口接收允许为   TRUE
 134   1              uart_received_finished=FALSE;   // 串口接收完成标志 为 false
 135   1              uart_trans_count=0;                             // 串口发送完成字节数
 136   1              uart_receive_count=0;
 137   1              uart_trans_willing=0;                   // 串口将要发送请求标志
 138   1          uart_trans_finished=TRUE;           // 串口发送完成,处于发送空闲状态
 139   1          uart_char_space=0;
 140   1              uart_frame_space=0;
 141   1      
 142   1      #ifdef SENDDEV
 143   1              locnode.ip=0xc0a80008;
 144   1              locnode.mask=0xffffff00;
 145   1              locnode.gate=0xc0a8032;
 146   1              memcpy(mymac,default_mac,6);                
 147   1              memcpy(locnode.mac,mymac,6);
 148   1              locnode.port=9000;
 149   1              remnode.port=locnode.port;
 150   1              remnode.ip=0xc0a80007;
 151   1              serial0_str("I am SEND CONTROL DEVICE,");
 152   1      #endif
 153   1      
 154   1      #ifdef RECVDEV
                      locnode.ip=0xc0a80007;
                      locnode.mask=0xffffff00;
                      locnode.gate=0xc0a80031;
                      memcpy(mymac,default_mac,6);                
                      memcpy(locnode.mac,mymac,6);
                      locnode.port=9000;
                      remnode.port=locnode.port;
                      remnode.ip=0xc0a80008;
                      serial0_str("I am RECEIVE CONTROL DEVICE,");
              #endif
 165   1      
 166   1              uart_trans_length=0;
 167   1      //      clear_watchdog();
 168   1              if(check_rtl8019as())
 169   1                      serial0_str("NOT Find rtl8019as");
 170   1              else
 171   1                      serial0_str("FOUND rtl8019as network controller");
 172   1      
 173   1              resetnic();
 174   1              net_overtime_count=0;
 175   1              runtime=0;
 176   1              first_arp=1;
 177   1              flush_gate_mac();
 178   1              
 179   1      //      while(1){
C51 COMPILER V7.07   MAIN                                                                  06/30/2006 16:21:50 PAGE 4   

 180   1      //              while(RI==0);
 181   1      //              RI=0;
 182   1      //              videotest();
 183   1      //      }
 184   1      
 185   1      #ifdef SENDDEV
 186   1              while (1) {
 187   2      //              if (dog>=60) {  clear_watchdog(); }
 188   2                      uart_receive_count= RS485_getAll(uart_receive_buf);
 189   2                      if (uart_receive_count)
 190   2                                      do_uart_process();
 191   2                                      // 只在串口发送完成后才处理网络接收
 192   2                              // 旨在保证在单发送缓冲区情况下不会覆盖,
 193   2                              // 在多发送缓冲区的情况下不会溢出。
 194   2                      if ((rxlen=get_ethernet(&etherframe))>0)
 195   2                      {
 196   3                                      ret=do_net_process(&etherframe,rxlen);
 197   3                                      if (ret==ARP_RX)
 198   3                                      {
 199   4                                              being_echo=FALSE;                               //  表示ARP全过程完成,对方返回了其MAC
 200   4                                              net_overtime_count=0;                   //  清除等待网络响应延时计数器,为下一次处理初始化
 201   4                                      }
 202   3                                  if (ret==CLIENT_DONE)                               // 表示应用的UDP数据包接收完成
 203   3                                      {
 204   4                                      }
 205   3                      }
 206   2                      if (WDIF==1)                    //120000 *10ms =20 minute
 207   2                      {
 208   3                                      resetWD();
 209   3                                      first_arp=1;
 210   3                                      flush_gate_mac();               //刷新网关的物理地址mac
 211   3                       }
 212   2              }
 213   1      #endif
 214   1      
 215   1      #ifdef RECVDEV
                      while (1) {
              //              if (dog>=60) {  clear_watchdog(); }
              
              //          if (uart_trans_finished)
                                      if ((rxlen=get_ethernet(&etherframe))>0)
                                      {
                                                      ret=do_net_process(&etherframe,rxlen);
                                                      if (ret==ARP_RX)
                                                      {
                                                              being_echo=FALSE;                               //  表示ARP全过程完成,对方返回了其MAC
                                                              net_overtime_count=0;                   //  清除等待网络响应延时计数器,为下一次处理初始化
                                                      }
                                          if (ret==CLIENT_DONE)                               // 表示应用的UDP数据包接收完成
                                                      {                                               
                                                              RS485_232(uart_trans_buf, uart_trans_length);
                                                      }
                                      }
              
              //              if (uart_received_finished)
              //                              do_uart_process();
                                              // 只在串口发送完成后才处理网络接收
                                      // 旨在保证在单发送缓冲区情况下不会覆盖,
                                      // 在多发送缓冲区的情况下不会溢出。
                              if (WDIF==1)                    //67108864 clock
                              {
                                              resetWD();
C51 COMPILER V7.07   MAIN                                                                  06/30/2006 16:21:50 PAGE 5   

                                              first_arp=1;
                                              flush_gate_mac();               //刷新网关的物理地址mac
                              }
                      }
              #endif
 247   1      }
 248          
 249          /*************************************************************************/
 250          // 通过计数器uart_char_space 判断串口60MS没有新数据到达就认为一帧数据接收完成
 251          // 在网络要求远端返回数据状态下,网络超时计数器加 1(10ms)

⌨️ 快捷键说明

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