|
|
View previous topic :: View next topic |
Author |
Message |
sergiofil
Joined: 27 Oct 2011 Posts: 3 Location: Portugal
|
timer1 count problem pic18f4580 |
Posted: Thu Oct 27, 2011 4:52 am |
|
|
Hi all,
I'm programming timer1 on pic18f4580.
I want to make timer1 overflow every 10 usec. To analyze that, I use a led, and if led blink every 5 sec I know that it's correct.
So, I calculate values for timer, but led blinks every 30 sec, not 5 sec as I expect.
I also try make timer1 overflow every 104.896ms (as in the example), and it works this away.
So, can anyone help me to find what I'm doing wrong !
My code is:
Code: |
#include <18F4580.h>
#fuses NOWDT, NOFCMEN, INTRC_IO, NOPUT, NOPROTECT, NOIESO, NOBROWNOUT, MCLR, NOLPT1OSC, PBADEN, STVREN, WDT32768, NOXINST, NOCPD, NOWRT
#use delay(clock=8M)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int32 countTimer;
#INT_TIMER1
void isr_timer1()
{
output_toggle(PIN_A1);
if(countTimer < (500000))
countTimer ++;
else
{
output_toggle(PIN_A0);
countTimer = 0;
}
set_timer1(65531);
}
void main()
{
countTimer = 0;
set_tris_a(0b00000000);
output_bit(PIN_A0, 0);
output_bit(PIN_A1, 0);
enable_interrupts(INT_TIMER1); // Enable Timer 1 Interrupt
enable_interrupts (GLOBAL) ;
//arranca o timer1
set_timer1(65531);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
/* timer1 calculate:
8MHz / 4 = 2MHz
2MHz / 4 = 500000 Hz
1 / 500000Hz = 0.2 useg -> timer1 will increment every 0.2 useg
I want overflow every 10 useg: 10 useg / 2 useg = 5
65536 - 5 = 65531
To blink a led every 5 seconds:
5 seg / 0.00001 = 500000
when my countTimer count to 500000, i toggle output to blink led.
*/
while(TRUE)
{
}
}
|
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Thu Oct 27, 2011 5:03 am |
|
|
No hope......
You can't execute an interrupt that fast especially at 8MHz.
Calling an interrupt takes typically about 60 instruction times (call and return). Then in your interrupt you are comparing a 32bit value (about a dozen instructions), then incrementing a 32bit value (about 15 instructions), and setting the timer to a value (probably another 4 instructions). So total of about 90+ instructions. At 8Mhz, each instruction takes 0.5uSec, so I'd guess about 45uSec _minimum_ for successive interrupts. When you set the timer, about 60 instructions have already executed, so the time will be this much long.
Seriously, if you want to do something this fast, your processor will have to do it in the main loop (not using interrupts), and do nothing else. You are trying to push performance beyond anything the chip is capable of.
Best Wishes |
|
|
pedrocnp
Joined: 26 Mar 2011 Posts: 1 Location: Portugal
|
|
Posted: Thu Oct 27, 2011 7:00 am |
|
|
Hi Ttelmah
This happens only in a timer1? I did experience with the timer 2 and resulted, why?
best regards
Ttelmah wrote: | No hope......
You can't execute an interrupt that fast especially at 8MHz.
Calling an interrupt takes typically about 60 instruction times (call and return). Then in your interrupt you are comparing a 32bit value (about a dozen instructions), then incrementing a 32bit value (about 15 instructions), and setting the timer to a value (probably another 4 instructions). So total of about 90+ instructions. At 8Mhz, each instruction takes 0.5uSec, so I'd guess about 45uSec _minimum_ for successive interrupts. When you set the timer, about 60 instructions have already executed, so the time will be this much long.
Seriously, if you want to do something this fast, your processor will have to do it in the main loop (not using interrupts), and do nothing else. You are trying to push performance beyond anything the chip is capable of.
Best Wishes |
|
|
|
|
|
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
|