View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
18f4550 : delay and timer0 problem... |
Posted: Mon Feb 27, 2012 3:44 pm |
|
|
Hi,
I have a demoboard with a pic 18f4550 and 20Mhz crystal. I try to do a 10us interruption with a timer... but the delay isn't good.
My code is like that :
For beginning :
Code: |
#include <18F4550.h>
#fuses NOWDT, HS, NOPROTECT, NOPUT
#use delay(clock=20M, crystal)
|
My interrupt function :
Code: |
#int_timer0
void interruption_timer0()
{
output_high(pin_e1);
delay_us(1);
output_low(pin_e1);
set_timer0(0);
}
|
My void main(void) :
Code: |
void main(void)
{
setup_timer_0(T0_INTERNAL|T0_DIV_1|T0_8_BIT);
set_timer0(0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
for(;;)
{
}
}
|
My result (with a scope) : the pulse on e1, 1.44us, interrupts every 63us...
I don't understand, because : (1/20M)*4*256=51.2us...
If someone have an idea...
Thanks in advance |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Feb 27, 2012 5:21 pm |
|
|
even if you COULD wish away the overhead of the ISR
- you will have precious little time to do any other processing of any use.
there are simply MUCH better, perfectly ACCURATE ways to make a repetitive pulse stream. I could do this with just a few - in fact damned few - 74HCT series IC's and a calculated master oscillator frequency.
who needs a PIC? what you are chasing is best done as a pure and really pretty simple bit of HARDWARE counters and gates alone.
OR a pretty dinky single chip
PLA - ( think GAL PAL FPGA etc etc )
let me share my favorite Larry Niven quote:
Quote: |
When all you have is a hammer, every problem needs to look like a nail.
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 27, 2012 6:30 pm |
|
|
You could fix the problem by making the PIC run faster. The 18F4550
has a PLL and can run at 48 MHz with your crystal.
Change your first 3 lines to this:
Code: |
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1
#use delay(clock=48M)
|
Then try your test again. |
|
![](templates/subSilver/images/spacer.gif) |
|