2019-09-24 01:14 PM
#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;
}
}
}
Solved! Go to Solution.
2019-09-25 08:50 AM
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
2019-09-25 09:17 AM
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