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.

18 REPLIES 18

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

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.

Just a waste of time.

You said:

"The following code causes the PD2 output pin to appear constantly high. When I set line 5 of the interrupt code to use bres PD_ODR, #2, the output appears low visually".

If this happens is because it enters in the interrupt code.

I asked: Which is the value of CLK_CMSR register? And you ignored my question.

 

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

> I asked: Which is the value of CLK_CMSR register? And you ignored my question.

No, you ignored my response that it is truly set to 128kHz. But, to satisfy you, the value is 0xd2 - which of course indicates it is LSI / 128kHz. Both PD3 and PD2 LEDs light up right away when I insert this code after setting up clock, but before the code in question, confirming beyond a doubt it is 128kHz.

    ; verify LSI is clock source by turning on PD4
    ld a, CLK_SWR
    cp a, #0xd2
    jrne 1$
    bset PD_ODR, #3

    ; verify some more, looking at master status register
    ld a, CLK_CMSR
    cp a, #0xd2
    jrne 1$
    bset PD_ODR, #2

 

Right, I read it too superficially and interpreted #0 as the value to be written, sorry.

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.

Not a problem, it is confusing when used to C-style, then see that assembly instruction with zero but the value is actually being set instead of reset...

That happened to me - when I was already programming ASM more than 40 years ago... But in fact, when switching from C back to ASM, you do have to be a little more careful.

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.

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