A dynamic time-multiplexing method for timer resources in an embedded system

By dynamically time-division multiplexing timer resources, the problem of encoder and servo motor resource conflicts in embedded systems is solved, achieving embedded system resource optimization without increasing hardware costs or reducing control accuracy.

CN122632689APending Publication Date: 2026-08-25GUILIN UNIV OF ELECTRONIC TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610763486.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-05-29
Publication Date
2026-08-25

AI Technical Summary

Technical Problem

In embedded control systems, a single timer resource is insufficient to simultaneously support encoder signal acquisition and servo PWM output. Existing technologies increase hardware costs or consume CPU resources, leading to reduced system complexity and real-time performance.

Method used

A dynamic time-division multiplexing method is adopted, which enables dynamic switching of encoder and servo functions through software configuration of timer resources. This includes status flag detection, saving and restoring of count values ​​and configuration registers, thus avoiding the need for additional hardware.

Benefits of technology

It achieves timer resource reuse without increasing hardware costs, maintains motor control precision and servo motor action accuracy, and has a mode switching time of less than 1ms, meeting real-time requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure FT_1
    Figure FT_1
  • Figure FT_2
    Figure FT_2
  • Figure FT_3
    Figure FT_3
Patent Text Reader

Abstract

The application discloses a dynamic time-sharing multiplexing method for timer resources in an embedded system and belongs to the technical field of embedded control. The application aims to solve the resource conflict problem that a single timer cannot simultaneously support encoder signal collection and rudder PWM output. The method comprises the following steps: configuring the timer as a first working mode (an encoder interface mode) during system initialization; detecting a second peripheral function (a rudder control) request in real time; when the request is triggered, closing an interrupt and saving a timer count value and a register state; reconfiguring the timer as a second working mode (a PWM output mode) to drive the rudder to perform an action; and restoring the timer to the first working mode and restoring all states after the action is completed. The application does not need to increase hardware cost, the mode switching time is less than 1 ms, the encoder data error is less than 1 pulse, the rudder angle error is less than ±1°, and the application can be widely applied to embedded systems, such as mobile robots and mechanical arms, which need to simultaneously use an encoder and a rudder.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of embedded control technology, and in particular to a dynamic time-division multiplexing method for solving the resource conflict problem that a single timer cannot simultaneously support encoder signal acquisition and servo motor PWM output. Background Technology

[0002] In embedded control systems, timers are among the most commonly used peripherals, widely applied in pulse counting, PWM output, and timer interrupts. In mechatronic systems such as mobile robots and robotic arms, it is often necessary to simultaneously use encoders to collect motor speed and displacement information, and to use PWM signals to control servo motor movements.

[0003] However, low-cost microcontrollers have limited timer resources, and when the system needs to control multiple motors and servos, insufficient timer resources often occur. In existing technologies, there are two main methods to solve this problem: one is to add an extra hardware timer chip, which increases system cost and circuit complexity; the other is to use software to simulate PWM output, which consumes a large amount of CPU resources and reduces system real-time performance and control accuracy.

[0004] In particular, certain applications (such as material handling robot systems based on STM32F103RCT6) require multiple timers for motor PWM control, encoder acquisition, and servo control, but this chip only provides 8 timers, which is severely insufficient. Therefore, there is an urgent need for a timer resource reuse method that does not increase hardware costs or reduce control accuracy. Summary of the Invention

[0005] The purpose of this invention is to overcome the shortcomings of the prior art and provide a dynamic time-division multiplexing method for timer resources in embedded systems, which solves the resource conflict problem that a single timer cannot simultaneously support the functions of the first peripheral device and the second peripheral device without increasing hardware costs.

[0006] This invention is achieved through the following technical solution: a method for dynamic time-division multiplexing of timer resources in an embedded system, comprising the following steps:

[0007] S1: System initialization, configure the target timer to the first working mode for implementing the first peripheral function, and initialize the status flag bit used to indicate the second peripheral function request;

[0008] S2: Real-time detection of the status flag bit to determine whether a control request for the second peripheral function is triggered;

[0009] S3: When the control request is detected, disable the interrupt of the target timer and save the current count value and current configuration register state of the target timer;

[0010] S4: Reconfigure the target timer to a second operating mode for implementing the second peripheral function, and execute the second peripheral function;

