cancel
Showing results for 
Search instead for 
Did you mean: 

I need to know why the timer is not updating for every 1000hz in my code

VSrin
Associate II

#include <iostm8l.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include "defs.h"

unsigned int count = 0;

@svlreg @interrupt void TIM1(void)

{

count += 1;

TIM1_SR1 &= ~(0x01);

}

main()

{

CLK_DIVR = 0x00; // Set the frequency to 16Mhz

CLK_PCKENR2 = 0x02; // clock for timer1

PC_DDR = 0x80; // direction output for led

 PC_CR1 = 0x80; // fast push pull mode

PE_DDR = 0x80; // direction output for led

 PE_CR1 = 0x80; // fast push pull mode

TIM1_PSCRH = 0x3e;

TIM1_PSCRL = 0x80;

TIM1_ARRH = 0x3e; 

TIM1_ARRL = 0x80;

TIM1_CR1 = 0x81;

TIM1_IER = 0x01;

  _asm("rim\n");

while(1)

{

if (count == 1000)

{

PE_ODR ^= 0x80;

count = 0;

}

}

}

11 REPLIES 11
VSrin
Associate II

Thank you so much, in my previous configuration I did not change the arr values according to the program. So, there was a miscalculation. Now it works. I have one small clarification where did you find the equation for timer interrupt generation? I cannot find in the reference manual, I use stm8l152c6 mcu

Counters, both for the timer, and the clock prescaler act as dividers. The math and concepts are fairly pedestrian, and diagrammed in the documentation for the timer.

In silicon the "end state" can be implemented with a comparator, so be sensing the current state is N-1, the next state can reset the count to 0

0 thru N-1 has N states.

UPDATE RATE = TIMCLK / ((PSCR+1) * (ARR+1))

UPDATE RATE = (TIMCLK / (PSCR+1)) / (ARR+1) // Alternate expression of the same math

If it is easier think of it as a factoring problem, there are multiple workable solutions

UPDATE RATE = TIMCLK / (Q * P)

or

UPDATE RATE = (TIMCLK / Q) / P

Where PSCR = Q-1 and ARR = P-1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..