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.

#include "pwm.h"
void PWM_GPIO_Init( void )
{
    PC_DDR |= ( 1 << 6 );  //PC6 push-pull output
    PC_CR1 |= ( 1 << 6 );
}
// Timer 1 is initialized
void TIM1_Init( void )
{
    PWM_GPIO_Init();
    TIM1_CR1 = (1<<7);                        // Automatic preloading is allowed
    
    TIM1_CCMR1 = (6<<4) | (1<<3) | (1<<2);      //TIM1 CH1 PWM1 Output Comparison 1 Preloaded Enable Output Comparison 1 Fast enable
    //TIM1_CCER1 |= 0x01; //CC1 indicates the output high level is valid
    TIM1_CCER1 |= (1<<0) | (1<<2);            CC1 indicates the output OC1 high level active Enable OC1N OC1N High level active
    TIM1_PSCRH = 0x00;                      // preload frequency 0
    TIM1_PSCRL = 0x00;                      //16M
    
    TIM1_ARRH = FRE >> 8;                     // Set the auto reloading value to 8 bits higher
    TIM1_ARRL = FRE;                          // Set the auto reload value to 8 bits lower
  
    TIM1_BKR |= (1<<7) | (1<<4);                 // Brake register enable OC1 output timer not working output invalid level open brake input brake input low level valid
    TIM1_DTR = 0x18;                          // Dead zone time 24*125ns=3000ns
    TIM1_EGR = (1<<0);                        // Generate an update event
    TIM1_CR1 |= 0x01;                           // Enable the counter
}
//TIM1 CH1 PC6
void TIM1_CH1_OUT( unsigned int DC )
{
    TIM1_CCR1H = DC >> 8;   // Capture the comparison register 8 bits higher
    TIM1_CCR1L = DC;        // Capture the lower 8-bit duty cycle of the compare register
}
Copy the code

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.

#include "iostm8s103F3.h"
#include "led.h"
#include "pwm.h"

void SysClkInit( void )
{
    CLK_SWR = 0xe1;       //HSI 16MHz CPU clock frequency of the primary clock source
    CLK_CKDIVR = 0x00;    //CPU clock is 0, system clock is 0
}
// The PB5 port is set as the input port as the brake control pin
void BK_GPIO_Init( void )
{
    PB_DDR &= ~( 1 << 5 );       //PB5 input mode
    PB_CR1 |= ( 1 << 5 );        //PB5 is output with a pull resistor
}
/* PC6 is TIM1_CH1 output channel. PC3 is TIM1_CH1N output channel. PB5 is brake control port TIM1_CH1 PC3 TIM1_CH1N PB5 TIM1_BKIN Function */
void main( void )
{
    SysClkInit();
    __asm( "sim" );                       // Disable interrupts
    BK_GPIO_Init();                       // Brake control input
    TIM1_Init();
    __asm( "rim" );                       // Enable interrupts
    delay_ms( 500 );
    TIM1_CH1_OUT(FRE/2);                  // Set duty cycle to 50%
    while( 1 )
    {
        LED = 0;                         / / the LED = 0 and LED =! LED execution efficiency varies greatly
        __asm( "nop" );                  
        LED = 1;
        __asm( "nop"); }}Copy the code

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.