PIC18f4520 Timer0 Interrupt Problem

Status
Not open for further replies.

Amardeep

Junior Member level 3
Joined
Aug 16, 2011
Messages
28
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,449
Hi
I am Using PIC18f4520 Micro controller.
I want to Generate timer interrupt after every 1sec.
I am using internal clock for timer, Internal oscillator at 125khz, Timer0 in 16-bit Mode
please guide me how to calculate exact value for TH0 and TL0 register

amardeep.singh190@gmail.com
 

You could do it using Timer 0 in 8 bit mode.

125KHz / 4 = 31,250 /* Timer increment frequency. */

1 / 31250 = 0.000032 /* Increment time. */

0.00032 * 256 = 0.008192 /* Using 1:256 prescaler */

1 / 0.008192 = 122 /* counts for 1 second. */

256 - 122 = 134 /* Timer pre load value */

TMR0 = 134

If you pre-load the timer in the interrupt routine, you should have a 1 second interrupt.
Use mplab sim and the stopwatch to fine tune.
 
Thanks Alot .. I Got Every Point ...
 

Hello Btbass

when i simulate the code using MPLAB SIM the time of timer inerrupt is random not fix .. what should be the problem ?
here is attachment of my code View attachment TIMER.txt
 

This is My Code ....

 

Here is a free tool that you can just input the required interrupt frequency into and it will generate all the necessary code to initialise the timer registers for you.

**broken link removed**

Hope this is helpful.

/Pheetuz
 

recheck SCS1:SCS0: System Clock Select bits with data sheet
Because in your code T0CON=0x46;
That means you are using 1:128 prescale value(Fosc/4/128)
T0CS bit=0, Internal instruction cycle clock (CLKO).

The IDLEN bit (OSCCON<7>) controls CPU clocking, while the
SCS1:SCS0 bits (OSCCON<1:0>) select the clock
source.
Check POWER-MANAGED MODES with the datasheet if it is correct
 
Last edited:
You need to pre-load the timer in the interrupt routine so there is a count of 122 before the next interrupt.

Code:
void TIMER0_ISR(void)
  {
  INTCONbits.TMR0IF = 0; 

  TMR0L = 134;	/* Pre Load timer */ 
  
  ++check;
  
  if(check==0x46)//70
   LED=1;
 
  Delay(); // Millisecond delay
  }
 
thnx evry1 specially @btbass for the big help mate... my problem has been solved... thnx for giving me time n helping in solving the issue.. help appreciated
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…