1. eMBErrorCode is an enumeration type variable, representing an error code, with a total of 8 error codes. The most commonly used one is MB_ENOERR, which means no error.
2. The eMBMode enumeration type variable represents the working mode of the device, which are MB_RTU, MB_ASCII and MB_TCP.
3. The eMBEventType enumeration type variable defines the event type, which are EV_READY, indicating that the Startup is complete; EV_FRAME_RECEIVED, indicating that the frame is received; EV_EXECUTE, indicating that the function is executed; and EV_FRAME_SENT, indicating that the frame has been sent.
4. The eMBParity enumeration type variable represents the parity options, namely MB_PAR_NONE no parity, MB_PAR_ODD odd parity, and MB_PAR_EVEN even parity.
5. The static variable ucMBAddress in the mb.c file stores the device address. This variable is initialized in the eMBInit function.
6. In C51Modbus, the source code in the freeModbus library is changed. For example, try not to use function pointers, but directly call related functions, and determine which function to call based on the working mode in eMBCurrentMode. Add reentrant before some function declarations in the freeModbus library, which is a keyword unique to the Keil compiler. One disadvantage of this is that it cannot dynamically bind functions, which causes the library code to lose portability. This is due to the special nature of the C51 compiler being incompatible with the ANSI standard.
7. The ENTER_CRITICAL_SECTION() and EXIT_CRITICAL_SECTION() macros actually turn off and on global interrupts.
8. Functions with the xMBPort prefix belong to the port layer, which means they are independent of the ModBus protocol stack.
9. The first letter of the function name in the freeModbus library indicates the return value type, for example, e indicates the return type of enum; v indicates void without return value; x indicates BOOL Boolean type. Note that this rule is not always true, but the main functions basically comply with this rule. MB after the first letter indicates that it is a function belonging to the ModBus protocol stack.
10. The macro #define F_MCU in the port.h file defines the operating frequency of the microcontroller. Its value is needed to calculate the reload value of the Uart0 timer and the Tick timer.
11. In the main function of the program, the method of using the protocol stack is:
eStatus = eMBInit( MB_RTU, 0x0A, 0, 9600, MB_PAR_EVEN );
/* Enable the Modbus Protocol Stack. */
eStatus = eMBEnable() ;
for( ;; )
{
( void )eMBPoll() );
…
}
12. In the xMBPortSerialInit function of the port layer, Uart0 and the timer it uses need to be configured according to the incoming baud rate, parity, and data bit length settings.
13. Configure the receive and transmit enable in the vMBPortSerialEnable function of the port layer. Since there is only the receive enable control bit REN0 in the register SCON0 of the microcontroller, but no transmit enable control bit, a TxEnable variable is defined in the portserial.c file to indicate the transmit enable state. If both receive and transmit are disabled, the Uart0 interrupt must be disabled, that is, ES0 = 0.
14. The variable usTimerT35_50us in the eMBRTUInit function represents the number of ticks that T35 times out if a tick is performed every 50us. This formula is very important:
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
The function xMBPortTimersInit takes the variable usTimerT35_50us as the input parameter to set the T35 timeout timer.
15. Two state variables are defined in the mbrtu.c file. One is the receiving state variable eRcvState, which is the eMBRcvState enumeration type and has 4 states. After enabling the ModBus protocol stack, it is assigned STATE_RX_INIT, the initial state; the other is the sending state variable eSndState, which is the eMBSndState enumeration type and has two states. It is initialized to the sending idle state, namely STATE_TX_IDLE.
16. The eMBState state variable in the mb.c file is an enumeration type, representing the working state of the device. There are three states, namely "uninitialized", "enabled" and "disabled". After calling the eMBInit function, the eMBEnable function must be called to enable the ModBus protocol stack, in which the eMBState state variable is changed from "uninitialized state" to "enabled state", and then the serial port is enabled and the T35 timer is turned on.
17. If the T35 timer times out and generates an interrupt, the xMBRTUTimerT35Expired function is called, which contains a state machine switch. It sends an event notification through xMBPortEventPost according to the current receiving state, then turns off the T35 timer and sets the current receiving state to STATE_RX_IDLE.
18. The eMBException enumeration variable indicates the type of Exception. There are 10 types of Exception, which are defined in the ModBus protocol.
19. In eMBPoll(), first get the event through the xMBPortEventGet function. If there is no event, exit. If there is an event, process it according to the event type. EV_READY is sent by the xMBRTUTimerT35Expired function after the protocol stack is initialized, indicating that the startup is complete; EV_FRAME_RECEIVED is sent by the xMBRTUTimerT35Expired function after T35 times out, indicating that a frame has been received and needs to be framed, calling the eMBRTUReceive function; EV_EXECUTE is the last step in the process of processing EV_FRAME_RECEIVED. If the address of this frame matches the local address, an EV_EXECUTE event is issued for application layer processing.
20. In the eMBRTUReceive function, first check whether the frame size meets the requirements, and then perform CRC check. The prototype of this function is:
eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
The first parameter is to return the address in the frame, that is, the first byte in the frame; the second parameter passed in will be used as an array in the future, so the pointer type is used; the third parameter represents the length of the PDU, that is, the length of the frame excluding the address byte and CRC check byte.
21. In eMBPoll(), process the EV_EXECUTE event. First, extract the FunctionCode from the PDU, and then find the corresponding processing function according to the FunctionCode. The xMBFunctionHandler structure type variable xFuncHandlers defines the processing function pxHandler corresponding to each FunctionCode. The first parameter ucMBFrame of the function is the storage address of the PDU, and the second parameter usLength returns the length of the PDU. If the frame is not a broadcast frame, the device needs to send a reply. If an error occurs before, an error report frame must be replied.
22. In Keil, the program needs to be compiled using large mode, otherwise error c249: 'data': segment too large will occur.
23. If the baud rate is 9600, then t3.5 = (11 * 3.5) / 9600 = 4.01 ms. The 8-bit mode Timer cannot be used because the longest timeout period of the 11.0590MHz main frequency after the maximum 48 division is 1.11ms, which cannot meet the timeout requirement of T35.
Previous article:51 MCU Assembly: Multi-channel Delay Switch
Next article:Working principle of microcontroller reset circuit
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- Free shape LED ball
- Internet of Things vs Industrial Internet of Things: 10 Differences That Matter
- The stm32f030c8t6 chip crashes as long as it fetches data during an interrupt
- 【Arduino】168 sensor module series experiments (219) --- INMP441 omnidirectional microphone
- Technology Live: LPC55S69, designed to prevent hackers, helps you build secure edge nodes at low cost (share and win 20E coins)
- The upcoming nrf52840 metro express development board
- Electronic Instrumentation and Electrical Measurement
- 6 Task Scheduling
- [RVB2601 Creative Application Development] + A Preliminary Study on Connecting to Alibaba Cloud
- Analog Circuits Syllabus