Skip to main content
Associate II
August 21, 2023
Solved

how to change output pin to pwm pin

  • August 21, 2023
  • 2 replies
  • 2630 views

hello guys i am new to stm,i am using stm32103 and stmcubeide ,my question is very basic ,i know how to initialize a pin as an output pin or as a pwmpin but i donot know that how to change an output pin into a pwmoutput pin and then again convert that pin from pwmout to general output pin in the middle of a program ,in my case i am using  pin no PA8 as TIM1_ch1 pwm out ,, I want to convert it to PA8 GPIO_output then again ,from GPIO_output to TIM1_ch1 in middle of programme please help me,

 

This topic has been closed for replies.
Best answer by waclawek.jan

You need to change GPIOA_CRH.CNF8 from 0b00 (General purpose output push-pull) to 0b10 (Alternate function output Push-pull) and vice versa (assuming GPIOA_CRH.MODE8 is already non-zero from previous setup of this pin to GPIO Output). That would be

GPIOA->CRH |= (0b10 << GPIO_CRH_CNF8_Pos);

and then back using

GPIOA->CRH &= (0b10 << GPIO_CRH_CNF8_Pos);

Note, that these operations are not atomic, so if you want to change GPIOA->CRH both in non-interrupt and interrupt context, you'd need to disable/enable interrupts accordingly.

JW

2 replies

TDK
August 21, 2023

> i know how to initialize a pin as an output pin or as a pwmpin but i donot know that how to change an output pin into a pwmoutput pin

Changing a pin is simply re-initializing it as the other thing. No need to undo anything. Procedure is the same, as long as you aren't relying on default values at reset.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
waclawek.janBest answer
Super User
August 21, 2023

You need to change GPIOA_CRH.CNF8 from 0b00 (General purpose output push-pull) to 0b10 (Alternate function output Push-pull) and vice versa (assuming GPIOA_CRH.MODE8 is already non-zero from previous setup of this pin to GPIO Output). That would be

GPIOA->CRH |= (0b10 << GPIO_CRH_CNF8_Pos);

and then back using

GPIOA->CRH &= (0b10 << GPIO_CRH_CNF8_Pos);

Note, that these operations are not atomic, so if you want to change GPIOA->CRH both in non-interrupt and interrupt context, you'd need to disable/enable interrupts accordingly.

JW