CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Trouble TMR0 with TMR1

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Trouble TMR0 with TMR1
PostPosted: Wed Feb 10, 2010 1:39 pm     Reply with quote

Hi, I am use a pic18f452 to 20MHz and I need to generate two signals of different frequencies, for this I am using the interruption of the TMR0 and TMR1, when only one of them works (TMR0 or TMR1) and the other is DISABLED everything works OK but when both are enabled (TMR0 and TMR1) only work TMR0, someone could tell me what my mistake??

Here is my code:
Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)

int16 demo;
int i,j;

#define LED       PIN_E1
#define OSC       PIN_E2

#define ON           output_high
#define OFF          output_low

#INT_TIMER0
void tempo()
   {
   set_timer0(65500);
   if (i == 0){
      i = 1;
      ON(OSC);
      }
   else {
    OFF(OSC);
    i = 0;
   }   
}

#INT_TIMER1
void temp1()
   {
      set_timer1(26462);
      if (j == 0){
      j = 1;
      ON(LED);
      }
   else {
    OFF(LED);
    j = 0;
   }   
}

void main(){

    setup_timer_0(RTCC_INTERNAL | RTCC_DIV_2);
    enable_interrupts(INT_TIMER0);

    setup_timer_1(T1_INTERNAL | T1_DIV_BY_2);
    enable_interrupts(INT_TIMER1);
    set_timer1(26462);            

    enable_interrupts(GLOBAL);

    OFF(LED);
    OFF(OSC);

    while(true);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 10, 2010 1:54 pm     Reply with quote

Do some math.

Timer0 is a 16-bit counter (in this PIC). You have set it to a count
of 65500 which is very close to the rollover value (the end count).
How much time does it take to count up and generate an interrupt ?
Here is the math:

65536 - 65500 = 36 clocks.

One clock is one instruction cycle. But, the PIC must execute the
CCS interrupt handler code. This is 40 or 50 instruction cycles.
While the first Timer0 interrupt is being handled, another one will
occur. So as soon as interrupt handler exits, it will immediately
jump back into the interrupt handler again, to process the Timer0
interrupt. And so on.

Because the #int_timer0 routine is placed first in your program, it
will be the first to be tested in the CCS interrupt handler code.
Because there is always a pending interrupt, the handler will always
jump to the Timer0 routine. The Timer1 routine will never be executed,
even though it has a pending interrupt.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Wed Feb 10, 2010 2:26 pm     Reply with quote

How I can solve this, please can you give a example?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 10, 2010 2:49 pm     Reply with quote

Think. You want to generate a signal with a frequency of (I guess)
about 20 KHz. What hardware module inside the PIC has the ability
to do that ?
Guest








PostPosted: Wed Feb 10, 2010 4:15 pm     Reply with quote

Confused PWM Mode ..
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Thu Feb 11, 2010 7:39 am     Reply with quote

Quote:
You want to generate a signal with a frequency of (I guess)
about 20 KHz

Hi PCM programmer, No, I do not want to generate a signal with a frequency of 20KHz, I want to know how can use the Interrupt of TMR0 and TMR1 at the same time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 11, 2010 1:28 pm     Reply with quote

The CCS interrupt handler takes roughly 40 to 50 instruction cycles
to execute. That does not include the additional time required by your
code in the #int_timer0 routine. That will add several more instruction
cycles. You are trying to have an #int_timer0 interrupt every 36
instruction cycles, but the PIC can only handle an interrupt at a rate
of once every (say) 75 instruction cycles, or greater.

Your interrupt rate is too fast. The PIC can't handle it.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Thu Feb 11, 2010 2:48 pm     Reply with quote

Thanks..
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group