STM8 SINGLE chip MICROCOMPUTER PWM brake function
This is the 31st day of my participation in the August Text Challenge.More challenges in August
When using STM8 single chip microcomputer to output PWM wave, there is a very practical function is the brake function. This braking function acts as an emergency brake button in the device. Such as at the time of output PWM drive motor, suddenly appeared system abnormality, should immediately stop the output of the PWM waveform, if use the software to control the PWM output stop response time will be longer, the brake function is equivalent to an interrupt, once triggered, immediately stop the PWM waveform output, response speed is very fast.
About the braking function, the official information is as follows:
The following direct use of code to demonstrate how to output the PWM waveform when using the brake function.
Enable the MOE and BKE bits in the TIM1_BKR register at initialization
MOE bit is used to enable the OUTPUT of PWM waveform, and BKE bit is used to open the brake input function. Brake polarity is the default, i.e. active low. When the brake signal is high voltage PWM output normally, when the brake polarity is low voltage PWM output stops. The PWM waveform is output using TIM1_CH1 and TIM1_CH1N complementary channels.
Then we need to initialize the IO port of the brake. The brake function uses PB5 port, which is set as the input port. The PB5 port is pulled to a high level by an external pull-up resistor. When an anomaly occurs, the PB5 port is pulled to a low level, producing a brake signal, and then the PWM signal stops output.
Here, we should note that the PWM complementary output function and brake function used in the code are the remapping function of the MCU, not the default function. So be sure to turn on the remapping function for these three pins when writing code. Otherwise, there will be no PWM waveform output and the braking function will not work.
To enable the remapping function, do as follows:
Use the STVP software to open the binary to be burned, and then select the OPTION BYTE OPTION in the middle of the software.
AFR0-- AFR7 bit is used to set the pin remapping function. By default, the pin function is shown in the following options. Click the options here and select the function you want to set in the drop-down list.
Click on the options after AFR7, and a list of functions you want to remap will automatically pop up. Because the code is using PWM complementary output function, so here to select the above option. Remap port PC3 to TIM1_CH1N and port PC4 to TIM1_CH2N.
Do the same for the other options
Remap port PC3 to TIM1_CH1N, port PC6 to TIM1_CH1, and port PB5 to TIM1_BKIN. That is to turn on PWM complementary output function, and take PB5 port as brake function.
After the remapping function is set, you can click the download button to download the code to the microcontroller. At this time through oscilloscope can see in PC6 and PC3 port complementary PWM waveform output, then if give PB5 port a low level, PWM output will stop immediately. When the LEVEL of PB5 port returns to high level, the PWM output is still off. As long as THE PB5 port appears a low level, PWM output will be off forever. Want to restore PWM output, you need to restart the system, this and the equipment emergency stop button use function is the same.