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;

}

}

}

1 ACCEPTED SOLUTION

Accepted Solutions

Set PSCR = 16000-1 and ARR = 1000-1

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

View solution in original post

11 REPLIES 11

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?

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

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

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

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

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;

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

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

In the manual, it is mentioned as ***_CNT = ***_PSC/(PSCR[15:0]+1). Maybe I got confused

***_CNT = ***_PSC/(PSCR[15:0]+1)

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 << 😎 + (uint16_t)TIM1_CNTRL ) >= 62500 )

{

TIM1_CNTRH = 0;

   TIM1_CNTRL = 0;

PC_ODR ^= 0x80;

count = 0;

}

}

}

Set PSCR = 16000-1 and ARR = 1000-1

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