GPS synchronous AC sampling method based on MCU timer
By implementing the GPS synchronous AC sampling method on the MCU timer, the development difficulty and hardware cost problems caused by the simultaneous participation of FPGA and MCU in the prior art are solved, and high-precision nanosecond-level synchronous sampling is achieved.
Patent Information
- Application Number
- CN202411948691.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-04-22
AI Technical Summary
When the prior art realizes synchronous AC sampling of smart devices for power system microcomputer measurement and control protection, it requires FPGA and MCU to participate simultaneously, which increases the development difficulty and hardware cost.
The GPS synchronous AC sampling method based on the MCU timer is adopted. The PPS second pulse signal and ADC conversion signal output by the GPS module are captured by two timers of the MCU, and the error is calculated and the PWM output frequency is adjusted to realize synchronization between the ADC conversion signal and the PPS second pulse signal.
High-precision synchronous and equal-spaced AC sampling at the nanosecond level is realized, reducing project development difficulty and equipment hardware costs, and synchronization error reaches nanosecond level.
Smart Images

Figure CN119861541A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of power grid operation safety assurance, and more specifically, to a GPS synchronous AC sampling method based on an MCU timer. Background Art
[0002] A large number of microcomputer-based measurement, control and protection intelligent devices are running in the power system. Synchronization technology is a factor that affects the quality of AC sampling. The development of synchronous AC sampling technology provides more possibilities and options for the measurement, control and protection algorithms of intelligent devices in the power system. The crystal oscillators of different devices are independent of each other, and there is a system clock error, which makes it difficult to synchronize the sampling of different devices.
[0003] At present, smart devices can achieve synchronous AC sampling through FPGA+MCU. FPGA synchronizes the system clock according to the GPS second pulse PPS input signal, so that the starting points of the two signals are at the same time, and then outputs the sampling conversion signal of the required frequency through frequency multiplication. MCU obtains the sampling data from FPGA through communication and processes the data.
[0004] However, this implementation requires both FPGA and MCU, which increases the difficulty of project development and the cost of equipment. Summary of the invention
[0005] The purpose of the present invention is to solve the problem of synchronous AC sampling of microcomputer measurement and control protection intelligent equipment in power system, and propose a GPS synchronous AC sampling method based on MCU timer. This method uses GPS second pulse PPS signal and only requires the participation of MCU to achieve nanosecond level high-precision synchronous equal-interval AC sampling, which reduces the difficulty of project development and the hardware cost of equipment.
[0006] The technical solution of the present invention is:
[0007] The present invention provides a GPS synchronous AC sampling method based on an MCU timer, comprising:
[0008] S1, start the first channel and the second channel of the first timer of the MCU; start the first channel and the second channel of the second timer of the MCU;
[0009] S2, using the first channel of the first timer to capture the PPS second pulse signal output by the GPS module, recording the whole second count value of the first timer, and generating a first timer event and sending it to the second channel of the second timer;
[0010] S3, the second channel of the second timer captures the first timer event and records the whole second count value of the second timer;
[0011] S4, the first channel of the second timer outputs the ADC conversion signal according to the preset sampling frequency, generates a second timer event and sends it to the second channel of the first timer;
[0012] S5. The second channel of the first timer captures the second timer event, obtains the error of the ADC conversion signal output time in full seconds according to the full second count value of the first timer and the full second count value of the second timer, and adjusts the PWM output frequency of the second timer according to the error, so that the ADC conversion signal output time in full seconds is synchronized with the GPS second pulse full second start time, that is, the standard full second time.
[0013] Further, the step of obtaining the error of the ADC conversion signal output time in full seconds includes:
[0014] Obtaining the whole second count value of the first timer and the whole second count value of the second timer;
[0015] The difference between the integer second count value of the second timer and the integer second count value of the first timer is calculated, and the difference is determined as the error of the integer second output time of the ADC conversion signal.
[0016] Further, adjusting the PWM output frequency of the second timer according to the error includes:
[0017] Determine whether the error is greater than a preset threshold, where the threshold is 1-3 timer clock pulse widths;
[0018] If yes, then according to the size of the error, the PWM output frequency of the second timer is adjusted to reduce the error between the full second output time of the ADC conversion signal and the full second start time of the GPS second pulse.
[0019] Furthermore, the synchronization error between the ADC conversion signal output time in full seconds and the GPS second pulse start time in full seconds is at the nanosecond level.
[0020] Furthermore, it also includes:
[0021] When the whole second count value of the first timer and the whole second count value of the second timer are maintained in a preset equilibrium state, stop adjusting the PWM output frequency of the second timer;
[0022] The preset equilibrium state is that the error of the output time of the ADC conversion signal in whole seconds calculated according to the whole second count value of the first timer and the whole second count value of the second timer is less than a preset threshold.
[0023] Furthermore, it also includes:
[0024] The sampled AC signal frequency is tracked. When the AC signal frequency changes, the PWM output frequency of the second timer outputs the ADC conversion signal according to the updated sampling frequency, and S5 is re-executed to perform error adjustment.
[0025] Beneficial effects of the present invention:
[0026] The method of the present invention uses two timers of the MCU to respectively input and capture the PPS second pulse signal and the ADC conversion signal output by the GPS module to obtain two whole second count values; the error between the whole second output moment of the ADC conversion signal and the whole second start moment of the PPS second pulse signal is calculated according to the two count values, and the output frequency of the second timer of the MCU is adjusted accordingly to achieve the synchronization of the ADC conversion signal and the PPS second pulse signal. The synchronization of the ADC whole second trigger conversion signal and the PPS whole second start time can reach the error level of nanoseconds; only the participation of the MCU is required to achieve nanosecond-level high-precision synchronous equal-interval AC sampling, which reduces the difficulty of project development and reduces the hardware cost of the equipment.
[0027] The present invention also controls the ADC sampling frequency through the PWM signal, and calculates the error and adjusts the output signal in real time in the interrupt program. The present invention realizes high-precision synchronization between the ADC sampling conversion signal and the GPS PPS signal, improves the synchronization accuracy of the sampled data, and is of great significance to data acquisition applications that require synchronous sampling.
[0028] Other features and advantages of the present invention will be described in detail in the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The above and other objects, features and advantages of the present invention will become more apparent through a more detailed description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, wherein like reference numerals generally represent like components throughout the exemplary embodiments of the present invention.
[0030] Figure 1 A flow chart of a GPS synchronous AC sampling method based on an MCU timer according to an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0031] The preferred embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the preferred embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited to the embodiments set forth herein.
[0032] Figure 1 A flow chart of a GPS synchronous AC sampling method based on an MCU timer according to an embodiment of the present invention is shown.
[0033] like Figure 1 As shown, the present invention provides a GPS synchronous AC sampling method based on an MCU timer, which may specifically include:
[0034] S1, obtaining the PPS pulse-per-second signal output by the GPS module, wherein the whole second starting time of the PPS pulse-per-second signal is the standard whole second time;
[0035] S2, inputting and capturing the PPS pulse-per-second signal through the first channel of the first timer of the MCU to obtain a first full-second count value and output a first timer event;
[0036] S3, capturing the first timer event through the second channel of the second timer of the MCU to obtain a second full second count value, and the full second output time of the ADC conversion signal is the full second time to be adjusted;
[0037] S4, outputting an ADC conversion signal and a second timer event through a first channel of a second timer of the MCU;
[0038] S5, capturing the input of the second timer event through the second channel of the first timer of the MCU, and obtaining the error between the full second output time of the ADC conversion signal and the full second start time of the PPS second pulse signal according to the first full second count value and the second full second count value;
[0039] S6. If the error is greater than a preset threshold, the PWM output frequency of the second timer of the MCU is adjusted according to the error so that the whole second output time of the ADC conversion signal is synchronized with the whole second start time of the PPS second pulse signal, and the threshold is 1-3 timer clock pulse widths;
[0040] S7, repeat the above steps until the first full second count value and the second full second count value are maintained in a preset equilibrium state, and then stop adjusting; the equilibrium state is that the error of the full second output time of the ADC conversion signal calculated according to the first full second count value and the second full second count value is less than the threshold value;
[0041] S8. When the frequency of the sampled AC signal changes, the PWM output frequency of the second timer outputs the ADC conversion signal according to the updated sampling frequency, and S6 is re-executed to perform error adjustment and repeat the error adjustment.
[0042] When implementing:
[0043] (1) MCU starts timer 1 channel 1.
[0044] (2) MCU starts timer 1 channel 2.
[0045] (3) MCU starts timer 2 channel 1.
[0046] (4) MCU starts timer 2 channel 2.
[0047] (5) Timer 1 channel 1 input captures the PPS pulse signal GPS_PPS output by the GPS module. When an input capture event occurs, it is a standard full second moment, and the full second count value TIM1_CCR_PPS of timer 1 is captured. The TIM1_CCR_PPS value is recorded in the interrupt processing function.
[0048] (6) Timer 2 channel 2 input captures the event of timer 1 channel 1. When an input capture event occurs, it is the time when the ADC conversion signal is output in full seconds, and the full second count value TIM2_CCR_PPS of timer 2 is captured. The TIM2_CCR_PPS value is recorded in the interrupt processing function.
[0049] (7) Timer 2 channel 1 implements PWM output and outputs ADC conversion signal ADC_CHG according to the sampling frequency.
[0050] (8) Timer 1 channel 2 input captures the timer 2 channel 1 event. When an input capture event occurs, an interrupt is generated. In the interrupt processing function, according to TIM1_CCR_PPS and TIM2_CCR_PPS recorded in the previous step, the error Err of the ADC_CHG full second output time is obtained. According to the error Err obtained, the PWM output frequency ADC_CHG full second output time of timer 2 is adjusted.
[0051] (9) After completing the first round of adjustment, continue the second round of adjustment according to the adjusted parameters. After multiple rounds of adjustment, the error between the ADC_CHG full second output time and the standard full second time can reach the nanosecond level. When TIM1_CCR_PPS and TIM2_CCR_PPS maintain a balanced state, stop the error adjustment. The error of the GPS PPS second pulse input signal is within 30 nanoseconds, and the clock pulse width of the MCU timer is selected at the nanosecond level. Through this solution, synchronous AC sampling between different devices can be achieved with an accuracy of nanoseconds.
[0052] The MCU crystal oscillator has its inherent error. During the operation of the smart device, as the running time goes by, the cumulative error of the timer count will gradually increase. When the cumulative error breaks the existing balance between TIM1_CCR_PPS and TIM2_CCR_PPS, repeat step 5) to eliminate this error.
[0053] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those skilled in the art without departing from the scope and spirit of the described embodiments.
Claims
1. A GPS synchronous AC sampling method based on MCU timer, characterized in that: include: S1, starting the first channel and the second channel of the first timer of the MCU; Start the first channel and the second channel of the second timer of the MCU; S2, using the first channel of the first timer to capture the PPS second pulse signal output by the GPS module, recording the whole second count value of the first timer, and generating a first timer event and sending it to the second channel of the second timer; S3, the second channel of the second timer captures the first timer event and records the whole second count value of the second timer; S4, the first channel of the second timer outputs the ADC conversion signal according to the preset sampling frequency, generates a second timer event and sends it to the second channel of the first timer; S5. The second channel of the first timer captures the second timer event, obtains the error of the ADC conversion signal output time in full seconds according to the full second count value of the first timer and the full second count value of the second timer, and adjusts the PWM output frequency of the second timer according to the error, so that the ADC conversion signal output time in full seconds is synchronized with the GPS second pulse full second start time, that is, the standard full second time.
2. The GPS synchronous AC sampling method based on MCU timer according to claim 1 is characterized in that: The step of obtaining the error of the ADC conversion signal output time in whole seconds includes: Obtaining the whole second count value of the first timer and the whole second count value of the second timer; The difference between the integer second count value of the second timer and the integer second count value of the first timer is calculated, and the difference is determined as the error of the integer second output time of the ADC conversion signal.
3. The GPS synchronous AC sampling method based on MCU timer according to claim 1 is characterized in that: Adjusting the PWM output frequency of the second timer according to the error includes: Determine whether the error is greater than a preset threshold, where the threshold is 1-3 timer clock pulse widths; If yes, then according to the size of the error, the PWM output frequency of the second timer is adjusted to reduce the error between the full second output time of the ADC conversion signal and the full second start time of the GPS second pulse.
4. The GPS synchronous AC sampling method based on MCU timer according to claim 3 is characterized in that: The synchronization error between the output time of the ADC conversion signal in full seconds and the start time of the GPS second pulse in full seconds is at the nanosecond level.
5. The GPS synchronous AC sampling method based on MCU timer according to claim 1 is characterized in that: Also includes: When the whole second count value of the first timer and the whole second count value of the second timer are maintained in a preset equilibrium state, stop adjusting the PWM output frequency of the second timer; The preset equilibrium state is that the error of the output time of the ADC conversion signal in whole seconds calculated according to the whole second count value of the first timer and the whole second count value of the second timer is less than a preset threshold.
6. The GPS synchronous AC sampling method based on MCU timer according to claim 1, characterized in that: Also includes: The sampled AC signal frequency is tracked. When the AC signal frequency changes, the PWM output frequency of the second timer outputs the ADC conversion signal according to the updated sampling frequency, and S5 is re-executed to perform error adjustment.