cancel
Showing results for 
Search instead for 
Did you mean: 

Switching Port Pins between Alt Function and Normal mode

RMyer.1
Associate III

Hi All,

In a situation where someone might be flashing an LED using a Timer Output (eg Alternate Function Mode connected to a Timer channel output), what if you want to then turn the LED on solid or totally off and then maybe back to the Timer flashing mode, what is the best approach to this?

Would you need to switch the Port pin from Alternate Mode back to a regular output, or can you just disconnect it from the Timer Output, eg Disable Capture/Compare Output enable bit in TIMx_CCER and then just control it directly in the GPIOx_BSRR register.

My understanding is if the Alternate Mode is set for a pin then writing to the GPIOx_BSRR register won't do anything to the output state.

Thanks.

6 REPLIES 6
TDK
Guru

What chip? Older families are different.

Generally, you need to change the GPXIO_MODER register to set the pin as GPIO instead of AF. Doing anything in TIMx_CCER won't help, it will still be controlled by the timer.

Writing to BSRR/ODR when the pin is not in output mode will have no effect. The ODR register will update, but since it's not being used as output, it won't have any other effect.

There are other ways to set a pin high or low using strictly timer functionality.

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

Thanks for the response, sorry for not mentioning the chip, it is a STM32F303K8T6 (in a Nucleo-32 board).

Would you mind explaining please how the pin can be set to be fixed high or low by the timer itself? That sounds like a much cleaner solution.

rpip.1
Associate III

you can use PWM mode of TIM. it's avaliable to make TIM On or Off totally by writing CCR to 0 or ARR+1.

Thanks, your response made me read the datasheet a little closer. I have the Timer in Output Compare mode and ST says for the OCxM bits:

OCxM=001 = Set Active.

OCxM=010 = Set Inactive.

OCxM=011 = Toggle Output << What I currently have to generate the LED flashing

So the way I read that even when the Timer Expires I just need to have the OCxM bits as 001 to turn the Output High (and stay High) and 010 to turn the Output Low (and stay low). Does that seem right?

it's right. Setting OCxM is a way.

Yes, that will do it.
If you feel a post has answered your question, please click "Accept as Solution".