STM32 PWM.

PWM on STM32 is generated using timers; the documentation says a lot about them, but PWM turned out to be quite easy to configure. We'll generate PWM using channel 4 of timer 1.

First, we need to find which pin is responsible for channel 4 of timer 1; to do this, open the Technical DataSheet for your MCU (mine is the STM32F103VET6) and find which pin has TIM1_CH4 as an alternate function.
STM32 PWM.


To use the timer, the pin must be configured as push-pull.
STM32 PWM.


Let's look at the registers we'll need.

TIMx_PSC – the input prescaler; its value can be calculated using the formula

F = fCK_PSC / (PSC[15:0] + 1)

  • F – the frequency at which the timer is clocked
  • fCK_PSC — the timer frequency before the prescaler
  • PSC[15:0] — the value of the PSC register

TIMx_ARR – the auto-reload register; the counter counts from 0 to TIMx_ARR, or the other way round depending on the counting direction; by changing this value we change the PWM frequency.

TIMx_CCRy[x – timer number, y – channel number] – determines the PWM duty cycle. That is, if we write 1000 to ARR and 300 to CCRy, then with an active-high level and non-inverted PWM the duty cycle will be 0.3, or 30%.

TIMx_CNT – the 16-bit counter register; it changes its value by ±1 with each incoming pulse, depending on the counting direction.

TIMx_CR1 ->DIR – the counting direction bit: when set to '0' the counter counts up, when set to '1' it counts down. When the counter is configured in center-aligned mode, the bit is read-only.

TIMx_CCER->CCyE[x – timer number, y – channel number] – setting '1' in this bit enables the corresponding timer channel as an output, including for generating PWM.

TIMx_BDTR->MOE – setting this bit to one allows the timer pins to be used as outputs.

The OCyM[2:0] bits, where y is the channel number, in the TIMx_CCMR register select the PWM mode, non-inverted or inverted.
STM32 PWM.

I should say that channels one and two are configured in the CCMR1 register, and channels three and four in the CCMR2 register.

TIMx_CCER->CCyP[x - timer number, y - channel number] - this bit selects the active level: '0' - high, '1' - low.

TIMx_CR1 ->CMS[1:0] - selects edge-aligned or center-aligned mode, which is similar to Fast PWM and Phase Correct PWM on AVR.
  • 00: Edge-aligned mode. The counter counts up or down depending on the direction bit (DIR).
  • 01: Center-aligned mode 1. The counter counts up and down. Interrupt flags of channels configured as outputs (CCxS=00 in TIMx_CCMRx) are set only when the counter is counting down.
  • 10: Center-aligned mode 2. The counter counts up and down. Interrupt flags of channels configured as outputs (CCxS=00 in TIMx_CCMRx) are set only when the counter is counting up.
  • 11: Center-aligned mode 3. The counter counts up and down. Interrupt flags of channels configured as outputs (CCxS=00 in TIMx_CCMRx) are set when the counter is counting both up and down.

TIMx_CRx->CEN – setting '1' in this bit starts the counter.

Let's write code to generate edge-aligned PWM.

#include "stm32f10x.h"

int main (void)
{
	// Clock GPIOA, TIM1 and the port alternate functions
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_AFIOEN;
        
        //PA11 push-pull
	GPIOA->CRH &= ~GPIO_CRH_CNF11;
	GPIOA->CRH |= GPIO_CRH_CNF11_1;

	GPIOA->CRH	&= ~GPIO_CRH_MODE11;
	GPIOA->CRH	|= GPIO_CRH_MODE11_1;
	
	//prescaler
	TIM1->PSC = 72;
	//reload value
        TIM1->ARR = 1000;
	//duty cycle
	TIM1->CCR4 = 300;
	//configure channel 4 as output, active level low 
	TIM1->CCER |= TIM_CCER_CC4E | TIM_CCER_CC4P;
	//allow the timer pins to be used as outputs
	TIM1->BDTR |= TIM_BDTR_MOE;
	//PWM mode 1, non-inverted PWM on channel 4
         TIM1->CCMR2 = TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1; 
        //if you need to configure channel one, you can do it like this
        //TIM1->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1;
	//count up
	TIM1->CR1 &= ~TIM_CR1_DIR;
	//edge-aligned, Fast PWM
	TIM1->CR1 &= ~TIM_CR1_CMS;
	//start the counter
	TIM1->CR1 |= TIM_CR1_CEN;
}

Let's see what we got.
STM32 PWM.

Changing the active level to high.
STM32 PWM.

STM32 PWM.

Changing non-inverted PWM to inverted.
STM32 PWM.

STM32 PWM.

Everything is back as it was before; nothing complicated.