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

📄 scu_bs.lst

📁 单片机串口RS485的读写操作,应用在多处理器中
💻 LST
📖 第 1 页 / 共 2 页
字号:
 253   2               {
 254   3               Message_byte_G = 1;
 255   3               }
 256   2            else
 257   2               {
 258   3               Message_byte_G = 0;
 259   3               }
 260   2      
 261   2            // Check tick data - send ack if necessary
 262   2            // NOTE: 'START' message will only be sent after a 'time out'
 263   2            if (SCU_B_SLAVE_Process_Tick_Message() == SLAVE_ID)
 264   2               {
 265   3               SCU_B_SLAVE_Send_Ack_Message_To_Master();
 266   3      
 267   3               // Feed the watchdog ONLY when a *relevant* message is received
 268   3               // (noise on the bus, etc, will not stop the watchdog...)
 269   3               //
 270   3               // START messages will NOT refresh the slave
 271   3               // - Must talk to every slave at regular intervals 
 272   3               SCU_B_SLAVE_Watchdog_Refresh();
 273   3               }
 274   2      
 275   2            // NOTE: calculations are in *TICKS* (not milliseconds)
 276   2            for (Index = 0; Index < SCH_MAX_TASKS; Index++)
 277   2               {
 278   3               // Check if there is a task at this location
 279   3               if (SCH_tasks_G[Index].pTask)
 280   3                  {
 281   4                  if (SCH_tasks_G[Index].Delay == 0)
 282   4                     {
 283   5                     // The task is due to run
 284   5                     SCH_tasks_G[Index].RunMe = 1;  // Set the run flag
 285   5         
 286   5                     if (SCH_tasks_G[Index].Period)
 287   5                        {
 288   6                        // Schedule periodic tasks to run again
 289   6                        SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period;
 290   6                        }
 291   5                     }
 292   4                  else
 293   4                     {
 294   5                     // Not yet ready to run: just decrement the delay 
 295   5                     SCH_tasks_G[Index].Delay -= 1;
 296   5                     }
 297   4                  }         
 298   3               }
 299   2            RI = 0;  // Reset the RI flag   
 300   2            }
 301   1         else
 302   1            {
 303   2            // ISR call was triggered by TI flag, after last character was sent
C51 COMPILER V6.10  SCU_BS                                                                 04/19/2001 14:04:06 PAGE 6   

 304   2      
 305   2            // RS485_Tx_Enable flag is reset here 
 306   2            RS485_Tx_Enable = 0;
 307   2      
 308   2            // Must clear the TI flag
 309   2            TI = 0;
 310   2            }
 311   1         }   
 312          
 313          /*------------------------------------------------------------------*-
 314          
 315            SCU_B_SLAVE_Send_Ack_Message_To_Master()
 316          
 317            Slave must send and 'Acknowledge' message to the master, after
 318            tick messages are received.  NOTE: Only tick messages specifically
 319            addressed to this slave should be acknowledged.
 320          
 321            The acknowledge message serves two purposes:
 322            [1] It confirms to the master that this slave is alive & well.
 323            [2] It provides a means of sending data to the master and - hence
 324                - to other slaves.
 325          
 326            NOTE: Direct data transfer between slaves is NOT possible.
 327          
 328          -*------------------------------------------------------------------*/
 329          void SCU_B_SLAVE_Send_Ack_Message_To_Master(void)
 330             {
 331   1         // Enable the slave RS-485 hardware (Tx)
 332   1         // NOTE: This flag will be reset in the 'Update' ISR
 333   1         RS485_Tx_Enable = 1;
 334   1      
 335   1         // Sending one byte of data at a time, depending on index value
 336   1         // If Message_byte_G is 0, send first byte (the slave ID)
 337   1         if (Message_byte_G == 0)
 338   1            {
 339   2            TI = 0;                                            
 340   2            TB8 = 1;  // Set 'Command' bit
 341   2            SBUF = SLAVE_ID;
 342   2            }
 343   1         else
 344   1            {
 345   2            // Message_byte_G is 1, send the data byte 
 346   2            TI = 0;
 347   2            TB8 = 0;
 348   2            SBUF = Ack_message_data_G;
 349   2            }
 350   1       
 351   1         // Data sent - return    
 352   1         }
 353          
 354          /*------------------------------------------------------------------*-
 355          
 356            SCU_B_SLAVE_Process_Tick_Message()
 357          
 358            The ticks messages are crucial to the operation of this shared-clock
 359            scheduler: the arrival of a tick message (at regular intervals) 
 360            invokes the 'Update' ISR, that drives the scheduler.
 361          
 362            The tick messages themselves may contain data.  These data are 
 363            extracted in this function. 
 364          
 365          -*------------------------------------------------------------------*/
