Signal sampling and PWM control using single-chip timer

The PWM control method is widely used in various control systems, but the adjustment of the pulse width is generally implemented by hardware. If the PWM controller is used or the PWM circuit [1] is added to the system, the cost is high, the response speed is slow, and there is a compatibility problem between the PWM controller and the system. In addition, the signal sampling in the control system is usually completed by the A/D converter. Therefore, when the detection accuracy is high, the conditioning circuit is complicated, and the number of bits of the A/D is high, so that the design system cost is high. .

This article takes the application of temperature control system as an example, introduces the timer TIme_A design in the new type of single chip MSP430F413 produced by Motorola, which can use the time quantity for temperature sampling and realize PWM adjustment. In order to achieve high-precision measurement and control of the control system with a small number of peripheral circuits, on the one hand, time-sampling is used, and 12-bit high precision is obtained in the case of eliminating one A/D; PWM adjustment is completely implemented in software within the interrupt to facilitate data communication and display. The system can solve the real-time online calculation and calculation accuracy problem of waveform generation in the interrupt, and can accurately and real-time calculate the pulse width at the set frequency.

1 single chip MSP430F413 and timer

MSP430 series MCU F413 has certain features in ultra-low power consumption and functional integration, which can greatly reduce the complexity of peripheral circuits. Its real-time processing capability and various peripheral modules make it applicable to multiple low power consumption. Field [2]. The general 16-bit timer TImer_A in the MSP430F413 has the following main function modules.

(1) A counter that continuously counts up to a predetermined value and returns 0.

(2) The software can select the clock source.

(3) 5 capture/compare registers, each with independent capture events.

(4) 5 output modules to support the need for pulse width modulation.

Each bit of the timer control register TACTL controls the configuration of the TImer_A and defines the basic operation of the 16-bit timer. The original clock source or the divided input clock source and four operating modes can be selected. There is also a clear function and an overflow interrupt control bit. The five capture/compare registers CCRx operate the same and are configured by their respective control registers, CCTLx.

2 Principle of time sampling and PWM control

Taking the temperature control system as an example, a method of implementing signal sampling and PWM control with a timer is introduced. The temperature control system includes a single chip microcomputer, a temperature measuring circuit, a load driving circuit, and other peripheral parts such as a power supply control, a low voltage detection, and a display circuit.

The main I/O ports used to measure and control temperature in the MSP430F413 are:

P1.0: Outputs a 50Hz square wave for generating a triangular wave.

P1.2: Drive temperature control actuator, 2kHz square wave PWM output.

P2.0: Pulse width capture.

2.1 Interrupt Settings of the MCU Port

The 50Hz square wave output, PWM output, and input capture of the temperature control system are all implemented by timed interrupts. These three interrupts are caused by the peripheral modules of the P0, P1, and P2 ports, respectively, and belong to the external maskable interrupt. During initialization, the three I/O ports are interrupted and set to the TIme_A control register TACTL, including the input signal divided by 2, and the auxiliary clock ACLK. When the capture/compare register is defined, TACTL is reassigned, the timer is started, and the continuous increment is started.

2.2 Pulse width capture to achieve temperature sampling

The temperature measuring circuit converts the temperature value into a voltage value, and the 50 Hz square wave generated by the single chip is transformed by the capacitor charging and discharging circuit to obtain a triangular wave of the same frequency, and the voltage value is cut by the triangular wave, thereby converting the temperature value into a pulse of the corresponding width and feeding the pulse into the single chip microcomputer. The waveform changes are shown in Figure 1.

Signal sampling and PWM control using single-chip timer

By setting the mode bits in CCTLx, the corresponding capture/compare register CCRx can be set to capture mode for precise positioning of time events. If the trigger edge of the selected pulse occurs on the selected input pin, the value of the timer count will be copied to CCRx. According to this principle, P2.0 is selected as the input pin, CCTL2 is set to capture mode, and the measured temperature value is converted into pulse by analog measurement circuit, P2.0 captures the falling edge of pulse, enters interrupt T2, and gets The number of pulses per unit time with the same temperature value is stored in CCR2 for further processing.

In this way, the system completes the analog-to-digital conversion without using the A/D converter. Because the clock accuracy of the single-chip microcomputer is high, and the amount of time is a very high relative precision, the temperature sampling of the time amount in the system can obtain 12-bit high precision, and the 50 Hz pulse can be used, which can greatly eliminate the power frequency interference. These provide the necessary conditions for precise temperature control.

