This is the 20th day of my participation in the August Text Challenge.More challenges in August

Debugging STM8 MCU PWM function, often encounter a situation, is PWM initialization after the completion of the program downloaded to the MCU, there is no waveform output. Sometimes the code is downloaded from other debugging, but there is no waveform output on their own chip. At this time either scold the double wear code people cheat people, upload the code is wrong, or suspect their microcontroller is broken.

Now let’s analyze how to judge the case where the naming code is correct but there is no waveform output.

First look at the PWM initialization code

void TIM1_Init( void )
{
    TIM1_CCMR2 = 0x60;          //TIM1 CH2 output mode PWM1
    TIM1_CCER1 |= 0x10;         / / CC2 for the output
    TIM1_PSCRH = 0x00;
    TIM1_PSCRL = 0x03;          //16M/(1+3)=4 M
    
    TIM1_ARRH = 1000 >> 8;       // Set the auto reloading value to 8 bits higher
    TIM1_ARRL = 1000;            // Set the auto reload value to 8 bits lower
    
    TIM1_CCR2H = 500 >> 8;       // Capture the comparison register 8 bits higher
    TIM1_CCR2L = 500;            // Capture the lower 8-bit duty cycle of the compare register
    
    TIM1_BKR = 0x80;            // Brake register enable OC1 output timer not working output invalid level
    TIM1_CR1 |= 0x01;           // Allow timer interrupt
}
Copy the code

Set TIM1_CCMR2 to 0x60, that is, set channel 2 of timer 1 to PWM1 output mode. Next, set the TIM1_CCER1 register value to 0x10 and enable timer 1 channel 2 output. The TIM1_PSCR register is used to set the timer divider value, where the divider value is set to 3 and the clock frequency of the counter (F CK_CNT) is equal to F CK_PSC /(PSCR[15:0]+1). The timer frequency can be calculated according to the formula. The system frequency is 16M, so the timer frequency is 16 / (3+1) = 4M, that is, the working frequency of the timer is 4M. The TIM1_ARR register is used to set the automatic reloading value, that is, the output frequency of PWM, which is set to 1000 here. The working frequency of timer is 4M, and the automatic retransmission value is 1000, then the PWM output frequency is 4000000/1000 = 4000HZ. This means that the PWM output frequency is 4K, and the TIM1_CCR2 register is used to set the comparison value of channel 2, with which the counter is compared. When the counter value is greater than this value, the output level is flipped.

It can also be seen from official data that in PWM1 mode, when the counter value is less than TIM1_CCR, the active level is output, and when the counter value is greater than TIM1_CCR, the inactive level is output.

The effective level is set in TIM1_CCER1.

Is 0, the default is high level effective, so here when the counter is less than 500, output high level, when the counter value is greater than 500 output level.

Finally, the output is enabled by TIM1_BKR and the counter is turned on by TIM1_CR1. So in the timer 1 channel 2 pin can output PWM wave.

Hang the oscilloscope on the PC7 pin, observe the waveform, then find that there is no waveform output? Is there a problem with the initialization of the PWM? There is no problem with the initialization Settings. However, when using STM8 microcontroller, special attention should be paid to a point. Is the single-chip pin multiplexing function open.

In STM8 MCU there is an option byte, this option byte is specifically used to set the single-chip IO pin multiplexing function remapping, because some IO ports have many functions, so specific each IO port to use which function, is set by this option byte.

This OPT2 is used to set up the multiplexing function, NOPT2 is the inverse of OPT2. The OPT2 has eight bits, each with a 0 and a 1 for a different function.

The specific meanings of each representative are as follows:

This OPT2 defaults to 0, which means that the default pin function is on the top line. The above code uses the timer 1 channel 2 pin, also known as TIM1_CH2, which is set with the AFR0 bit. As can be seen from the table, the default function of AFR0 bit is SPI pin function, and the TIM function required by PWM must be set to bit 1 of OPT0, that is, AFR0 bit.

There are two ways to set the option word. One is to add the option word setting code in the program, and the other is to directly set the interface of the software. Setting option words in code is not covered here because it is cumbersome and error-prone. Here is a direct introduction to the use of burn software to set.

The software used here is ST Visual Programmer

The interface is as follows:

With this software to the single-chip burning program when you can directly set the option word, the above code compiled. Then use the burn software to open the compiled files.

Once open, click the OPTIN BYTE option under the software to open the code’s options Settings, and the software will display AFR0- the default option for AFR7. As you can see from the table analysis of the option bytes above, what needs to be set here is AFR0. Click on the last edge of the AFR0 line.

And then you can see that the options after AFR0 are going to have a downward arrow, and there are two options at the bottom, and the top option is the option that’s set to 0, and the bottom option is the option that’s set to 1, so you definitely want to select the bottom option.

PC5 is set to TIM2_CH1,PC6 to TIM1_CH1, and PC7 to TIM1_CH2. The second bit of the leftmost option byte state is also changed to 01. The timer function is set successfully. Note that the default functions of THE IO ports PC5, PC6, and PC7 will be changed after the option word is set.

After setting up, the code is directly written to the microcontroller, then it will be found that the oscilloscope has output waveform.

If the microcontroller does not work properly during code debugging, check whether the remapping function needs to be enabled for the I/O port. Ensure that the remapping function is normal, and then check other parts.