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.

Why unwanted delay is introuduced in Timer1 of PIC16F877A?

Status
Not open for further replies.

umery2k75

Advanced Member level 1
Advanced Member level 1
Joined
Apr 19, 2006
Messages
434
Helped
42
Reputation
82
Reaction score
16
Trophy points
1,298
Location
Pakistan
Activity points
5,748
pic 16f877a timer1 c program

I'm happy with the code, the code works fine and I know the simple logical working of interrupts and how to set the values to get desired response, but the thing which bothers me is that, why Timer 1 starts it's operation very late? I have looked for this thing in it's data sheet too, but I couldn't find anything. If you people know about this, then tell me, but I don't hope that this problem can be answered. I think in this way, because I couldn't find anything on the PIC16F877A datasheet that has a factor which has its effect on Timer1 operation.


I have two codes one that was made around Timer0 8-bit and other was built around Timer1 16-bit.

TIMER 0(8-BIT)
==========

Code:
#include<16F877A.H>
#include<string.h>
#use delay (clock=03579545)
#fuses   NODEBUG,XT,NOWDT,PUT,NOPROTECT,NOLVP
#use rs232 (baud=2400,xmit=PIN_C6,rcv=PIN_C7)
#define INTS_PER_SECOND 6
int8  int_counts;
int8 ledstate=0;
int8 Hun_Milli_Seconds_Count=0;

#int_rtcc
void clock_isr()
{
   if(--int_counts==0)
      {
         ++Hun_Milli_Seconds_Count;
         int_counts=INTS_PER_SECOND;
      }
}

void main()
{
      set_tris_b(0x00);
      output_low(PIN_B0);
      setup_counters(RTCC_INTERNAL,RTCC_DIV_64);
      enable_interrupts(INT_RTCC);
      enable_interrupts(GLOBAL);
             while(1)
                  {
                              if(Hun_Milli_Seconds_Count==0)   //Makes up approx. a sec
                              {
                                  output_high(PIN_B0);
                                 }

                           if(Hun_Milli_Seconds_Count==1)   //Makes up approx. a sec
                             {
                                  output_low(PIN_B0);
                                   }

                          if(Hun_Milli_Seconds_Count==2)   //Makes up approx. a sec

                                 Hun_Milli_Seconds_Count=0;
                  }

}

As soon as the PIC is power on, I can see the output of PIN_B0 attached by LED turning ON/OFF immediately afterpowering up. I'm happy with it's output.

TIMER 1(16-BIT)
==========

Code:
#include<16F877A.H>
#include<string.h>
#use delay (clock=03579545)
#fuses   NODEBUG,XT,NOWDT,NOPUT,NOPROTECT,NOLVP
#use rs232 (baud=2400,xmit=PIN_C6,rcv=PIN_C7)
#define INTS_PER_SECOND 1
int8  int_counts;
int8 ledstate=0;
int8  Hun_Milli_Seconds_Count=0;

#int_TIMER1
void clock_isr()
{
   if(--int_counts==0)
      {
         ++Hun_Milli_Seconds_Count;
         int_counts=INTS_PER_SECOND;

      }
}

void main()
{

      set_tris_b(0x00);
      output_low(PIN_B0);

      set_timer1(0);
      setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // setup interrupts
      enable_interrupts(INT_TIMER1);
      enable_interrupts(GLOBAL);

             while(1)
                  {
                              if(Hun_Milli_Seconds_Count==0)   //Makes up approx. a sec
                              {
                                  output_high(PIN_B0);
                                 }

                           if(Hun_Milli_Seconds_Count==1)   //Makes up approx. a sec
                             {
                                  output_low(PIN_B0);
                                   }

                          if(Hun_Milli_Seconds_Count==2)   //Makes up approx. a sec

                                 Hun_Milli_Seconds_Count=0;
                  }

}

When PIC is powered on, The ouput PIN_B0 having LED on it, turns ON and it remain On for around 63-64 seconds.After this begins its normal LED blinking work,I don't understand why the interrupt response is getting started too late, why delay is there?

Timer1 seems like a old Diesel engine to me, which is kept at some place in North Pole, which is trying to start on in intense cold. :D

The time is precise, it takes around somewhere 63-64 seconds to turn on



Added after 1 minutes:

Please don't care about the values in the diagram like 109mS and 18.30mS. I made that diagram for some other work.
 

timer1 pic16f877a

Hi,
Assign Int_Count values of 6 for first routine and 1 for second routine in the beginning of the program.

Regards,
Laktronics
 

timer1 coding for pic16f877a

Assign Int_Count values of 6 for first routine and 1 for second routine in the beginning of the program.

I didn't get your point, what do you mean by first routine and second routine?

If I assign this:

Code:
int8  int_counts=6;

then to what variable I should assign 1?
 

delay pic16f877a

Hi,
You assign 1 for the same variable in the 16bit timer routine.
Regards,
Laktronics
 

    umery2k75

    Points: 2
    Helpful Answer Positive Rating
pic16f877 timer1 interrupt

Hi Laktronics,
You know what, the thing which you told me to assign variable int counts=1 just worked perfectly in Timer1 and now my code is working great in Timer1.The first thing I want to say is a BIG THANKS to you and secondly I was thinking what would I do, if you didn't read my post? you wouldn't have replied me and I might have not yet solve this problem.The way you approach this problem was great! Approaching a problem is an art I must say.
According to mathematician George Polya there is a best way to approach a problem and he wrote three books on it :D

Added after 6 minutes:

First thing to notice in my program was that the variable int_counts was not initialized to any constant,so it could have any unknown value in it.

In Timer0 program the variable int_counts must have smaller value like 1,5,etc. So when I runned this program.I just saw the LED working ON/OFF right instantly.

In Timer1 program the variable int_counts must have some big value like 50,60,etc. So when I runned that program I saw the huge delay, then my timer started working properly.
 

timer1 pic16f877

Hi,
Forgetting to initialise the variables is a common problem with C and many times it will not show up since C assigns zero value to those variables. In your case, since you are subtracting first and checking for zero, the variable int_counts becomes INT8 Max value (255) after subtraction and you need so many interrupts before you increment 100_msec register, all along the LED remains ON.

Also, I feel that the LED flashing rate is not same in both the timer cases. If so, you can make it same by using int_counts_persec to 32 in timer 0 program, unless I have missed someyhing else in the code.

Regards,
Laktronics
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top