C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 1   


C51 COMPILER V9.56.0.0, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN .\Objects\main.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE ..\app\main.c LARGE OPTIMIZE(8,SPEED) BROWSE INCDIR(..\bsp;..\app) DEBUG
                    - OBJECTEXTEND PRINT(.\Listings\main.lst) TABS(2) OBJECT(.\Objects\main.obj)

line level    source

   1          //=================================================//
   2          #define ALLOCATE_EXTERN
   3          #include "stdio.h"
   4          #include "bsp.h"
   5          #include "typedef.h"
   6          #include "ModBusRTUSlave.h"
   7          #include "intrins.h"
   8          //==============接口定义==========================
   9          
  10          void load_set_from_slave(void);
  11          
  12          bit wait_slave_res = 0;
  13          u8 prev_board=0,cur_board=1;
  14          u8 cur_board_sid=1;
  15          u8 modbus_send_buf[20]={0};
  16          u16 regIndex;
  17          void deal_modbusrtu_master(u8 *recvData,u8 datalen)
  18          {
  19   1        u16 index,i;
  20   1        u16 ioValue;
  21   1        if(recvData[0] == cur_board_sid)//如果是正确的回复
  22   1        {
  23   2          if(datalen == 8 && recvData[1] == 0x05){
  24   3            index=(cur_board_sid-1)+(cur_board-1)*6;
  25   3            if(index <= 7) Modbus_OutputIO[0] &= ~(1<<index);
  26   3            else if(index >7 && index <16) Modbus_OutputIO[1] &= ~(1<<(index%8));
  27   3            else Modbus_OutputIO[2] &= ~(1<<(index%8));
  28   3            wait_slave_res = 0;
  29   3            load_set_from_slave();
  30   3          }
  31   2          else if(recvData[1] == 0x06 && datalen==8  &&HoldReg_Changed==1)//修改下位机阈值
  32   2          {
  33   3            if(HoldReg_Board_stationId == 1) index = (HoldReg_BoardId-1)*81+HoldReg_Board_addr;
  34   3            else index = (HoldReg_BoardId-1)*81+11+(HoldReg_Board_stationId-2)*14+HoldReg_Board_addr;
  35   3            Modbus_HoldReg[index] = (u16)recvData[4]<<8;
  36   3            Modbus_HoldReg[index] |= recvData[5];
  37   3            wait_slave_res = 0;
  38   3            HoldReg_Changed = 0;
  39   3          }else if(recvData[1] == 0x02 && recvData[2] == 0x02){//读取下位机状态
  40   3            ioValue = (u16)recvData[4]<<8 | recvData[3];
  41   3            if(cur_board < 3){
  42   4              if(cur_board_sid == 1){
  43   5                index = (cur_board -1)*96;
  44   5                for(i = 0; i<11;i++){
  45   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  46   6                }
  47   5              }else{
  48   5                index = (cur_board -1)*96+11+(cur_board_sid -2)*14;
  49   5                for(i=0;i<14;i++)
  50   5                {
  51   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  52   6                }
  53   5              }
  54   4            }else{
C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 2   

  55   4              if(cur_board_sid == 1){
  56   5                index = 192;
  57   5                for(i = 0;i<8;i++){
  58   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  59   6                }
  60   5                index = 200;
  61   5                for(i=8;i<11;i++){
  62   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  63   6                }
  64   5              }else if( cur_board_sid==2){
  65   5                index = 211;
  66   5                for(i = 0;i<14;i++){
  67   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  68   6                }         
  69   5              }else if(cur_board_sid == 3){
  70   5                index = 225;
  71   5                for(i = 0;i<14;i++){
  72   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  73   6                }
  74   5              }else if(cur_board_sid == 4){
  75   5                Set_InputIO_Value(239,(ioValue & 0x0001)?1:0);
  76   5                Set_InputIO_Value(240,(ioValue & 0x0002)?1:0);
  77   5                index = 256;
  78   5                for(i = 2;i<14;i++){
  79   6                  Set_InputIO_Value(index+i-2,(ioValue & (u16)(1<<i))?1:0);
  80   6                }
  81   5              }else{
  82   5                index = 268+(cur_board_sid-5)*14;
  83   5                for(i = 0;i<14;i++){
  84   6                  Set_InputIO_Value(index+i,(ioValue & (u16)(1<<i))?1:0);
  85   6                }           
  86   5              }         
  87   4            }
  88   3            wait_slave_res = 0;
  89   3            if(cur_board_sid<6) cur_board_sid++;
  90   3            else
  91   3            {
  92   4              cur_board_sid=1;
  93   4              if(cur_board<3) cur_board++;
  94   4              else cur_board=1;
  95   4            }
  96   3          }
  97   2        }
  98   1      }
  99          //void load_set_from_flash(void)
 100          //{
 101          //  u16 i;
 102          //  Flash_ReadArr(BOARD1_SET_ADD,126,(u8*)&Modbus_HoldReg[1]);
 103          //  Modbus_HoldReg[0] = Modbus_HoldReg[1];
 104          //  Modbus_HoldReg[64]=Modbus_HoldReg[63];
 105          //  Flash_ReadArr(BOARD2_SET_ADD,126,(u8*)&Modbus_HoldReg[66]);
 106          //  Modbus_HoldReg[65] = Modbus_HoldReg[66];
 107          //  Modbus_HoldReg[129]=Modbus_HoldReg[128];
 108          //  Flash_ReadArr(BOARD3_SET_ADD,126,(u8*)&Modbus_HoldReg[131]);
 109          //  Modbus_HoldReg[130] = Modbus_HoldReg[131];
 110          //  Modbus_HoldReg[194]=Modbus_HoldReg[193];
 111          //  if(Modbus_HoldReg[0] == 0 || Modbus_HoldReg[0] == 0xFFFF)
 112          //  {
 113          //    for(i=0;i<195;i++){
 114          //      Modbus_HoldReg[i] = 2048;
 115          //    }
 116          //    Flash_EraseBlock(BOARD1_SET_ADD);
