cancel
Showing results for 
Search instead for 
Did you mean: 

Can not set up PWM for a different Pin than PA5

AnnonymusMonkey
Associate

Hey,
after trying to setup a different PIN for example: PC0 for PWM I just can not get it to work.
But this sample code works only for PA5:

 

 

#include "stm32f3xx.h"

int main(void) {
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
	RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
	GPIOA->MODER |= 0b10 << 10;
	GPIOA->AFR[0] |= 0b0001 << 20;
	TIM2->CCMR1 = 0 << 16 | 0b110 << 4;
	TIM2->CCER = TIM_CCER_CC1E;
	TIM2->PSC = 7999;
	TIM2->ARR = 999;
	TIM2->CCR1 = 100;
	TIM2->CR1 = TIM_CR1_CEN;

	for (;;)
		__WFI();
}

 

 

How would I need to change it for use with PC0?
Since its kind of for a project I can not use HAL.
I use STM32CubeIDE on the Nucleo-F303RE board.
Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Use the datasheet to find out which channels are available on which pin.

The only timer available on PC0 is TIM1_CH1. So change your timer to TIM1 and initialize PC0 to use TIM1_CH1.

The datasheet lists the AF number for each function. In this case it's AF2 for TIM1_CH1 on PC0. You use that on the GPIOx MODER register to set the AF mode.

TDK_0-1701528849524.png

 

If you're programming registers, the reference manual will be the best source of information on how those registers work.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Guru

Use the datasheet to find out which channels are available on which pin.

The only timer available on PC0 is TIM1_CH1. So change your timer to TIM1 and initialize PC0 to use TIM1_CH1.

The datasheet lists the AF number for each function. In this case it's AF2 for TIM1_CH1 on PC0. You use that on the GPIOx MODER register to set the AF mode.

TDK_0-1701528849524.png

 

If you're programming registers, the reference manual will be the best source of information on how those registers work.

If you feel a post has answered your question, please click "Accept as Solution".
STOne-32
ST Employee

Dear Gentleman,

 

First, welcome in the community and indeed you can code direct registers . You need to just to know the pins and hardware connected for each GPIO and remap functions with Alternate such as Timers TIM2 channels to be able to output the PWM on a specific pins . You can refer to Table 14 in the datasheets and look for AFx where TIMx_CHy are mapped . Then as done in the code to clock the used peripherals and I/O.

 

Hope it helps you .

Cheers,

STOne-32

> The only timer available on PC0 is TIM1_CH1. So change your timer to TIM1 and initialize PC0 to use TIM1_CH1.

If you are going to use TIM1, don't forget, that it's an Advanced timer, so in addition to what you do in other timers, it needs TIM1_BDTR.MOE to be set for outputs to be enabled.

JW