cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S using TIM4 interrupts

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

18 REPLIES 18
AA1
Senior III

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?

 

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

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?

 

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

Which is the value of CLK_CMSR register?

 

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

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).

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.

That is fascinating. PM0044 states RIM is not needed when using WFI:

whitehorsesoft_0-1778771900264.png

I must be misunderstanding... in any event later today I'll bring the RIM instruction in and test.