C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 3   

 117          //    Flash_WriteArr(BOARD1_SET_ADD,126,(u8*)&Modbus_HoldReg[1]);
 118          //    Flash_EraseBlock(BOARD2_SET_ADD);
 119          //    Flash_WriteArr(BOARD2_SET_ADD,126,(u8*)&Modbus_HoldReg[66]);
 120          //    Flash_EraseBlock(BOARD3_SET_ADD);
 121          //    Flash_WriteArr(BOARD3_SET_ADD,126,(u8*)&Modbus_HoldReg[131]);
 122          //  }
 123          //}
 124          void send_collect_frame(void)
 125          {
 126   1        u16 calCRC;
 127   1        
 128   1        modbus_send_buf[0]=cur_board_sid;
 129   1        modbus_send_buf[1]=0x02;
 130   1        modbus_send_buf[2]=0;
 131   1        modbus_send_buf[3]=0;
 132   1        modbus_send_buf[4]=0;
 133   1        modbus_send_buf[5]=14;
 134   1        calCRC=crc16(modbus_send_buf,6);
 135   1        modbus_send_buf[6]=(calCRC>>8)&0xFF;
 136   1        modbus_send_buf[7]=calCRC&0xFF;
 137   1        if(cur_board != prev_board)
 138   1        {
 139   2          bsp_uart2_init(cur_board);
 140   2          prev_board = cur_board;
 141   2        }
 142   1        wait_slave_res = 1;
 143   1        reset_timeout_countdown(50);
 144   1        bsp_uart2_send(modbus_send_buf,8,cur_board);
 145   1        
 146   1      }
 147          void build_slave_adj(u8 id)
 148          {
 149   1        u16 calCRC;
 150   1        modbus_send_buf[0]=id;
 151   1        modbus_send_buf[1]=0x05;
 152   1        modbus_send_buf[2]=0;
 153   1        modbus_send_buf[3]=0;
 154   1        modbus_send_buf[4]=0;
 155   1        modbus_send_buf[5]=0xFF;
 156   1        calCRC=crc16(modbus_send_buf,6);
 157   1        modbus_send_buf[6]=(calCRC>>8)&0xFF;
 158   1        modbus_send_buf[7]=calCRC&0xFF; 
 159   1      }
 160          void workprocess(void)
 161          {
 162   1        
 163   1        u16 calCRC;
 164   1        u8 i;
 165   1        u32 setSlaveAdjustCmd=((u32)(Modbus_OutputIO[2])<<16)|((u32)(Modbus_OutputIO[1])<<8) | Modbus_OutputIO[0]
             -;
 166   1        //1、检查输出开关量
 167   1        if(wait_slave_res == 0 && setSlaveAdjustCmd != 0)
 168   1        {
 169   2          for(i=0;i<18;i++)
 170   2          {
 171   3            if((setSlaveAdjustCmd>>i) & 0x00000001) break;
 172   3          }
 173   2          if(i!=18)
 174   2          {
 175   3            cur_board = i/6 +1;
 176   3            cur_board_sid = i%6+1;
 177   3            build_slave_adj(cur_board_sid);
C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 4   

 178   3            if(cur_board != prev_board)
 179   3            {
 180   4              bsp_uart2_init(cur_board);
 181   4              prev_board = cur_board;
 182   4            }
 183   3            wait_slave_res = 1;
 184   3            reset_timeout_countdown(50);
 185   3            bsp_uart2_send(modbus_send_buf,8,cur_board);
 186   3          }
 187   2        }
 188   1        //2、检查保持寄存器是否被修改
 189   1        if(HoldReg_Changed == 1 && wait_slave_res == 0)
 190   1        {
 191   2          if(HoldReg_BoardId != prev_board)
 192   2          {
 193   3            bsp_uart2_init(HoldReg_BoardId);
 194   3            cur_board=HoldReg_BoardId;
 195   3          }
 196   2          if(HoldReg_Board_stationId == 1) 
 197   2            regIndex = (HoldReg_BoardId-1)*81 + HoldReg_Board_addr;
 198   2          else
 199   2            regIndex = (HoldReg_BoardId-1)*81+11+(HoldReg_Board_stationId -2)*14 + HoldReg_Board_addr;
 200   2          modbus_send_buf[0]=HoldReg_Board_stationId;
 201   2          modbus_send_buf[1]=0x06;
 202   2          modbus_send_buf[2]=0;
 203   2          modbus_send_buf[3]=HoldReg_Board_addr;
 204   2          modbus_send_buf[4]=(HoldReg_new_value>>8)&0xFF;
 205   2          modbus_send_buf[5]=HoldReg_new_value&0xFF;
 206   2          calCRC=crc16(modbus_send_buf,6);
 207   2          modbus_send_buf[6]=(calCRC>>8)&0xFF;
 208   2          modbus_send_buf[7]=calCRC&0xFF;
 209   2          wait_slave_res = 1;
 210   2          prev_board=cur_board;
 211   2          cur_board_sid=HoldReg_Board_stationId;
 212   2          reset_timeout_countdown(50);    
 213   2          bsp_uart2_send(modbus_send_buf,8,prev_board);
 214   2        }
 215   1      
 216   1      }
 217          void build_read_slave_set(u8 id)
 218          {
 219   1        u16 calCRC;
 220   1        modbus_send_buf[0]=id;
 221   1        modbus_send_buf[1]=0x03;
 222   1        modbus_send_buf[2]=0;
 223   1        modbus_send_buf[3]=0;
 224   1        modbus_send_buf[4]=0;
 225   1        modbus_send_buf[5]=14;
 226   1        calCRC=crc16(modbus_send_buf,6);
 227   1        modbus_send_buf[6]=(calCRC>>8)&0xFF;
 228   1        modbus_send_buf[7]=calCRC&0xFF; 
 229   1      }
 230          void load_set_from_slave(void)
 231          {
 232   1        u32 cur_ms;
 233   1        u8 i,j,k;
 234   1        bit timeout = 0;
 235   1        for(i=1;i<4;i++)
 236   1        {
 237   2          bsp_uart2_init(i);
 238   2          for(j=1;j<7;j++)
 239   2          {
C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 5   

 240   3            cur_ms = g_clock_ms;
 241   3            build_read_slave_set(j);
 242   3            bsp_uart2_send(modbus_send_buf,8,i);
 243   3            timeout = 0;
 244   3            while( rs485_has_recv_timeout2() == 0)
 245   3            {
 246   4              if(g_clock_ms - cur_ms > 50){
 247   5                timeout = 1;
 248   5                break;
 249   5              }
 250   4            }
 251   3            if(timeout==0)
 252   3            {
 253   4              if(guc_Uartcnt2==33 && guc_Uartbuf2[0] == j && guc_Uartbuf2[1] == 0x03)
 254   4              {
 255   5                if(j==1){
 256   6                  for(k=0;k<11;k++){
 257   7                    Modbus_HoldReg[(i-1)*81+k] = ((u16)guc_Uartbuf2[3+k*2]<<8)|guc_Uartbuf2[4+k*2];
 258   7                  }
 259   6                }else{
 260   6                  for(k=0;k<14;k++)
 261   6                  {
 262   7                    Modbus_HoldReg[(i-1)*81+11+(j-2)*14+k] = ((u16)guc_Uartbuf2[3+k*2]<<8)|guc_Uartbuf2[4+k*2];
 263   7                  }
 264   6                }
 265   5              }
 266   4            }
 267   3            delay_ms(50);
 268   3          }
 269   2          
 270   2        }
 271   1        cur_board = 1;
 272   1      
 273   1      }
 274          void main(void)
 275          {
 276   1        bsp_init();
 277   1        Modbus_RTU_Init(guc_Uartbuf,guc_UartTxbuf,bsp_uart_send_buff);
 278   1        //load_set_from_flash();
 279   1        delay_ms(500);
 280   1        //读取各下位机阈值
 281   1        load_set_from_slave();
 282   1        delay_ms(500);
 283   1        load_set_from_slave();
 284   1        while(1)
 285   1        {
 286   2      //    check_modbus_request_frame();
 287   2          if(ms100_flag)
 288   2          {
 289   3            ms100_flag = 0;
 290   3            if(wait_slave_res == 0) send_collect_frame();
 291   3          }
 292   2          if(s1_flag)
 293   2          {
 294   3            s1_flag = 0;
 295   3          }
 296   2          if(rs485_has_recv_timeout())
 297   2          {
 298   3            ModBusRtuSlave_Service(guc_Uartcnt);
 299   3            guc_Uartcnt = 0;
 300   3          }
 301   2          if(wait_slave_res ==1){//如果正在等待下位机回复
C51 COMPILER V9.56.0.0   MAIN                                                              02/14/2025 12:21:42 PAGE 6   

 302   3            if(rs485_has_recv_timeout2())
 303   3            {
 304   4              stop_timeout_countdown();
 305   4              deal_modbusrtu_master(guc_Uartbuf2,guc_Uartcnt2);
 306   4              guc_Uartcnt2=0;
 307   4            }
 308   3            if(timeout_flag==1)
 309   3            {
 310   4              wait_slave_res = 0;
 311   4              if(cur_board_sid<5) cur_board_sid++;
 312   4              else
 313   4              {
 314   5                cur_board_sid=1;
 315   5                if(cur_board<3) cur_board++;
 316   5                else cur_board=1;
 317   5              }       
 318   4            }
 319   3          }
 320   2          workprocess();
 321   2          hal_feed_dog();
 322   2        }
 323   1      }
 324          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2888    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =     25      21
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      1       1
END OF MODULE INFORMATION.


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