[0011] S5: After the second peripheral function is completed, the second working mode is turned off, the count value and configuration register state saved in step S3 are restored, the target timer is returned to the first working mode, and its interrupt is re-enabled;

[0012] S6: Continue executing the first peripheral function and wait for the next control request.

[0013] Furthermore, the first peripheral device functions as encoder signal acquisition, and the second peripheral device functions as servo PWM output. The first operating mode described in this invention is, in a specific embodiment, an encoder interface mode; the second operating mode is, in a specific embodiment, a PWM output mode.

[0014] Furthermore, the encoder interface mode described in step S1 is the T1+T2 quadrature decoding mode, which is used to acquire the pulse signal of the AB phase incremental Hall encoder and calculate the motor speed and displacement.

[0015] Furthermore, the status flags mentioned in step S3 include a motor running flag, a servo motor action flag, and a timer mode flag, wherein: the motor running flag indicates that when the motor is running, the timer needs to work in encoder mode; the servo motor action flag indicates that when a servo motor control task needs to be performed, the timer needs to switch to PWM mode; the timer mode flag is used to record the current working mode of the timer, and has a first state and a second state, wherein the first state indicates encoder mode and the second state indicates PWM mode.

[0016] Furthermore, the step S3 of saving the timer count value and configuration register status specifically includes: reading the current timer count value and storing it in a global variable; and saving the values ​​of the timer's auto-reload register, prescaler register, control register, and interrupt enable register.

[0017] Further, the servo control parameters mentioned in step S4 include servo number, target angle, and action delay. The mapping relationship between the target angle and the PWM duty cycle is as follows: 0° corresponds to the first pulse width, 180° corresponds to the second pulse width, and the PWM period is a predetermined period. In a specific embodiment, the first pulse width is 500μs, the second pulse width is 2500μs, and the predetermined period is 20ms.

[0018] Furthermore, in step S4, after configuring the timer to PWM output mode, a predetermined delay (specifically 10-20ms) is added. This delay is used to wait for the servo power supply and PWM signal to stabilize and is not part of the mode switching time. The mode switching time (the software configuration time in steps S3+S5) is less than 1ms.

[0019] Furthermore, the restoration of the timer count value and encoder interface mode configuration in step S5 specifically includes: rewriting the saved configuration register value into the corresponding timer register; writing the saved count value into the timer count register; clearing the timer interrupt flag and re-enabling the timer interrupt.

[0020] Furthermore, the method is applied to STM32 series microcontrollers. In a specific embodiment, the timer is timer 4 of STM32F103RCT6, with channels 1 and 2 used to connect to the encoder AB phase, and channels 3 and 4 used to connect to the PWM signal input terminals of the two servos.

[0021] Furthermore, the condition for triggering the servo control request in step S2 is that the system detects a specific task execution command, and the motor operation flag indicates that the motor is in a stopped state.

[0022] Compared with existing technologies, this invention has the following advantages: First, it achieves time-division multiplexing of timer resources solely through software methods, eliminating the need for additional hardware timer chips and reducing system cost and circuit complexity. Second, it completely saves and restores the timer state during mode switching, achieving near-lossless switching (encoder data error less than 1 pulse) for peripherals like encoders that require continuous, uninterrupted timing, ensuring that motor control accuracy is unaffected. Third, the servo control uses hardware PWM output, avoiding the problem of software-simulated PWM consuming CPU resources and ensuring the accuracy and stability of servo movements. Fourth, the software configuration time for mode switching is less than 1ms, having minimal impact on the overall system real-time performance and fully meeting the requirements of real-time control systems such as mobile robots. Fifth, this method can be widely applied to all embedded systems that require simultaneous use of encoders and servos, especially low-cost control systems based on STM32 series microcontrollers. Attached Figure Description

[0023] Figure 1 This is an overall flowchart of the dynamic time-division multiplexing method described in this invention;

[0024] Figure 2 This is a sequence diagram of the timer mode switching in this invention;

[0025] Figure 3 Hardware block diagram of the material handling robot system for applying the present invention; Detailed Implementation

[0026] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described below with reference to the accompanying drawings and embodiments. It should be noted that the following detailed descriptions are exemplary and intended to provide further explanation of the invention.

[0027] Example 1.

[0028] This embodiment uses a material handling robot system based on STM32F103RCT6 as an example to illustrate the specific implementation of the present invention.

