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-14 11:04 PM
To followup, I added the RIM instruction, but no change in behavior.
2026-05-15 12:27 AM
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
2026-05-15 1:01 AM
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.
2026-05-15 1:42 AM
> 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.
2026-05-15 1:44 AM - edited 2026-05-15 1:49 AM
> 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
2026-05-15 1:46 AM
Right, I read it too superficially and interpreted #0 as the value to be written, sorry.
2026-05-15 2:04 AM
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...
2026-05-15 3:54 AM
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.
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!