Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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 ....

void chk_isr();

void TIMER0_ISR(void);


/*******ISR of 0x08 Location********/

#pragma code My_HiPrio_Int=0x08
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
#pragma interrupt chk_isr



/*****Check ISR Starts*******/

void chk_isr(void)
{
if( INTCONbits.TMR0IF==1)
TIMER0_ISR();
}


void main()
{
OSCCON=0X14;
RELAY=0X00;
check=0x0000;
LED_DIR=0;
LED=0;
T0CON=0x46; // timer0 8bit with prescalar for 256
TMR0L=0x86;
INTCONbits.GIE=1; // ENABLE all INT
INTCONbits.TMR0IF=0; // FLAG
INTCONbits.TMR0IE=1; // ENABLE TIMER INT
T0CONbits.TMR0ON=1; // TIMER ON

while(1)
{
LED=0;
}
}

void TIMER0_ISR(void)
{

INTCONbits.TMR0IF=0;
++check;
if(check==0x46)//70
LED=1;
Delay(); // Millisecond delay
}
 

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
 
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

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top