2.3 PWM signal generation principle

The capture/compare registers CCR0 and CCR1 are defined as compare modes, and their output units OUT0 and OUT1 correspond to the microcontroller pins P1.0 (TA0) and P1.2 (TA1), respectively. After entering the compare mode, if the count value of the timer CCRx is equal to the value in the compare register x, the comparison signal EQUx is output to the output unit OUTx while the signal is set, reset or inverted according to the selected mode. Among them: set EQU0 to flip the OUT0 signal, the signal clock is synchronized with the timer clock, so that a 50Hz square wave signal can be obtained on the P1.0 pin; set the EQU1 output mode to PWM reset/set.

The output of the timer interrupt in the setting mode is shown in Figure 2. According to the set PWM reset/set mode, if the CCR1 counter overflows, EQU1 resets OUT1; if the CCR0 counter overflows, EQU0 sets OUT1. The duty cycle is changed by using the difference between CCR0 and CCR1 to count the starting point, thereby completing the PWM output on P1.2. The adjustment of the duty cycle of the system is achieved by changing the cardinality of CCR1. When the timer clock is 2MHz and the count value of CCR1 and CCR0 is 1 000, a PWM output frequency of 2 kHz can be obtained. The load driving circuit amplifies and filters the PWM signal outputted by the P1.2 pin of the single chip microcomputer to drive the high-power actuator.

Signal sampling and PWM control using single-chip timer

3 software design

3.1 System main program

In the main program, system initialization, timer initialization, temperature sample value reading, load driving and display are included. The system performs temperature value sampling and PWM output in the timed interrupt, and the duty cycle of the PWM output pulse is obtained by the PID algorithm. The main program flow chart of the system is shown in Figure 3.

3.2 PID pulse width adjustment

The modulation of the pulse width by the system is implemented by the PID algorithm. According to the principle of the algorithm, the system designs a set of PID algorithm which is completely realized by software, and completes the self-tuning of parameters in the control process. PID adjustment control process: the single-chip reads the actual temperature Tn in digital form, and then compares it with the set temperature Tg, and obtains the difference en=Tn-Tg. According to the positive and negative and the size of en, the PID formula is called, and the calculation is obtained. The output voltage Δun is consistent with the duty cycle, adjusting the temperature rise and fall, while finding the optimal condition and changing the PID parameters.

Signal sampling and PWM control using single-chip timer

The output of the incremental PID control algorithm [3]:

Signal sampling and PWM control using single-chip timer

The PID adjustment program is directly written into the microcontroller, and the base value of the counter CCR1 is changed according to the obtained value, thereby changing the duty ratio of the output pulse to achieve the purpose of adjusting the PWM.

3.3 Timed Interruption

The timing interrupt subroutine flow is shown in Figure 4. The crystal frequency used in the system is 2MHz. The function of the T0 interrupt is to obtain a square wave with a frequency of 50Hz and a duty ratio of 90% to generate a triangular wave and check whether there is data missing during one cycle. The T0 mode overflow is flipped to a high level, and the output comparison interval is 18 ms. Among them, CCR0 adds the PWM mode, which is the difference between CCR0 and CCR1 to generate the pulse width required for the output.

Signal sampling and PWM control using single-chip timer

The T1 interrupt handles the PWM output of the control port, and checks whether the data is repeatedly acquired in one cycle. The T1 output compares to generate a low level, and the output comparison interval is 20ms. The T2 interrupt captures the pulse width of the temperature measurement port to obtain the measured temperature value.

4 Conclusion

The method of using the timer Time_A in the MSP430F413 to perform temperature sampling and implementing PWM adjustment can be widely used in a single-chip microcomputer with port capture function. Compared with the traditional method, it not only simplifies the hardware structure of the measurement and control circuit, but also can easily establish a human-machine interface, and realize the adjustment of parameters by software, so that the control is more accurate, real-time and reliable. After experiments, the method was applied to the temperature control system to obtain the expected precise PWM adjustment waveform. This method can also be used in other microcontroller control systems.

Processor Chip

Shenzhen Kaixuanye Technology Co., Ltd. , https://www.iconlinekxys.com