2026-05-08 7:04 PM
Greetings, I'm trying to set up TIM4 per the reference manual (RM0016).
With the clock set to LSI at 128kHz, the following code causes the PD2 output pin to appear constantly high visually via a wired LED. I would expect it would be blinking once per second. I don't have access to a scope so I can't see what is actually happening. Anyone spot anything I'm doing incorrectly?
sim
; F_Master Prescalar tim clk
; 128_000 Hz / 1024 = 125
mov TIM4_PSCR, #0b1010 ; 2 ** 0b1010 = 1024 prescaler
; ARR at 0x7d = 125
mov TIM4_ARR, #0x7d
bset TIM4_IER, #0 ; UIE: Update interrupt enable
bset TIM4_CR1, #0 ; CEN: Counter enable...and the interrupt code:
tim4_up_isr:
; PD2 blinks once per second
; from TIM4
bres TIM4_SR, #0 ; UIF: Update interrupt flag
bcpl PD_ODR, #2
iret...fwiw when I set line 5 of the interrupt code to use bres PD_ODR, #2, the output appears low visually.
Solved! Go to Solution.
2026-05-15 5:10 AM
@Peter BENSCH your reply on another thread also applies here, and it is the solution here as well - I am attempting to set the prescaler on TIM4 as if it takes a 4 bit value, when in fact it is taking a 3 bit value, so I'm losing the most significant bit when setting the prescaler. All other timing is off because of that. Thank you for your help!
2026-05-09 4:27 AM
I think the clock is still the default HSI 16 MHz. Due to this the LED blinks very fast and it seems it is always on.
Are you sure the clock is LSI 128 KHz?
2026-05-09 9:23 AM
Thanks for the double-check but yes it is LSI 128 kHz. I omitted the code setting the clock, to focus on just the TIM4 code.
I'm wondering if maybe I'm not setting something in the correct order, or if another register should be set that I've missed? FYI I'm using RM0016 and DS7147.
2026-05-09 11:29 AM
You have code to set the clock to LSI 128 KHz, but this doesn't prove that the clock is LSI 128 KHz. The code can be wrong. You need to verify that the clock is really 128 KHz. Did you read the clock registers to confirm it?
2026-05-09 6:44 PM
The master clock is not the issue, it truly is at 128 kHz.
2026-05-10 1:09 AM
Which is the value of CLK_CMSR register?
2026-05-11 11:32 AM
Thank you for the review. I'm curious what you mean by global interrupts being enabled in "but one thing to check is whether global interrupts are actually enabled"?
2026-05-11 8:05 PM
Sorry @jimmyande0 now I see what you mean. I did not include it in the code sample, but there is a wfi instruction further down. That will enable global interrupts (RIM instruction not needed with WFI).
2026-05-13 1:19 AM
WFI only makes the CPU wait; it does not enable global interrupts. So for TIM4 to wake the MCU and run the ISR, you still need:
Without RIM, the timer may keep running and set its flag, but the interrupt will not be serviced as expected.
Regards
/Peter
2026-05-14 8:19 AM
That is fascinating. PM0044 states RIM is not needed when using WFI:
I must be misunderstanding... in any event later today I'll bring the RIM instruction in and test.