[0029] like Figure 3 As shown, the system includes an STM32F103RCT6 main control chip, four 520 DC motors with AB-phase incremental Hall encoders, four MG996R servo motors, an OpenMV vision module, and an HC-SR04 ultrasonic module. Channels 1 and 2 of Timer 4 are connected to the AB phase encoder of the fourth motor, while channels 3 and 4 are connected to the two servo motors of the robotic arm.

[0030] When the system is running, the motor needs to run continuously to drive the trolley to move. At this time, Timer 4 needs to work in encoder mode to collect motor speed and displacement information. When the trolley reaches the target position and needs to perform the action of grabbing or releasing materials, the motor stops running. At this time, Timer 4 needs to be switched to PWM mode to output servo motor control signal.

[0031] The specific steps of the dynamic time-division multiplexing method described in this invention are as follows:

[0032] S1: System initialization, configure Timer 4 to encoder interface mode, T1+T2 quadrature decoding, prescaler coefficient to 0, auto-reload value to 65535; enable Timer 4 update interrupt for encoder count overflow handling; initialize status flags: motor_run=1, servo_run=0, timer_mode=0; initialize servo control parameters: servo 1 initial angle 90°, servo 2 initial angle 0°.

[0033] S2: Status detection. The servo_run flag is monitored in real time during the main loop of the system. When the system enters the capture mode, all motors are stopped first, motor_run is set to 0, and then servo_run is set to 1.

[0034] S3: Mode switching preparation. When servo_run=1 and motor_run=0 are detected, immediately disable the Timer 4 interrupt; read the current count value of Timer 4 TIM4->CNT and store it in the global variable timer_cnt_save; save the configuration registers of Timer 4: TIM4->ARR, TIM4->PSC, TIM4->CR1, TIM4->DIER.

[0035] S4: PWM Mode Configuration and Output. Reconfigure Timer 4 to PWM output mode with a prescaler of 72 and an auto-reload value of 20000 (corresponding to a 20ms cycle). Configure Channels 3 and 4 to PWM output mode with high-level active polarity. Add a 15ms delay to wait for the timer hardware to stabilize and the servo power to be established. This 15ms delay is not part of the mode switching time but rather a portion of the servo action execution cycle. Calculate the PWM duty cycle based on the servo target angle: Servo 1 target angle 90°, corresponding to a pulse width of 1500μs, and a comparator register value of 1500; Servo 2 target angle 90°, corresponding to a pulse width of 1500μs, and a comparator register value of 1500. Write the calculated comparator register values ​​to TIM4->CCR3 and TIM4->CCR4 to output the PWM signal. Delay for 500ms to wait for the servo action to complete.

[0036] S5: Encoder mode resumes, PWM output is disabled, TIM4->CCR3 and TIM4->CCR4 are set to 0; the saved configuration register values ​​are rewritten to TIM4->ARR, TIM4->PSC, TIM4->CR1, and TIM4->DIER; the saved count value timer_cnt_save is written to TIM4->CNT; the timer 4 interrupt flag is cleared and the timer 4 interrupt is re-enabled; servo_run is set to 0, motor_run is set to 1, and timer_mode is set to 0.

[0037] S6: Continue operation, restore motor control, continue to perform encoder signal acquisition and trolley motion control tasks; wait for the next servo control request.

[0038] Example 2.

[0039] This embodiment verifies the effectiveness of the method of the present invention.

[0040] Test environment: Hardware platform: STM32F103RCT6 microcontroller, system clock 72MHz; Test object: Timer 4, encoder (500-line incremental encoder), servo motor (MG996R); Test tools: Tektronix TBS1102 oscilloscope, Saleae logic analyzer.

[0041] Test conditions: Motor speed 100rpm, servo motor performs a reciprocating motion from 90° to 180° every 1 second, test duration is 2 hours of continuous operation.

[0042] Test methods: Mode switching time: Before step S3 begins and after step S5 ends, a square wave is generated by GPIO toggling. The duration of the high level (i.e., the total software configuration time) is measured using a logic analyzer. Encoder error: Encoder pulses are synchronously acquired using an independent hardware counter (Timer 3), and the difference between the two counters before and after the switch is compared. Servo angle error: The actual angle of the servo output shaft is measured using an angle sensor and compared with the target angle.

[0043] Test results: Software mode switching time, average 0.8ms, maximum 1.2ms (measured 1000 times); encoder data error, ≤1 pulse (out of 1000 switching times, 995 errors were 0, and 5 errors were 1 pulse); servo angle error: ≤±1°; system stability: no faults after 2 hours of continuous operation.

