Timer Calculator Application Released!

Timer Calculator is a free software development tool used for easier calculation of timer interrupts.
Author
Message
aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Timer Calculator Application Released!

#46 Post by aCkO » 25 May 2013 18:38

Toley wrote:I believe the soft is correct. Timer1 is a 16bits counter, if you are running at 8MHz it will increment every 0.5us. For 100ms it will need 200 000 tick this is over 16 bits.
You forgot the prescaler:

200000 / 4 = 50000

TMR1 preload = 65536 - 50000 = 15536

v2.00 will give you 15535 and complain about the difference between calculated and entered time value although it is obvious that there should be no difference. This is why I suspect that integer math was not used in the calculation. At least, it is very easy to determine the limits, i.e. the min and max interrupt time based on MCU clock and available prescaler and postscaler values:

Code: Select all

TIME = COUNT * (4/Fosc) * PS
The expression above has the following limits for Fosc = 8 MHz:

Code: Select all

MIN_TIME = 1 * (4/8000000) * 1 = 0.5 us
MAX_TIME = 65536 * (4/8000000) * 8 = 262.144 ms
However, all versions of the application have the same bug that can be reproduced in these steps:
1. Choose PIC18, 8 MHz clock, Timer0
2. Enter 1000 seconds as interrupt time and press calculate. You will get a popup saying that there is no solution which is OK
3. Enter 1 second and press calculate. You will get the solution
4. Now enter 1000 seconds again and press calculate:
timer_calculator.jpg
timer_calculator.jpg (21.54 KiB) Viewed 175590 times
You will also get the solution along with some strange value for the prescaler (1:0) and a warning that difference is ~999 seconds or 99.9%.

In my opinion, timer is a generic, architecture independent concept, determined by simple set of variables (size, frequency, prescaler, postscaler etc.) governed by integer math rules and every calculator application should be coded with these things in mind. I might even create one if I get some spare time :)

Regards

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Problem Timer Calculator

#47 Post by tyzanu » 27 May 2013 10:49

Not functioning properly?Im using KIT BIGAVR6 with atmega128 ... 8Mhz
I do something wrong in the code attached? generate a real-time clock...

int milisecunde = 0;
int secunde = 0, secundemain;
int minute = 0, minutemain = 0;
char txt[12];


void InitTimer0()
{
SREG_I_bit = 1;
OCR0 = 125;
TCCR0 = 0x28;
TCCR0 |= 0x03;
OCIE0_bit = 1;
}

void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_COMP
{
milisecunde++;
if(milisecunde == 1000)
{
milisecunde = 0;
secunde++;
}

if(secunde == 60)
{
secunde = 0;
minute++;
}
}

void main()
{
UART1_Init(9600);// Initialize UART module at 9600 bps
Delay_ms(100);
InitTimer0();
milisecunde = 0;
secunde = 0, secundemain = 0;
minute = 0, minutemain = 0;

while(1)
{
asm cli;
secundemain = secunde;
minutemain = minute;
asm sei;

UART1_Write_Text("Clock: ");
IntToStr(minutemain, txt);
UART1_Write_Text(txt);
UART1_Write_Text(":");
IntToStr(secundemain, txt);
UART1_Write_Text(txt);
UART1_Write_Text("\n\r");

delay_ms(1000);

}
}
Attachments
tcalc.JPG
tcalc.JPG (51.71 KiB) Viewed 175562 times
timer_calculator_at128.zip
(27.92 KiB) Downloaded 1251 times

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Timer Calculator Application Released!

#48 Post by aCkO » 27 May 2013 12:00

tyzanu wrote:Not functioning properly?Im using KIT BIGAVR6 with atmega128 ... 8Mhz
I do something wrong in the code attached? generate a real-time clock...
It seems that the formula for OCRn calculation is off by 1. Everything else looks OK. If you check the datasheet, you will find a formula for OCx frequency in CTC mode:
avr_ctc.PNG
avr_ctc.PNG (4.13 KiB) Viewed 175557 times
If we do some math:

Code: Select all

OCRn = Fclk / (2 * N * Foc) - 1
Our interrupt time is:

Code: Select all

t = Toc / 2 = 1 / (2 * Foc)
If we combine these expressions we get the formula for OCRn as a function of interrupt time t:

Code: Select all

OCRn = Fclk * t / N - 1
In your case:

Code: Select all

OCR0 = 8000000 * 0.001 / 64 - 1 = 125 - 1 = 124
So, for 1ms interrupt OCR0 should be 124 (not 125 as the application suggests).

Regards

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Re: Timer Calculator Application Released!

#49 Post by tyzanu » 27 May 2013 12:49

not work correct with OCR0 = 124; :oops:

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Timer Calculator Application Released!

#50 Post by aCkO » 27 May 2013 13:40

tyzanu wrote:not work correct with OCR0 = 124; :oops:
Now you made me pull out my EasyAVR6 :)

124 is the correct value. Checked with scope.

Regards

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Re: Timer Calculator Application Released!

#51 Post by tyzanu » 29 May 2013 10:18

work ok, thanks for information...

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Timer Calculator Application Released!

#52 Post by filip » 31 May 2013 12:56

