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.

Timer for mikrco pro compiler using pic 18f4550

Status
Not open for further replies.

khatus

Member level 3
Joined
Oct 7, 2017
Messages
56
Helped
1
Reputation
2
Reaction score
0
Trophy points
6
Activity points
441
This is the code for generating a delay of 1ms by using PIC18F4550 Timer0 Interrupt Service Routine.My question is
why these two lines
Code:
TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
TMR0L    = 0xB2;
have been used once inside the function void Interrupt()
and again inside the function void InitTimer0()?
I know these are value to be loaded to the timer. But What is the reason for writing in two places?
Code:
TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
TMR0L    = 0xB2;
This line would have been written only once in the function.
Here is y complete code
Code:
//**Generating a delay of 1ms by using PIC18F2550 Timer0 Interrupt Service Routine**//

#define Pulse LATB

void InitTimer0();

void main()
{
    OSCCON = 0b00000000; /* Configure oscillator frequency to 20MHz */
    TRISB = 0x00;        /* Set as output Port */
    Pulse = 0xff;        /* Send high on PortB */
    InitTimer0();

    while(1);

}
/***************Interrupt Service Routine for Timer0******************/
void Interrupt()
{
 if (TMR0IF_bit)
  {

    TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
    TMR0L    = 0xB2;
    TMR0IF_bit =0;    /* Make Timer0 Overflow Flag to '0' */

  //Enter your code here
    Pulse =~ Pulse;   /* Toggle PortB at of 500 Hz */

   }
}

void InitTimer0()
{
 /* Enable 16-bit TMR0 register,pre-scaler=64,External clock=20MHz,timer OFF */

    T0CON = 0b00000101;   /*Configure TIMER0 control register*/

    TMR0H = 0xFF;         /* Load Count for generating delay of 1ms */
    TMR0L = 0xB2;

    GIE_bit =1;        /* Enable Global Interrupt */
    PEIE_bit =1;       /* Enable Peripheral Interrupt */
    TMR0IE_bit =1;     /* Enable Timer0 Overflow Interrupt */
    TMR0IF_bit = 0;    /* clear Timer0 interrupt flag bit */

    TMR0ON_bit = 1;    /* Turn ON Timer0 */
}
 

You need to initialise the timer and start it or you will never get an interrupt.
The interrupt handler resets the timer registers so that it will generate the next one.
 

So, once we are inside the isr this means timer 0 have overflowed and become zero so we gotta load it again with zero.
 

    G4BCH

    Points: 2
    Helpful Answer Positive Rating
Hi,

Some thoughts...

with "0xFFB2" .. this means you just use 78 ticks. So an 8 bit timer may be sufficient.

or use a lower value prescaler to get more accurate timing.

Both will have no influence on processing power.


Klaus
 

One of the little tricks you may need when dealing with these timers that don't have limit registers is that the timer will keep on 'ticking' while the ISR is being called, especially if you have low pre-scalar values. If you need really precise timing, then you will find that you need to take these extra 'ticks' into account. (By the way they occur because the hardware needs to respond to the interrupt, the ISR preamble code needs to save registers etc before it starts to execute your code, your test to see which is the trigger interrupt, and then there are the times needed for the instructions that reload the TMRx registers themselves).
What you can do is to use code such as:
Code:
TMR0H = <your value>;
TMR0L = <your value> - TMR0L;
This also allows you to not be quite so strict about resetting the timer as the first instructions.
Just a suggestion...
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top