• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

Author
alpha91
New Member
• Total Posts : 3
• Reward points : 0
• Joined: 2014/09/06 07:21:38
• Location: 0
• Status: offline
0

# PIC Timer1 Setting Calculation

Hi all,
I am learning Timer1 for interrupt recently and i wish to understand about the calculation of TMR1H and TMR1L value.
I did my calculation by using MikroC calculator and refer to some online example, my calculation is as below:

PIC microcontroller: 16F628A
Interrupt time: 300ms
Clock Freq : 4MHz
Prescaler: 8

300ms * (4Mhz)/ (4*8) = 37500

65535 - 37500 = 28035

TMR1H = 28035/256 = 109
TMR1L = 15535 - (256*109) = 131

I had compare the answer with MikroC calculator and TMR1H is correct.
TMR1L should be 15536 - (256*109) = 132;
My question is, where did 15536 and 256 come from?
Some tutorial also mention that for prescaler, I need to calculate the division ration as 500000/8 = 62500.
But I am wondering what is this value for.
Can anyone explain these to me?
I got these value from others example but I have no clue on how to get it.

List Solutions Only

Aussie Susan
Super Member
• Total Posts : 3609
• Reward points : 0
• Joined: 2008/08/18 22:20:40
• Location: Melbourne, Australia
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/26 19:22:34 (permalink) ☄ Helpfulby alpha91 2016/09/28 05:22:04
+1 (1)
I'm not sure about the 15536 but I suspect that it is a type and should be 65536.
65536 = 2^16 and 256 = 2^8. Therefore this is simply a way of taking the 16 bit value and making it 2 8-bit values.
The other way to do this is to display the number as a hex value and the use the top and bottom 8 bits.
One thing to remember with the timer calculations for these processors is that the timer keeps on counting after it rolls over from 0xffff to 0x0000 and triggers the ISR. Time will be spent in saving the PC etc on the stack and calling the ISR and it is quite possible (especially for a fast clock to the timer) that it will count to 0x01 or beyond before your ISR code to reset the timer count is executed.
A common 'trick' is to precalculate the timer value (as you have done) and set the registers with that value as part of the initialisation, but in the ISR to subtract the "current" timer count form the precalculated value before putting it back into the timer count registers. This keeps the timer more accurate and stops it from slipping in time.
Susan
qhb
Superb Member
• Total Posts : 9998
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/26 20:24:03 (permalink) ☄ Helpfulby alpha91 2016/09/28 05:22:08
+1 (1)
Or just use Timer 2, which is much better for periodic interrupts, because it has the PR2 register, which takes care of the timer reload for you automatically.
Yes, you have less resolution on the period, but that's rarely a problem.

