Continue to Site

# What is the procedure for PIC timer0 to generate a 1 ms or 1 sec interrupt?

Status
Not open for further replies.

#### zahidkhan

##### Member level 2
hi all
what is the procedure for pic timer0 to generate a 1 ms or 1 sec interrupt.
regards...

timer0 calculator

Hi,

Its very easy...
First and most importnat is that you will know that an interupt on the timers will happen only when they reach the end, i.e. 0xFFFF +1.
So lets says this, you are using external OSC at 4MHz, and your times dosnt have any prescalar or postscale set. and you are using the OSC as timer.
So to get an interup, you first need to know how fast the timer will incramet. this is done by diving the OSC by 4 ( thats the MIPS of the PIC ) and then cahnge is to the time domain:
f = OSC/4 = 4,000,000 / 4 = 1,000,000 or 1MHz
To the time domain:
t = 1/f = 1/1,000,000 = 0.000001 Sec = 1uSec
So your timer will incrament every 1uSec. To get to 1mSec you will have to do:
1mSec/1uSec = 1000 clock cycles.
Now we need to load the timer with this settings, as i said before the timer will interupr only with it will reach the end, so we need to take the end and subtract the clock cycle that will pass, i.e.:
TMR0 = 0xFFFF - (time we want ) = 0xFFFF - 0x03E8 ( 1000 in Hex ) = 0xFC17
This is very easy to do with the PC calculator, that enable you to change from Dec. to Hex. in one button.
So the answer we have is 0xFC17. Load this value each time to your timer and then you will get an interupt every 1mSec.
Now you can play around and figure out, what is the max time you can get. And i you use the pre-scalare the your original clock wile have to divid even more. See the drawing in the data sheet from more info.

If you found my answer useful, click on the button that says Helped me. ( NO points will be taken from you! And you will get 3 Points)

Good luck.

Points: 2

Points: 2

### dibagay

Points: 2
pic timer0 calculator

bUT TMR0 can hold maximum of 256 i.e it is an 8-bit timer. Isnt it ?.how can we load it with 0xFC17.
thanks

pic timer0

You are right: TIMER0 is an 8-bit timer ..

Have a look at this:
Simple and fast system to get reliable timer periods from
a PIC controller.
This system (PIC assembler source is provided) gives a simple,
fast way to generate regular periods with a PIC at any clock speed.
Great for one second events like simple clocks. You can use any
crystal you have, 4.0 MHz, 12.137253 MHz, (ANY crystal) and ANY
prescaler value, and still get perfect one second timing.
It will generate reliable periods from milliseconds to many seconds,
with very fast code execution.
https://www.romanblack.com/one_sec.htm

Regards,
IanP

Points: 2
V
Points: 2

### eagle1109

Points: 2
timer0 pic

Here's the equation to calculate:

Period = (256 - TMR0)*(4/fosc)*(Prescaler)

For example, if Period = 1 ms and fosc = 4 MHz and using Prescaler 1:4

1ms = (256 - TMR0)(1us)(1)
TMR0 = 6 = 0x06

This is thevalue you must load to the timer 0 register. Remember to turn on the timer and clear the timer interrupt flag each time after the timer interrupt occurs. Careful with the global interrupt also because any unclear interrupt flag would trigger another interrupt. For 1 s of period, choose proper prescaler value which is suitable. Remember* timer 0 is only 8-bit. In order to have more precise timing, use 16 bit timer.

All the best.

Points: 2

Points: 2

### eagle1109

Points: 2
pic tmr0

Hi,

zahidkhan said:
bUT TMR0 can hold maximum of 256 i.e it is an 8-bit timer. Isnt it ?.how can we load it with 0xFC17.
thanks

You are correct is cases. As i didnt know what PIC you are using, i just explained how all the timers work. In the PIC18 the TMR0 has 8 or 16 bit, so either way, the idea was more importnant.

Also, contagiouseddie is very correct. I forgot to mention that, you have to reset the TMR0IF flag. You dont have to set to interupt if you dont want to, just wait until the TMR0IF will change. Each way has its advantages and disadvantages.

