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

📄 userspc3.lst

📁 使用8052单片机控制SPC3总线桥实现PROFIBUS-DP现场总线的从站部分C程序源码
💻 LST
📖 第 1 页 / 共 3 页
字号:
 196          UBYTE                   diag_len;
 197          UWORD                   user_baud_value;
 198          UBYTE                   user_diag_flag;
 199          UBYTE                   *io_byte_ptr;
 200          UBYTE                   i;
 201          UBYTE                   zyk_wd_state;
 202          UBYTE                   zyk_dps_state;
 203          UBYTE                   prm_tst_buf[32]= {0,0,0,0,0,0,0,0,0,0,
 204                                                                                                  0,0,0,0,0,0,0,0,0,0,
 205                                                                                                  0,0,0,0,0,0,0,0,0,0,0,0};
 206          enum ERRORCODES error_code;
 207          
 208          /* Locate the structure of the SPC3 in the address space               */
 209          
 210          SPC3 xdata spc3 _at_ 0x1000;
 211          
 212          
 213          /************************************************************************/
 214          /*  D e s c r i p t i o n :                                             */
 215          /*                                                                      */
 216          /*  USER-TASK                                                           */
 217          /************************************************************************/
 218          
 219          
 220          void    main ()
 221          {
 222   1      
 223   1      #if defined __C51__
 224   1              UBYTE xdata * xdata loesch;
 225   1              UWORD zaehl;
 226   1      
 227   1              SPC3_RESET = SPC3_RESET_SET;        /* force the Reset Pin          */
 228   1              for(i = 100; i ; i--);
 229   1              SPC3_RESET = !SPC3_RESET_SET;
 230   1      
 231   1              HW_WATCHDOG_TRIGGER = 1;            /* Retrigger the HW Watchdog    */
 232   1              HW_WATCHDOG_TRIGGER = 0;
 233   1      
 234   1              SPC3_INTERRUPT_ENABLE = !I_ENABLE;  /* Set the interrupt parameters */
 235   1              SPC3_INTERRUPT_PRIOR = P_HIGH;      /* of the microprocessor        */
 236   1      
 237   1              loesch = &spc3.int_req.b[0];        /* Delete the SPC3 internal RAM */
 238   1              for (zaehl=0x16; zaehl<=0x05ff; zaehl++)
 239   1                      {
 240   2                      *(loesch + zaehl) = 00;
 241   2                      }
C51 COMPILER V7.50   USERSPC3                                                              10/24/2007 09:35:20 PAGE 5   

 242   1      
 243   1      #elif   _C166
                      T3OUT = 1;                          /* force the SPC3 reset pin (P3.3) */
                      for(i = 100; i ; i--);
                      T3OUT = 0;
                      CC11IC = 0x18;                      /* SPC3-Interrupt: ILVL = 6, GLVL = 0 */
              #else
                      UBYTE * loesch;
                      UWORD zaehl;
              
                      outp(SPC3_RESET,0x21);
                      outp(SPC3_RESET,0x00);
                      loesch = &spc3.int_req.b[0];        /* Delete the SPC3 internal RAM */
                      for (zaehl=0x16; zaehl<=0x05ff; zaehl++)
                      {
                              *(loesch + zaehl) = 00;
                      }
              #endif
 260   1      
 261   1      
 262   1      /* activate the indication functions */
 263   1      SPC3_SET_IND(GO_LEAVE_DATA_EX | WD_DP_MODE_TIMEOUT | NEW_GC_COMMAND |\
 264   1                                 NEW_SSA_DATA | NEW_CFG_DATA | NEW_PRM_DATA | BAUDRATE_DETECT);
 265   1      
 266   1      /* set the watchdog value in the SPC3, which checks the microprocessor */
 267   1      DPS2_SET_USER_WD_VALUE(20000);
 268   1      
 269   1      
 270   1      /* In this example the input and output bytes are transfered to the
 271   1         IO area, which is addressed by the io_byte_ptr. In the case of the IM183
 272   1         there is RAM. */
 273   1      
 274   1      
 275   1              io_byte_ptr = ((UBYTE*) 0x2E000L);
 276   1      
 277   1      for (i=0; i<2; i++)
 278   1              {
 279   2              (*(io_byte_ptr + i)) = 0;
 280   2              }
 281   1      
 282   1      /* fetch the station address, in this case the station address
 283   1         is fixed in EPROM*/
 284   1      this_station = OWN_ADDRESS;
 285   1      
 286   1      /* get the Identnumber    */
 287   1      ident_numb_high = IDENT_HIGH;
 288   1      ident_numb_low =  IDENT_LOW;
 289   1      
 290   1      /* Allow not the change of the slave address by the PROFIBUS DP      */
 291   1      /* Attention: The set_slave_address service is not blocked by this */
 292   1      real_no_add_chg = TRUE;
 293   1      
 294   1      
 295   1      /* Reset the User und DPS */
 296   1      user_dps_reset();
 297   1      
 298   1      
 299   1      /* Enable the SPC3 interrupt in the interupt controller of the microprocessor */
 300   1      #if defined __C51__
 301   1        SPC3_INTERRUPT_ENABLE = I_ENABLE;
 302   1        GLOBAL_INTERRUPT_ENABLE = I_ENABLE;
 303   1      #elif _C166
C51 COMPILER V7.50   USERSPC3                                                              10/24/2007 09:35:20 PAGE 6   

                CC11IE = 1;
              #endif
 306   1      
 307   1      
 308   1      for (;;)
 309   1              {   /*=== Begin  of the endless loop ===*/
 310   2              zyk_wd_state = SPC3_GET_WD_STATE();    /*for info.: the actuall WD State*/
 311   2      
 312   2              zyk_dps_state = DPS2_GET_DP_STATE();   /*for info.: the actuall PROFIBUS DP State*/
 313   2      
 314   2      
 315   2              DPS2_RESET_USER_WD();           /* Trigger the user watchdog of the SPC3 */
 316   2      
 317   2      #ifdef __C51__
 318   2              HW_WATCHDOG_TRIGGER = 1;        /* Retrigger the HW Watchdog of the IM183*/
 319   2              HW_WATCHDOG_TRIGGER = 0;
 320   2      #endif
 321   2      
 322   2      /*============ Handling of the output data =================*/
 323   2      
 324   2              if (DPS2_POLL_IND_DX_OUT()) /* are new output date available? */
 325   2                      {
 326   3                      /* Confirm the receiving */
 327   3                      DPS2_CON_IND_DX_OUT();
 328   3      
 329   3                      /* Get the pointer to the actual output data */
 330   3                      user_output_buffer_ptr = DPS2_OUTPUT_UPDATE();
 331   3      
 332   3      
 333   3                      /* Example: Copy the output data to the IO */
 334   3                      for (i=0; i<user_io_data_len_ptr->outp_data_len; i++)
 335   3                         {
 336   4                         (*((io_byte_ptr) + i)) = (*(((UBYTE SPC3_PTR_ATTR*) user_output_buffer_ptr) + i));
 337   4                         }
 338   3                      }
 339   2      
 340   2      
 341   2      /*============ Handling of the input data =================*/
 342   2      
 343   2              /* Write the input data from the periphery to the ASIC */
 344   2              for (i=0; i<user_io_data_len_ptr->inp_data_len; i++)
 345   2                      {
 346   3                      *(((UBYTE SPC3_PTR_ATTR*) user_input_buffer_ptr) + i) = *((io_byte_ptr) + i);
 347   3                      }
 348   2      
 349   2              /* Give the actuall pointer / data to the SPC3/DPS2 an get a new pointer,
 350   2                         where the next input data can be written */
 351   2              user_input_buffer_ptr = DPS2_INPUT_UPDATE();
 352   2      
 353   2      
 354   2      /*== Handling of the external diagnosis and other user defined actions =====*/
 355   2      /* ATTENTION:         this is only an example              */
 356   2      
 357   2      /* Use the first Byte of the Input data as a service byte  */
 358   2      /*  for the change diag function                           */
 359   2      
 360   2              dps_chg_diag_srvc_byte_new = *((UBYTE*)(io_byte_ptr));
 361   2      
 362   2              if (user_diag_flag) /* is a diagnosis buffer available? */
 363   2                      {
 364   3                      /* Is there a change in the service byte (1.input byte) */
 365   3                      if (dps_chg_diag_srvc_byte_new == dps_chg_diag_srvc_byte_old)
C51 COMPILER V7.50   USERSPC3                                                              10/24/2007 09:35:20 PAGE 7   

 366   3                              {
 367   4                               /* no action */
 368   4                               }
 369   3                      else
 370   3                              {
 371   4                              /*== Handling of the external diagnosis  =====*/
 372   4                              /* only the least significant 3 byte are used */
 373   4                              if ((dps_chg_diag_srvc_byte_new & 0x07) !=
 374   4                                        (dps_chg_diag_srvc_byte_old & 0x07))
 375   4                                      {
 376   5                                      /* Mask the 3 bits */
 377   5                                      diag_service_code = dps_chg_diag_srvc_byte_new & 0x07;
 378   5      
 379   5                                      /* Write the length of the diagnosis data to the SPC3 */
 380   5                                      if (dps_chg_diag_srvc_byte_new & 0x01)
 381   5                                              diag_len = 16;   /* max. value of the IM308B */
 382   5                                      else
 383   5                                              diag_len = 6;
 384   5                                      diag_len = DPS2_SET_DIAG_LEN(diag_len);
 385   5      
 386   5                                      /* Write the external diagnosis data to the SPC3 */
 387   5                                      build_diag_data_blk ((struct diag_data_blk *)user_diag_buffer_ptr);
 388   5      
 389   5                                      /* Set the service code             */
 390   5                                      /* 0x01 External diagnosis          */
 391   5                                      /* 0x02 Static   diagnosis          */
 392   5                                      /* 0x04 External diagnosis Overflow */
 393   5                                      DPS2_SET_DIAG_STATE(diag_service_code);
 394   5      
 395   5      
 396   5                                      /* Trigger the diagnosis update in the SPC3*/
 397   5                                      DPS2_DIAG_UPDATE();
 398   5      
 399   5                                      /* Store "no diagnosis buffer available"  */

⌨️ 快捷键说明

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