[0044] The above data verifies the feasibility and superiority of the present invention. The impact of mode switching on encoder acquisition and servo control accuracy is negligible, fully meeting the requirements of real-time embedded systems.

[0045] The method of this invention is simple, implemented only through software, and does not increase hardware costs. It can be widely used in embedded control systems such as mobile robots, robotic arms, and drones that require the simultaneous use of encoders and servo motors, and is especially suitable for low-cost microcontroller platforms with limited resources.

[0046] Furthermore, although the embodiments of the present invention are illustrated using the STM32 series microcontroller as an example, those skilled in the art should understand that the method provided by the present invention is also applicable to other microcontrollers or DSP chips with timer peripherals (such as related chips from brands like GD32, NXP, and TI), and the principles of saving and restoring their status registers are the same, all of which fall within the protection scope of the present invention.

[0047] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for dynamic time-division multiplexing of timer resources in an embedded system, characterized in that, To resolve the resource conflict issue where a single timer cannot simultaneously support the functions of a first peripheral and a second peripheral, the following steps are included: S1: System initialization, configure the target timer to the first working mode for implementing the first peripheral function, and initialize the status flag bit used to indicate the second peripheral function request; S2: Real-time detection of the status flag bit to determine whether a control request for the second peripheral function is triggered; S3: When the control request is detected, disable the interrupt of the target timer and save the current count value and current configuration register state of the target timer; S4: Reconfigure the target timer to a second operating mode for implementing the second peripheral function, and execute the second peripheral function; S5: After the second peripheral function is completed, the second working mode is turned off, the count value and configuration register state saved in step S3 are restored, the target timer is returned to the first working mode, and its interrupt is re-enabled; S6: Continue executing the first peripheral function and wait for the next control request.

2. The dynamic time-division multiplexing method according to claim 1, characterized in that, The first peripheral device is for encoder signal acquisition, and the second peripheral device is for servo motor PWM output.

3. The dynamic time-division multiplexing method according to claim 2, characterized in that, The encoder interface mode described in step S1 is the T1+T2 quadrature decoding mode, which is used to acquire the pulse signal of the AB phase incremental Hall encoder and calculate the motor speed and displacement.

4. The dynamic time-division multiplexing method according to claim 1, characterized in that, The status flags mentioned in step S3 include a motor running flag, a servo motor action flag, and a timer mode flag, wherein: When the motor running flag indicates that the motor is running, the timer must be in encoder mode. When the servo action flag indicates that a servo control task needs to be performed, the timer needs to be switched to PWM mode; The timer mode flag is used to record the current operating mode of the timer. It has a first state and a second state, where the first state indicates encoder mode and the second state indicates PWM mode.

5. The dynamic time-division multiplexing method according to claim 1, characterized in that, Saving the timer count value and configuration register state in step S3 specifically includes: Read the current timer count value and store it in a global variable; Save the values ​​of the timer's auto-reload register, prescaler register, control register, and interrupt enable register.

6. The dynamic time-division multiplexing method according to claim 2, characterized in that, The servo control parameters mentioned in step S4 include servo number, target angle and action delay. The mapping relationship between the target angle and PWM duty cycle is as follows: 0° corresponds to the first pulse width, 180° corresponds to the second pulse width, and the PWM period is a predetermined period.

7. The dynamic time-division multiplexing method according to claim 1, characterized in that, In step S4, after reconfiguring the target timer to the second working mode and before executing the second peripheral function, a delay of a preset duration is inserted.

8. The dynamic time-division multiplexing method according to claim 1, characterized in that, The step S5, restoring the timer count value and configuration register state, specifically includes: Rewrite the saved configuration register value into the corresponding timer register; Write the saved count value into the timer count register; Clear the timer interrupt flag and re-enable the timer interrupt.

9. The dynamic time-division multiplexing method according to claim 1, characterized in that, The method is applied to STM32 series microcontrollers. The timer is timer 4 of STM32F103RCT6. Its channels 1 and 2 are used to connect the encoder AB phase, and channels 3 and 4 are used to connect the PWM signal input terminals of the two servo motors.

10. The dynamic time-division multiplexing method according to claim 4, characterized in that, The conditions for triggering the servo control request in step S2 are: the system detects a specific task execution command, and the motor operation flag indicates that the motor is in a stopped state.