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:31 AM
Set PSCR = 16000-1 and ARR = 1000-1
2019-09-24 02:13 PM
What frequency is it clocking?
16,000,000 / (16,000 * 16,000) = 0.0625 Hz
0.0625 / 1,000
= 0.0000625 Hz, so perhaps toggle PE7 every 16000 seconds?
2019-09-24 02:23 PM
Can you please explain this calculation?
I thought 16000000/16000 = 1000hz
1/1000 = 0.0001 seconds
So i used for variable "count" increment and compare for every 1000 times.
Please correct me if i am wrong
2019-09-24 04:51 PM
>>Can you please explain this calculation?
ARR and PSC both describe division counters. Arguable N-1 values, but I was illustrating scale of failure.
Clearly it is wrong, you've already stated it doesn't work as expected
Try
TIM1_PSCRH = 0x00;
TIM1_PSCRL = 0x00;
TIM1_ARRH = 0x3E;
TIM1_ARRL = 0x7F;
2019-09-25 05:54 AM
My timer works in this configuration but still i need to know how the calculation in the timer works,
TIM1_PSCRH = 0x00;
TIM1_PSCRL = 0x80;
TIM1_CR1 = 0x11;
TIM1_EGR = 0x01;
TIM1_IER = 0x01;
2019-09-25 08:00 AM
UPDATE RATE = TIMCLK / ((PSCR+1) * (ARR+1))
2019-09-25 08:09 AM
In the manual, it is mentioned as ***_CNT = ***_PSC/(PSCR[15:0]+1). Maybe I got confused
2019-09-25 08:11 AM
***_CNT = ***_PSC/(PSCR[15:0]+1)
2019-09-25 08:26 AM
can you provide me a configuration for 1 sec counter to toggle led. I got this for every one second,
main()
{
CLK_DIVR = 0x07; // 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_CR1 = 0x01;
TIM1_PSCRH = 0x00;
TIM1_PSCRL = 0x80;
TIM1_ARRH = 0xff;
TIM1_ARRL = 0xff;
//TIM1_EGR = 0x01;
//TIM1_IER = 0x01;
_asm("rim\n");
while(1)
{
if ( ( ((uint16_t)TIM1_CNTRH << 8) + (uint16_t)TIM1_CNTRL ) >= 62500 )
{
TIM1_CNTRH = 0;
TIM1_CNTRL = 0;
PC_ODR ^= 0x80;
count = 0;
}
}
}
2019-09-25 08:31 AM
Set PSCR = 16000-1 and ARR = 1000-1