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

20Mhz XTAL and PIC18F4550

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



Joined: 11 Feb 2006
Posts: 12

View user's profile Send private message

20Mhz XTAL and PIC18F4550
PostPosted: Sat Jan 06, 2007 9:44 am     Reply with quote

Hi

This is my first stint using PIC18F4550 and after programming it, the proto board is behaving akwardly now, specially some timing issues seem to be there.

This program outputs PWM freq from 30Khz to 99Khz @ 50% duty cycle and blinks two LEDs initially.

Then, it waits for the user to press any of two buttons and increases/decreases the freq accordingly.

The thing is, the blinking of LEDs was fine initially, but suddenly the PIC seems to have gone crazy ! The LEDs blink irregularly...

I have setup 20Mhz XTAL with 1MOhm across it and two 22pf to ground

Do go through the code :

Code:

/*
PCB version 3.249
PCM version 3.249
PCH version 3.249
*/

#if defined(__PCM__)
#error   Not designed for PIC16

#elif defined(__PCH__)
   #include <18F4550.h>
   #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
//NOVREGEN USB voltage regulator disabled
//HS High speed Osc (> 4mhz)
//HSPLL High Speed Crystal/Resonator with PLL enabled
   #use delay(clock=20000000)
   //#use delay(clock=48000000)

   #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif

int8 deltaBaseFreq[69] = {5, 10, 15, 19, 24, 28, 31, 35, 38, 41, 45, 47, 50, 53, 55, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 77, 79, 80, 82, 83, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 102, 103, 104, 105, 106, 106, 107, 108, 108, 109, 110, 110, 111, 112, 112, 113, 113, 114, 114, 115, 115, 116};

int8 deltaBaseFreqIndex = 0;
int8 baseFreq = 30;
int8 basePR2 = 165;

int8 finalPR2;
int16 finalPWMDutyCyclePeriod;

int16 basePWMDutyCyclePeriod = 333;

int8 changed = 0;
int8 i = 0;

void main()
{
   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

   /* Change Freq up after some time */
   for(deltaBaseFreqIndex = 0; deltaBaseFreqIndex < 69; ++deltaBaseFreqIndex)
   {
      output_high(PIN_B5);
      output_low(PIN_B4);
      delay_ms(150);

      finalPR2 = basePR2 - deltaBaseFreq[deltaBaseFreqIndex];

      finalPWMDutyCyclePeriod = deltaBaseFreq[deltaBaseFreqIndex];
      finalPWMDutyCyclePeriod <<= 1;
      finalPWMDutyCyclePeriod = basePWMDutyCyclePeriod - finalPWMDutyCyclePeriod;

      setup_timer_2(T2_DIV_BY_1, finalPR2, 1);
      set_pwm1_duty(finalPWMDutyCyclePeriod);

      output_low(PIN_B5);
      output_high(PIN_B4);

      delay_ms(150);
   }

   setup_timer_2(T2_DIV_BY_1, basePR2, 1);
   set_pwm1_duty(basePWMDutyCyclePeriod);


   /* Glow both LEDs to signify readiness */
   output_high(PIN_B5);
   output_high(PIN_B4);
   delay_ms(2000);
   output_low(PIN_B5);
   output_low(PIN_B4);

   delay_ms(2000);

   output_high(PIN_B5);
   output_high(PIN_B4);
   delay_ms(2000);
   output_low(PIN_B5);
   output_low(PIN_B4);

   deltaBaseFreqIndex = 0;

   while(TRUE)
   {
      /* Increment freq if B2 is pressed */

      if(input(PIN_B2))
      {
         ++deltaBaseFreqIndex;
         //glow Inc Freq LED - PIN_B5
         output_high(PIN_B5);
         //delay_ms(2000);
      delay_ms(200);
         output_low(PIN_B5);

         changed = 1;
      }
      else if(input(PIN_B1))
      {
         --deltaBaseFreqIndex;
         //glow Dec Freq LED - PIN_B4
         output_high(PIN_B4);

         delay_ms(200);

         output_low(PIN_B4);

         changed = 1;
      }

      if(changed)
      {
         finalPR2 = basePR2 - deltaBaseFreq[deltaBaseFreqIndex];

         finalPWMDutyCyclePeriod = deltaBaseFreq[deltaBaseFreqIndex];
         finalPWMDutyCyclePeriod <<= 1;
         finalPWMDutyCyclePeriod = basePWMDutyCyclePeriod - finalPWMDutyCyclePeriod;

         setup_timer_2(T2_DIV_BY_1, finalPR2, 1);
         set_pwm1_duty(finalPWMDutyCyclePeriod);

         changed = 0;
      }
  }
}

