Skip to main content
Visitor II
September 18, 2024
Question

Change timers output compare mode

Hello,

I have a problem, I would like to change a timer output compare mode on my STM32G484xE microcontroller. I would like to switch between toggle and PWM mode 1. I do the whole thing with a small function for writing registers:

void ProgRegister(uint32_t *RegAddr, uint32_t Bits, uint32_t BitMask){
 *RegAddr = (*RegAddr & ~BitMask) | Bits;
}

This is how I switch to the different output compare modes:

//switch to toggle mode
ProgRegister((uint32_t*) &TIM1->CCMR1, TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1, TIM_CCMR1_OC1M); // 0011 Toggle

//switch to pwm mode 1
ProgRegister((uint32_t*) &TIM1->CCMR1, TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1, TIM_CCMR1_OC1M); // 0110 PWM Mode 1

The timer is initialized in toggle mode, and if I then want to set it to PMW1 mode, it remains in toggle mode. The LOCK bits are also not set, so I should be able to write to the registers.

Do you have any idea what my problem could be, so that the mode does not switch?

 

This topic has been closed for replies.

2 replies

waclawek.jan
Super User
September 18, 2024

> it remains in toggle mode

How do you know?

Step through disasm of that function to see what happens at register level.

By casting to uint32_t * you removed volatile; I'm not sure if that's the problem.

JW

TDK
September 18, 2024

Consider using the standard MODIFY_REG macro instead. Does the same thing as your function but doesn't remove the volatile.

#define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))

 

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