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 CCS Technical Support

18F4550 delay_ms problem

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



Joined: 07 Jan 2018
Posts: 2
Location: Turkey

View user's profile Send private message

18F4550 delay_ms problem
PostPosted: Sun Jan 07, 2018 1:18 am     Reply with quote

Hello everybody, This is my first question in this form.
I am using 18F4550 at 48Mhz and program to have 1 second delay between steering leds. But actual delay seems 5 seconds. My code is as follows:
Code:

#fuses HSPLL, PLL5,CPUDIV1, NOWDT, NOLVP, NOPROTECT, VREGEN
#use delay(clock=48000000)

void main()
{
   int i=0b00000001;
   int tersDon=0;

   while(TRUE)
   {
      output_d(i);
      if(tersDon==0)
      {
         i=i<<1;
         if(i==0b10000000)
         {
            tersDon=1;
         }
         }
      else
      {
         i=i>>1;
         if(i==0b0000001)
         {
            tersDon=0;
         }
      }
      delay_ms(1000);
   }
}

HSPLL, PLL5,CPUDIV1 fuses should be enough to operate at 48MHz.

Later I am going to change this code to communicate with pc through usb, but with this timing it won't work.

Thanks for your help
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sun Jan 07, 2018 1:56 am     Reply with quote

Almost certainly implies your crystal is not oscillating.

The settings you have are for a 20MHz crystal. What crystal have you got?. What loading capacitors have you got?. How is it wired to the PIC?.

If the crystal doesn't start, the default unless you have NOFCMEN, is to 'drop back' to the internal oscillator at 8MHz. So you would get about 6 seconds.

The 4550, is quite fussy about it's crystals (more so than many later PIC's).
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 07, 2018 6:12 am     Reply with quote

As Mr. T. say it's probably your xtal hardware. I used 4MHz xtals for my 4550 projects and never had a any fail to start, even on white breadboards.
I'm pretty sure 4MHz will allow USB operation as well, but best to check the 'clock configuration' diagram in the datasheet as it's been years since I used them.

Jay
mgunesen



Joined: 07 Jan 2018
Posts: 2
Location: Turkey

View user's profile Send private message

PostPosted: Sun Jan 07, 2018 3:20 pm     Reply with quote

temtronic wrote:
As Mr. T. say it's probably your xtal hardware. I used 4MHz xtals for my 4550 projects and never had a any fail to start, even on white breadboards.
I'm pretty sure 4MHz will allow USB operation as well, but best to check the 'clock configuration' diagram in the datasheet as it's been years since I used them.

Jay


Problem solved, I was using test board and it was set to 4 Mhz. I changed to 20MHz and every thing is fine. Sorry for the simple mistake.

What is your advice for the usb operation, 20Mhz or 4Mhz?
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sun Jan 07, 2018 4:01 pm     Reply with quote

Honestly 4 or 8MHz.

The lower the master crystal, the less problems with RF being generated by the oscillator, and slightly lower power. 8MHz has the (fractional) advantage of using the divider, which absolutely ensures a 50:50 mark space to the USB PLL. However provided the crystal is not overdriven, 4MHz is fine.

The only reason you use a higher oscillator like 20MHz, is if you actually want to run the CPU physically at this frequency, or something else on the board needs this.

Otherwise PLL1, and you can run the CPU at 48, 24, 16Mhz etc., depending on how fast you actually want to clock. Smile
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 07, 2018 4:06 pm     Reply with quote

It doesn't matter, providing the xtal/caps WORK reliably. I still have LOTS of 4MHz from 16C84 projects from 2 decades ago, so I try to use them.
As for USB operation, I gave up using the internal USB version PICs.
re: software, the USB driver code took +- 1/3 of code space !
re: hardware,when you look at the cost of necessary parts and compare to a premade USB<>TTL module, the module only cost $1 CDN more.
Some of the benefits of the module are
1) can be used with ANY PIC
2) NO software driver required, less code to deal with
3) NO code space for USB driver
4) NO PCB problems, like oops D+ is D-, D- is D+ !
5) Reliablity, I've used them for years, no problems
6) cost savings. No bad PCB, No code issue, NO R&D time wasted !

yes, it takes a little more space in the product BUT that easily offsets the pain of making a second or third PCB due to layout issues. Also ANY PIC can be used, so it's easy to upgrade or migrate to another PIC.

just some points to think about.

Jay
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