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

18f4550 : delay and timer0 problem...

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

18f4550 : delay and timer0 problem...
PostPosted: Mon Feb 27, 2012 3:44 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:51 pm     Reply with quote

It takes time for the compiler to process the interrupt. This limits the
frequency at which interrupts can occur. Read these threads:
http://www.ccsinfo.com/forum/viewtopic.php?t=43399
http://www.ccsinfo.com/forum/viewtopic.php?t=29173
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Feb 27, 2012 5:21 pm     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 6:30 pm     Reply with quote

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.
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