Kamal



Joined: 11 Feb 2006
Posts: 12

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 1:57 pm     Reply with quote

What is the difference between HS and HSPLL ??

Is it that the PIC4550 does not support XTALs greater than 10Mhz .. will it support a 20Mhz XTAL ?

Also, as you can see, I am changing the PWM freq very fast (every 300ms)... is this reason for the PIC to behave erratically ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 2:22 pm     Reply with quote

Here is a test program that runs the 18F4550 at 20 MHz, with a 20 MHz
crystal. I tested this with PCH vs. 3.249 on a PicDem2-Plus board.
Code:

#include <18F4550.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)

//====================================
void main()
{

// Blink an LED on pin B0.
while(1)
  {
   output_high(PIN_B0);   
   delay_ms(500);
   output_low(PIN_B0);
   delay_ms(500);
  }

}
Ttelmah
Guest







PostPosted: Sun Jan 07, 2007 3:51 pm     Reply with quote

Kamal wrote:
What is the difference between HS and HSPLL ??

Is it that the PIC4550 does not support XTALs greater than 10Mhz .. will it support a 20Mhz XTAL ?

Also, as you can see, I am changing the PWM freq very fast (every 300ms)... is this reason for the PIC to behave erratically ?

The fuses on these chips are terrifying!.
HSPLL, has a different meaning, than HSPLL on most other chips. When selected, it means 'use the USB PLL circuit'. This takes a 4MHz frequency, and multiplies it to generate 96MHz. Now the frequency fed into this circuit is determined by the PLL prescaler, which is set by the 'PLL5' fuse, to be /5. So these two fuses together, mean take the incoming clock, divide it by 5, then multiply it up to give 96MHz!.
Now the USB frequency, is then divided by the CPUDIV fuse, to generate the clock fed to the CPU. The value for this fuse is divided by a further 2, when used with the USB clock. So HSPLL, PLL5, CPUDIV1, gives a CPU, at 48Mhz. Table 2-3 of the data sheet, the fourth value down for the 20Mhz input option.
So, the clock frequency is wrong for the fuses as given (should be 48000000), but the fuses as given, are OK for the chip.

Best Wishes
Kamal



Joined: 11 Feb 2006
Posts: 12

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 7:57 pm     Reply with quote

PCM programmer wrote:
Here is a test program that runs the 18F4550 at 20 MHz, with a 20 MHz
crystal. I tested this with PCH vs. 3.249 on a PicDem2-Plus board.
Code:

#include <18F4550.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
...


Allright .. the number of fuses you used here are not terrifying and you used the HS fuse instead of the HSPLL fuse...

I don;t want to use the USB Peripheral inside 4550, so do I have to use the HSPLL and related fuses or will simply HS do ?

I just wanna drive an HBridge (so need PWM for that), read a few switches and use the ADC.

What fuses I really need ?

Someone should write a document on all these fuses... the Microchip datasheet also is terrifying regarding these fuses !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 8:23 pm     Reply with quote

Quote:

I don't want to use the USB Peripheral inside 4550.
I just wanna drive an HBridge (so need PWM for that), read a few switches and use the ADC.

It looks like you just want to use this PIC as if it's an 18F452.
In that case, use the fuses as shown in my test program.
Kamal



Joined: 11 Feb 2006
Posts: 12

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 8:35 pm     Reply with quote

PCM programmer wrote:

It looks like you just want to use this PIC as if it's an 18F452.
In that case, use the fuses as shown in my test program.


Ok.. so the 18F452 is 4550 sans the USB Peripheral ?

Is the PLL only for USB ? If I want to run the 4550 at 48Mhz can I do away with the XTAL and use the PLL ?

How stable will it's performance be ?
Why at all use the XTAL if the PLL is equally effective, if I do not have any specific reason to run at a lower freq, can I just use the PLL and run at 48Mhz without worrying about stability and reliability ?

