Skip to main content
whitehorsesoft
Associate III
May 9, 2026
Solved

STM8S using TIM4 interrupts

  • May 9, 2026
  • 5 replies
  • 404 views

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.

Best answer by whitehorsesoft

@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!

5 replies

AA1
Senior III
May 9, 2026

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?

 

whitehorsesoft
Associate III
May 9, 2026

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.

AA1
Senior III
May 9, 2026

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?

 

whitehorsesoft
Associate III
May 10, 2026

The master clock is not the issue, it truly is at 128 kHz.

AA1
Senior III
May 10, 2026

Which is the value of CLK_CMSR register?

 

whitehorsesoft
Associate III
May 11, 2026

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

whitehorsesoft
Associate III
May 12, 2026

Sorry @unknown 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).

Peter BENSCH
Technical Moderator
May 13, 2026

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:

  • TIM4_IER.UIE = 1
  • global interrupts enabled via RIM

Without RIM, the timer may keep running and set its flag, but the interrupt will not be serviced as expected.

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
whitehorsesoft
Associate III
May 15, 2026

To followup, I added the RIM instruction, but no change in behavior.

Peter BENSCH
Technical Moderator
May 15, 2026

OK for RIM, but in your code visible here, you apparently want to enable the interrupt for TIM4, but you set UIE to 0:

bset TIM4_IER, #0 ; UIE: Update interrupt enable


RM0016, section 17.7.5:

UIE: Update interrupt enable
0: Update interrupt disabled
1: Update interrupt enabled

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
whitehorsesoft
Associate III
May 15, 2026

> but you set UIE to 0:...

no, that would be a bres instruction to set to 0. the bset instruction sets it to 1 - in this case, the 0th bit (LSB) is set to 1. The "0" indicates the bit order, not the value being set. Ref bres and bset in PM0044.

whitehorsesoft
whitehorsesoftAuthorAnswer
Associate III
May 15, 2026

@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!