qhb
Superb Member
• Total Posts : 9998
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/26 20:45:34 (permalink) ☄ Helpfulby alpha91 2016/09/28 05:22:15
+2 (2)
Ahhh, I just spotted  that you only want an interrupt every 300ms.
The slowest you can run timer2 at is Fosc /4 /16 /16 = 3906.25 Hz
So the 8 bit counter rolls over at 3906.25 / 256 = 15.2588 Hz, for a delay of 65.5 ms.
If you set the prescaler to 16, postscaler to 10, and PR2 to 124,
then the timer will roll over at 4MHz /4 /16 /10 /125 = 50 Hz => 20ms
You could just count 15 of those interrupts to get exactly 300ms.
alpha91
New Member
• Total Posts : 3
• Reward points : 0
• Joined: 2014/09/06 07:21:38
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/28 05:41:14 (permalink)
0
[Let me make clear of something, as Susan said, the interrupt is triggered when the timer is roll over from FFFFh back to 0000h. So what I need to do is to know for 300ms of cycle, which point should I start and the answer is 37500, right?

Second question is that I dont really understand what is the function of TMR1H and TMR1L.
Base on tutorial, Timer1 is a 16 bit timer and consist of two 8 bit part which is TMR1H and TMR1L. But what is the value in these two register represent?
post edited by annapolis - 2016/09/28 06:49:55
jack@kksound
code tags!
• Total Posts : 3201
• Reward points : 0
• Joined: 2014/05/14 10:03:19
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/28 10:37:50 (permalink) ☄ Helpfulby alpha91 2016/09/29 08:57:43
+1 (1)
alpha91
[Let me make clear of something, as Susan said, the interrupt is triggered when the timer is roll over from FFFFh back to 0000h. So what I need to do is to know for 300ms of cycle, which point should I start and the answer is 37500, right?

Second question is that I dont really understand what is the function of TMR1H and TMR1L.
Base on tutorial, Timer1 is a 16 bit timer and consist of two 8 bit part which is TMR1H and TMR1L. But what is the value in these two register represent?

The 16 bit timer is divided into two 8 bit register to allow the 8 bit processor to read and write them (8 bits at a time). The THRxL register is the lower 8 bits (0 to 7) and the TMRxH register is the upper 8 bits (8 to 15).  The register must be written in the proper order as the upper byte is buffered and is loaded to the actual timer by a write to the lower byte SFR (load TMR1H then TMR1L). This is pretty basic stuff.
1and0
Access is Denied
• Total Posts : 9530
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/28 11:20:59 (permalink) ☄ Helpfulby alpha91 2016/09/29 08:57:46
+1 (1)
alpha91
[Let me make clear of something, as Susan said, the interrupt is triggered when the timer is roll over from FFFFh back to 0000h. So what I need to do is to know for 300ms of cycle, which point should I start and the answer is 37500, right?

Second question is that I dont really understand what is the function of TMR1H and TMR1L.
Base on tutorial, Timer1 is a 16 bit timer and consist of two 8 bit part which is TMR1H and TMR1L. But what is the value in these two register represent?

No. At Fosc of 4 MHz, 300 ms takes 300000 instruction cycles. A prescaler of 8 reduces it to 37500 counts. Timer interrupt occurs at rollover from 0xFFFF to 0x0000, so the Timer should start counting at 65536 - 37500 = 28036 to get 37500 counts at rollover. In hexadecimal 28036 = 0x6D84 where the upper byte 0x6D = 109 and the lower byte 0x84 = 132, and these are loaded into TMR1H and TMR1L, respectively. Of course, you will have to account for time elapsed.

post edited by 1and0 - 2016/09/28 11:31:42
alpha91
New Member
• Total Posts : 3
• Reward points : 0
• Joined: 2014/09/06 07:21:38
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/29 09:03:13 (permalink)
+1 (1)
1and0
No. At Fosc of 4 MHz, 300 ms takes 300000 instruction cycles. A prescaler of 8 reduces it to 37500 counts. Timer interrupt occurs at rollover from 0xFFFF to 0x0000, so the Timer should start counting at 65536 - 37500 = 28036 to get 37500 counts at rollover. In hexadecimal 28036 = 0x6D84 where the upper byte 0x6D = 109 and the lower byte 0x84 = 132, and these are loaded into TMR1H and TMR1L, respectively. Of course, you will have to account for time elapsed.

Now only i realize that the calculation for TMR1H and TMR1L is just convert the decimal of 28036 to hex.
I am very sorry and thank you very much.
I have better understanding now.
1and0
Access is Denied
• Total Posts : 9530
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: PIC Timer1 Setting Calculation 2016/09/29 09:36:38 (permalink)
+2 (2)
alpha91

Now only i realize that the calculation for TMR1H and TMR1L is just convert the decimal of 28036 to hex.

You can also look at it this way:

28036 / 256 = 109
28036 % 256 = 132

Sds
New Member
• Total Posts : 8
• Reward points : 0
• Joined: 2019/03/12 20:03:34
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/20 20:02:19 (permalink)
0
Im using pic 16f648a i need time delay of 1 minute i used timer 1 i can't get enough results i used for loop for delay the code get into the Loop and i can't count the input pulse timer should run in background and while timer run's i want to count the pulse
help me code without using for loop for timer 1 delay.
here is my code i used xc8 compiler

#include < stdio.h>
#include < stdlib.h>
#include < pic16f877a.h>

void Timer0Delay(void);
#define Dpin PORTBbits.RB4

int main(int argc, char** argv)
{
TRISBbits.TRISB4=0;
while(1)
{
Dpin^=1;
Timer0Delay();
}
return (EXIT_SUCCESS);
}

void Timer0Delay(void)
{
T1CON=0x01;
TMR1H=0x30;
TMR1L=0xD4;
for(int i=1;i< =100;i++)
{
T1CONbits.TMR1ON=1;
while(INTCONbits.TMR0IF==0);
T1CONbits.TMR1ON=0;
INTCONbits.TMR0IF=0;
}
}

post edited by Sds - 2019/03/20 20:05:26
qhb
Superb Member
• Total Posts : 9998
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/20 20:08:05 (permalink)
0
Start your own topic, and post your code with code tags around it
i.e. [CODE] before all the code, and [/CODE] after, but use lower case ("code" and "/code") within the square brackets to make them work.

Nearly there...
Aussie Susan
Super Member
• Total Posts : 3609
• Reward points : 0
• Joined: 2008/08/18 22:20:40
• Location: Melbourne, Australia
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/21 18:18:29 (permalink)
0
Also this type of question has been asked and answered a number of times before - perhaps not with the exact same MCU but the answer is nearly always the same regardless.
Try doing a little searching.
Susan
Sds
New Member
• Total Posts : 8
• Reward points : 0
• Joined: 2019/03/12 20:03:34
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/21 19:53:23 (permalink)
0
Im using pic 16f648a i need time delay of 1 minute i used timer 1 i can't get enough results i used for loop for delay the code get into the Loop and i can't count the input pulse timer should run in background and while timer run's i want to count the pulse
help me code without using for loop for timer 1 delay.
here is my code i used xc8 compiler

#include < stdio.h>
#include < stdlib.h>
#include < pic16f877a.h>

void Timer0Delay(void);
#define Dpin PORTBbits.RB4

int main(int argc, char** argv)
{
TRISBbits.TRISB4=0;
while(1)
{
Dpin^=1;
Timer0Delay();
}
return (EXIT_SUCCESS);
}

void Timer0Delay(void)
{
T1CON=0x01;
TMR1H=0x30;
TMR1L=0xD4;
for(int i=1;i< =100;i++)
{
T1CONbits.TMR1ON=1;
while(INTCONbits.TMR0IF==0);
T1CONbits.TMR1ON=0;
INTCONbits.TMR0IF=0;
}
}

qhb
Superb Member
• Total Posts : 9998
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/21 19:54:32 (permalink)
0
ok, so you ignored the request to start your own topic, and you ignored the request to use code tags.
I'm done here.

Nearly there...
jack@kksound
code tags!
• Total Posts : 3201
• Reward points : 0
• Joined: 2014/05/14 10:03:19
• Location: 0
• Status: offline
Re: PIC Timer1 Setting Calculation 2019/03/22 07:48:19 (permalink)
0
@Sds: OK I'll do this part for you:
`#include < stdio.h>#include < stdlib.h>#include < pic16f877a.h> void Timer0Delay(void); #define Dpin PORTBbits.RB4 int main(int argc, char** argv) {TRISBbits.TRISB4=0; while(1){Dpin^=1; Timer0Delay(); }return (EXIT_SUCCESS);}void Timer0Delay(void){ T1CON=0x01; TMR1H=0x30; TMR1L=0xD4; for(int i=1;i< =100;i++){ T1CONbits.TMR1ON=1; while(INTCONbits.TMR0IF==0); T1CONbits.TMR1ON=0; INTCONbits.TMR0IF=0; }}`

Do not include the processor specific include file (#include < pic16f877a.h> ). Change it to:
`#include <xc.h>`
that does all the needed processor includes for you, properly.
If you want to do a long period timer that runs while your program does other things then you use an interrupt triggered from the timer overflow, usually at some basic rate like every 1 mSec or 100 mSec. You use a counter to count those interrupts and set a flag when the total time has elapsed. In your main function you test the flag periodically and when set you know your time period has passed.
i leave the details for you, this has been discussed many times in the forums and other places.

Now do as suggested: learn to use code tags (see post#11) and do some research on the topic of timer interrupts.