Page 4 of 6

Re: Timer Calculator Application Released!

Posted: 25 May 2013 18:38
by aCkO
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 175747 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

Problem Timer Calculator

Posted: 27 May 2013 10:49
by tyzanu
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);

}
}

Re: Timer Calculator Application Released!

Posted: 27 May 2013 12:00
by aCkO
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 175714 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

Re: Timer Calculator Application Released!

Posted: 27 May 2013 12:49
by tyzanu
not work correct with OCR0 = 124; :oops:

Re: Timer Calculator Application Released!

Posted: 27 May 2013 13:40
by aCkO
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

Re: Timer Calculator Application Released!

Posted: 29 May 2013 10:18
by tyzanu
work ok, thanks for information...

Re: Timer Calculator Application Released!

Posted: 31 May 2013 12:56
by filip
Hi,

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

Regards,
Filip.

Re: Timer Calculator Application Released!

Posted: 01 Jun 2013 14:04
by tyzanu
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++;
}
}

Re: Timer Calculator Application Released!

Posted: 06 Jun 2013 19:06
by MARIO
Timer Calculator 2.5.0.0 still have problems.
TCv2.5.PNG
TCv2.5.PNG (43.86 KiB) Viewed 175368 times
This setup is giving about 4ms not 1ms.

Re: Timer Calculator Application Released!

Posted: 14 Jun 2013 02:38
by sly
a fix for pic microcontrollers :

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

Re: Timer Calculator Application Released!

Posted: 14 Jun 2013 02:45
by aCkO
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

Re: Timer Calculator Application Released!

Posted: 14 Jun 2013 08:09
by filip
Hi,

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

Regards,
Filip.

Re: Timer Calculator Application Released!

Posted: 26 Aug 2013 13:38
by tyzanu
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
{
}
}

Re: Timer Calculator Application Released!

Posted: 05 Sep 2013 18:18
by tyzanu
you have found this problem???

Re: Timer Calculator Application Released!

Posted: 12 Sep 2013 20:49
by corado
Hallo,
where is the Timer Calculator for XMega?
Do you think you will have provide it shortly?
Best Regards