C51 COMPILER V6.10  SCU_BS                                                                 04/19/2001 14:04:06 PAGE 7   

 366          tByte SCU_B_SLAVE_Process_Tick_Message(void)
 367             {
 368   1         tByte Data;
 369   1      
 370   1         // Try to get data byte
 371   1         if (RI == 0)
 372   1            {
 373   2            // No data - something is wrong
 374   2      
 375   2            // Set the error flag bit
 376   2            Network_error_pin = NETWORK_ERROR;
 377   2      
 378   2            // Return slave ID 0
 379   2            return 0x00;
 380   2            } 
 381   1       
 382   1         // There *ARE* data available
 383   1         Data = (tByte) SBUF;
 384   1         RI = 0;  // Clear RI flag
 385   1      
 386   1         // What we do with this message depends if it a first or second byte
 387   1         if (Message_byte_G == 0)
 388   1            {
 389   2            // This is (should be) an ID byte
 390   2            Message_ID_G = Data;   
 391   2      
 392   2            if (RB8 == 0)
 393   2               {
 394   3               Message_ID_G = 0;  // Command bit should be set
 395   3               }
 396   2            }
 397   1         else
 398   1            {
 399   2            // This is (should be) a data byte
 400   2            // - Command bit should not be set
 401   2            if ((Message_ID_G == SLAVE_ID) && (RB8 == 0))
 402   2               {
 403   3               Tick_message_data_G = Data;
 404   3               }
 405   2            else
 406   2               {
 407   3               // Something is wrong - set Message_ID to 0
 408   3               Message_ID_G = 0; 
 409   3      
 410   3               // Set the error flag bit
 411   3               Network_error_pin = NETWORK_ERROR;
 412   3               }
 413   2            }
 414   1      
 415   1         return Message_ID_G;
 416   1         }
 417          
 418          
 419          /*------------------------------------------------------------------*-
 420          
 421            SCU_B_SLAVE_Watchdog_Init()
 422          
 423            This function sets up the watchdog timer.
 424          
 425            If the Master fails (or other error develop), 
 426            no tick messages will arrive, and the scheduler
 427            will stop.  
C51 COMPILER V6.10  SCU_BS                                                                 04/19/2001 14:04:06 PAGE 8   

 428          
 429            To detect this situation, we have a (hardware) watchdog
 430            running in the slave.  This watchdog - which should be set to
 431            overflow at around 100ms - is used to set the system into a
 432            known (safe) state.  The slave will then wait (indefinitely)
 433            for the problem to be resolved.
 434          
 435            NOTE: The slave will not be generating Ack messages in these 
 436            circumstances.  The Master (if running) will therefore be aware
 437            that there is a problem.  
 438          
 439          -*------------------------------------------------------------------*/
 440          void SCU_B_SLAVE_Watchdog_Init(void)   
 441             {
 442   1         // INIT NOT REQUIRED FOR 1232 EXTERNAL WATCHDOG
 443   1         // - May be required wwith different watchdog hardware
 444   1         //
 445   1         // Edit as required
 446   1         }
 447          
 448          
 449          /*------------------------------------------------------------------*-
 450          
 451            SCU_B_SLAVE_Watchdog_Refresh()
 452          
 453            Feed the external (1232) watchdog.
 454          
 455            Timeout is between ~60 and 250 ms (hardware dependent)
 456          
 457            Assumes external 1232 watchdog
 458          
 459          -*------------------------------------------------------------------*/
 460          void SCU_B_SLAVE_Watchdog_Refresh(void) reentrant
 461             {
 462   1         // Change the state of the watchdog pin
 463   1         if (WATCHDOG_state_G == 1)
 464   1            {
 465   2            WATCHDOG_state_G = 0;
 466   2            WATCHDOG_pin = 0;
 467   2            }
 468   1         else
 469   1            {
 470   2            WATCHDOG_state_G = 1;
 471   2            WATCHDOG_pin = 1;
 472   2            } 
 473   1         }    
 474          
 475          /*------------------------------------------------------------------*-
 476          
 477            SCU_B_SLAVE_Enter_Safe_State()
 478          
 479            This is the state enterted by the system when:
 480            (1) The node is powered up or reset
 481            (2) The Master node fails, and no working backup is available
 482            (3) The network has an error
 483            (4) Tick messages are delayed for any other reason
 484          
 485            Try to ensure that the system is in a 'safe' state in these 
 486            circumstances.
 487          
 488          -*------------------------------------------------------------------*/
 489          void SCU_B_SLAVE_Enter_Safe_State(void)
C51 COMPILER V6.10  SCU_BS                                                                 04/19/2001 14:04:06 PAGE 9   

 490             {
 491   1         // USER DEFINED - Edit as required
 492   1         TRAFFIC_LIGHTS_Display_Safe_Output();
 493   1         }  
 494          
 495          /*------------------------------------------------------------------*-
 496            ---- END OF FILE -------------------------------------------------
 497          -*------------------------------------------------------------------*/
 498          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    427    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      3       5
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      2       1
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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