2023-08-21 04:43 AM
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,
Solved! Go to Solution.
2023-08-21 06:24 AM
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
2023-08-21 06:10 AM
> 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.
2023-08-21 06:24 AM
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