cancel
Showing results for 
Search instead for 
Did you mean: 

how to change output pin to pwm pin

ahmash
Associate II

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

2 REPLIES 2
TDK
Guru

> 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".

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