Good lcuk.

Points: 2
V
Points: 2

### eagle1109

Points: 2
pic timer 0

Thank U all . Now i am getting my concepts clear. but having some problems in the code to generate a clock .I am using Proton+.Here the code.

Code:
Device 16F84A

XTAL =  4.0

Declare Lcd_DTPin PortB.4
Declare Lcd_ENPin Portb.3
Declare Lcd_RSPin Portb.2

Symbol T0IF = INTCON.2          ' TMR0 Overflow Interrupt Flag
Symbol T0IE = INTCON.5          ' TMR0 Overflow Interrupt Enable
Symbol GIE  = INTCON.7          ' Global Interrupt Enable
Symbol PS0  = OPTION_REG.0      ' Prescaler Rate Select
Symbol PS1  = OPTION_REG.1      ' Prescaler Rate Select
Symbol PS2  = OPTION_REG.2      ' Prescaler Rate Select
Symbol PSA  = OPTION_REG.3      ' Prescaler Assignment
Symbol T0CS = OPTION_REG.5
DIM    MS  AS WORD

DIM SECOND  AS BYTE
DIM MINUTE  AS BYTE
DIM HOUR    AS BYTE

PS0 =  1
PS1 =  0
PS2 =  0
PSA =  0

T0CS = 0			'set the clock source for internal oscillator
T0IF = 0                        'clear the interrupt flag
T0IE = 1                        'enable tmr0 interrupt
GIE  = 1                        'enable Global interrupts

TMR0 = 6

On Interrupt goto InterruptServiceRoutine

GoTo Main                       'start of program, jumps straight to main

Disable                         'disable interrupts
InterruptServiceRoutine:

T0IF = 0                        'clear the interrupt flag

TMR0 = 6

INC MS
IF MS >= 999 THEN
MS = 0
INC SECOND
IF SECOND >= 59 THEN
SECOND = 0
INC MINUTE
IF MINUTE >= 59 THEN
MINUTE = 0
INC HOUR
ENDIF
ENDIF
ENDIF
Resume                          'return back to where the main code was running
'before the interrupt
Enable                          're enanble interupts

Main:

Print at 1,1, DEC HOUR," ",DEC MINUTE," ", DEC SECOND," ",DEC MS
GoTo Main

Re: timer0 pic

Here's the equation to calculate:

Period = (256 - TMR0)*(4/fosc)*(Prescaler)

For example, if Period = 1 ms and fosc = 4 MHz and using Prescaler 1:4

1ms = (256 - TMR0)(1us)(1)
TMR0 = 6 = 0x06

This is thevalue you must load to the timer 0 register. Remember to turn on the timer and clear the timer interrupt flag each time after the timer interrupt occurs. Careful with the global interrupt also because any unclear interrupt flag would trigger another interrupt. For 1 s of period, choose proper prescaler value which is suitable. Remember* timer 0 is only 8-bit. In order to have more precise timing, use 16 bit timer.

All the best.

what about for getting 50mSec timer for 20MHz crystal at a 256 prescaler

V
Points: 2
Re: timer0 pic

what about for getting 50mSec timer for 20MHz crystal at a 256 prescaler

This is can not be achieved using tmr0 and 20Mhz crystal directly. You could look at using additional variables handled by the interrupt routine.

You will need 250000 clock cycles at 20Mhz to get this timing period, you could look at using timer1 (16 bit)

Last edited:

V
Points: 2
Re: pic timer0

How to get a 2 MHz pulse with 30% duty cycle from 20 MHz crystal? Can anyone provide me the code?

thanks..

Re: pic timer0

This is a great thread, I am new to this especially C, working similar timing problems to apply to a pic18f4550. But I do have a question. I notice that the system oscillator was used in the above examples and that ran at 4MHz which is too high for the internal oscillator. Second, if one uses the system oscillator/clock for timers, does that not tie up the entire PIC?

I will have at least a "while loop" for counting in the timer ..........I THINK, have not ventured far enough into this yet. I can just visualize tying up everything by using the system clock. Am I missing something here?

Thanks--

Foggy

Status
Not open for further replies.