View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Trouble TMR0 with TMR1 |
Posted: Wed Feb 10, 2010 1:39 pm |
|
|
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
|
|
Posted: Wed Feb 10, 2010 1:54 pm |
|
|
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
|
|
Posted: Wed Feb 10, 2010 2:26 pm |
|
|
How I can solve this, please can you give a example? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 10, 2010 2:49 pm |
|
|
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
|
|
Posted: Wed Feb 10, 2010 4:15 pm |
|
|
PWM Mode .. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Feb 11, 2010 7:39 am |
|
|
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
|
|
Posted: Thu Feb 11, 2010 1:28 pm |
|
|
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
|
|
Posted: Thu Feb 11, 2010 2:48 pm |
|
|
Thanks.. |
|
|
|