Thanks PCM programmer for clearing my previous doubts !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 07, 2007 9:10 pm     Reply with quote

Your original question was how to run at 20 MHz. Now you want to
run with the PLL. You'll have to wait until Monday for that answer,
when I can test it in hardware.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 11:53 am     Reply with quote

Actually, Ttelmah told you what you needed to do. I didn't look at his
post closely until this morning. You can figure out how to setup the
oscillator by looking at the block diagram of the clock circuit in the
18F4550 data sheet. This is on page 24 (page 26 in the Acrobat reader).
Here is a test program. I tested it on a PicDem2-Plus board with PCH
vs. 3.249. This is with a 20 MHz crystal.

Code:
#include <18F4550.h>
#fuses HSPLL, PLL5, CPUDIV1, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=48000000)

//=================================
void main()
{

// Blink an LED on Pin B0.
while(1)
  {
   output_high(PIN_B0);
   delay_ms(500);
   output_low(PIN_B0);
   delay_ms(500);
  }

}
Guest








PostPosted: Mon Jan 08, 2007 7:54 pm     Reply with quote

PCM programmer wrote:
Your original question was how to run at 20 MHz. Now you want to
run with the PLL. You'll have to wait until Monday for that answer,
when I can test it in hardware.


PCM programmer,

First, I thank you for solving my original timing problem. I am getting excellent performance right now.

I asked the question on PLL just out of curiosity - not a neccessity. It's is not my primary objective.

What I want to know is if the PLL is as stable as the crystal in terms of timing.

If so, I don't think it makes sense to hook up a Crystal in the first place, if the PLL is as stable as a XTAL, since it costs money and takes up board space too.

That's what I need clarification on.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 09, 2007 12:25 pm     Reply with quote

Quote:
What I want to know is if the PLL is as stable as the crystal in terms of timing.

Download the 18F4550 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39632c.pdf
Go to page 381 (page 383 in the Acrobat Reader).
Look at this section:
TABLE 28-9: PLL CLOCK TIMING SPECIFICATIONS
It has the jitter specifications for the PLL.

I can't find any specs on jitter when using a crystal in HS mode, etc.
I searched the whole Microchip website (using Google).

Quote:

If so, I don't think it makes sense to hook up a Crystal in the first place,
if the PLL is as stable as a XTAL, since it costs money and takes up board
space too.

The PLL requires an external crystal or oscillator. Look at page 24
of the data sheet (page 26 in the Acrobat reader), at this diagram:
FIGURE 2.1: PIC18F2455/2550/4455/4550 CLOCK DIAGRAM
It shows that the 96 MHz PLL source comes only from the "Primary
oscillator" pins. You must connect an external crystal or oscillator
to those pins, to provide the PLL a clock signal.

I suspect that you are confusing the Internal RC Oscillator with the PLL.
These are completely different sections in the Clock Diagram.
Go to page 383 in the Acrobat Reader and look at this table for
specifications of the Internal oscillator:
TABLE 28-10: AC CHARACTERISTICS: INTERNAL RC ACCURACY

For more on the Internal RC Oscillator, go to page 29 in the
Acrobat reader and read this section:
2.2.5.3 Internal Oscillator Output Frequency and Drift
Ttelmah
Guest







PostPosted: Tue Jan 09, 2007 4:12 pm     Reply with quote

PCM programmer hs already covered it pretty thoroughly. The key line, is the end of the first paragraph in section 2.2.5 in the data sheet:
"If the USB peripheral is not used, the internal oscillator may eliminate the need for external oscillator circuits on the OSC1 and/or the OSC2 pins". Note the key part about the USB. The internal oscillator, is _not_ accurate enough to meet the USB requirements, and hence it is not routed to this part of the circuit...
The clock timings for USB, require the device clocks, to be within 0.05%. You may well be suprised by how many devices on the market actually fail in this, when they are taken outside the 'room temperature' enviroment!. The internal oscillator typically manages +/-1%, but in some cases may well be well outside this. Not even close to good enough...
Fortunately, the clock jitter in the PLL, does not effect the actual timing accuracy, and once divided down to feed the USB circuitry, causes no problems.

Best Wishes
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