Hi,

Thank you for reporting this issue, I will notify our developers and it will be solved as soon as possible.

Regards,
Filip.

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Re: Timer Calculator Application Released!

#53 Post by tyzanu » 01 Jun 2013 14:04

I used to work right next function, but 7:00 hours is a delay of 2 seconds lag behind

void InitTimer0()
{
SREG_I_bit = 1;
OCR0 = 124;
TCCR0 = 0x28;
TCCR0 |= 0x03;
OCIE0_bit = 1;
}



void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_COMP
{
milisecunde++;
if(milisecunde == 2000)
{
milisecunde = 0;
secunde++;
}

if(secunde == 60)
{
secunde = 0;
minute++;
}
}

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Timer Calculator Application Released!

#54 Post by MARIO » 06 Jun 2013 19:06

Timer Calculator 2.5.0.0 still have problems.
TCv2.5.PNG
TCv2.5.PNG (43.86 KiB) Viewed 175211 times
This setup is giving about 4ms not 1ms.
BR
EasyPic6 and registered mkC PRO FOR PIC since 2011
You're never too old to learn something stupid.(PARAPROSDOKIAN)

sly
Posts: 43
Joined: 13 Feb 2005 02:20

Re: Timer Calculator Application Released!

#55 Post by sly » 14 Jun 2013 02:38

a fix for pic microcontrollers :

2.MCU clock frequency -> just put Timer clock frequency (MCU clock frequency/4)

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Timer Calculator Application Released!

#56 Post by aCkO » 14 Jun 2013 02:45

sly wrote:a fix for pic microcontrollers :

2.MCU clock frequency -> just put Timer clock frequency (MCU clock frequency/4)
And add 1 to the preload :)

Regards

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Timer Calculator Application Released!

#57 Post by filip » 14 Jun 2013 08:09

Hi,

This has been fixed by the developers and it will be included in the following version of the Timer Calculator.

Regards,
Filip.

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Re: Timer Calculator Application Released!

#58 Post by tyzanu » 26 Aug 2013 13:38

interruption in your example is 100ms, in timer calculator is 1s, but settings timer1 is the same

/* Project name:
Timer1_interrupt (Timer1 interrupt test)
* Copyright:
(c) MikroElektronika, 2012.
* Revision History:
20091106:
- Initial release;
20110511(TL):
- Adapted for dsPIC33EP512MU810;
* Description:
This code demonstrates how to use Timer1 and it's interrupt.
Program toggles LEDs on PORTB.
* Test configuration:
MCU: dsPIC33EP512MU810
http://ww1.microchip.com/downloads/en/D ... 70616g.pdf
Dev.Board: EasyPIC Fusion v7
http://www.mikroe.com/easypic-fusion/
Oscillator: HS-PLL, 80.00000 MHz
Ext. Modules: None.
SW: mikroC PRO for dsPIC
http://www.mikroe.com/mikroc/dspic/
Timer Calculator
http://www.libstock.com/projects/view/3 ... calculator
* NOTES:
- Use the ORG directive to declare your procedure as an interrupt service routine.
The address after the ORG statement indicates which interrupt you will assign to
your procedure.
Please consult the datasheet for your MCU for interrupt vector table
details and available addresses.
- Also, consult mikroC PRO for dsPIC30/33 and PIC24 help on how to use interrupts.
- Turn off all PORT LEDs except PORTB at SW15 (board specific).
*/

void Timer1Int() iv IVT_ADDR_T1INTERRUPT {
T1IF_bit = 0; // clear T1IF
LATB = ~ PORTB; // invert PORTB
}

void main() {
PLLPRE_0_bit = 0;
PLLPRE_1_bit = 0;
PLLPRE_2_bit = 0;
PLLPRE_3_bit = 0;
PLLPRE_4_bit = 0;

PLLFBD = 38; // PLL multiplier M=38

PLLPOST_0_bit = 0;
PLLPOST_1_bit = 0;

ANSELA = 0x00; // Convert all I/O pins to digital
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
ANSELG = 0x00;

TRISB = 0; // initialize PORTB as output
LATB = 0xAAAA; // initialize PORTB value
IPC0 = IPC0 | 0x1000; // interrupt priority level = 1
T1IF_bit = 0; // clear T1IF
T1IE_bit = 1; // enable Timer1 interrupts

T1CON = 0x8020; // Timer1 ON, internal clock FCY, prescaler 1:64
PR1 = 62500; // 100ms interrupt

while (1) // endless loop, interrupted by Timer1Int
{
}
}
Attachments
tc.JPG
tc.JPG (51.03 KiB) Viewed 169099 times

tyzanu
Posts: 182
Joined: 21 Aug 2011 17:27
Contact:

Re: Timer Calculator Application Released!

#59 Post by tyzanu » 05 Sep 2013 18:18

you have found this problem???

corado
Posts: 399
Joined: 28 Mar 2009 11:03

Re: Timer Calculator Application Released!

#60 Post by corado » 12 Sep 2013 20:49

Hallo,
where is the Timer Calculator for XMega?
Do you think you will have provide it shortly?
Best Regards

Post Reply

Return to “Timer Calculator”