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

PIC18F4550 & crystals, which to use?

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



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PIC18F4550 & crystals, which to use?
PostPosted: Wed Dec 06, 2006 9:24 am     Reply with quote

Hi There,
I've had a very hard look around the forums and datasheets but I can't seem to understand exactly what crystal/setup the 4550 requires.

I even posted on the microchip support web tickets. I got this response:

Quote:
When the PIC18F4550 is used for USB connectivity, it must have either a 6 MHz or 48 MHz clock for USB operation, depending on whether Low-Speed or Full-Speed mode is being used. This may require some forethought in selecting an oscillator frequency and programming the device.

Please see the TABLE 2-3: OSCILLATOR CONFIGURATION OPTIONS FOR USB OPERATION from datasheet for standard Input Oscillator Frequency which you need to use achieve suitable USB clock speeds.

USB Low Speed Operation
The USB clock for Low-Speed mode is derived from the primary oscillator chain and not directly from the PLL. It is divided by 4 to produce the actual 6 MHz clock. Because of this, the microcontroller can only use a clock frequency of 24 MHz when the USB module is active and the controller clock source is one of the primary oscillator modes (XT, HS or EC, with or without the PLL).


In the datasheet above the aformentioned table it talks about ceramic resonators of 4,8 & 16Mhz and crystal oscillators of 4,8 & 20Mhz. I'm a little confused with that.

Does anyone have a clear and defined procedure of how I can set the fuses in the CCS code to use a crystal for USB?

The crystals I have in my bits box are:
32Mhz, 25Mhz, 12Mhz, 10Mhz as well as a few others with fractional values eg. 28.322Mhz

many thanks
Ttelmah
Guest







PostPosted: Wed Dec 06, 2006 10:21 am     Reply with quote

The 4550, in common with a couple of other similar PICs, has a very complex oscillator layout. It needs 96MHz present inside the chip to run the USB (at full speed). It has a PLL, which proivides *24 multiplication from a 4MHz source. In front of this, it then has a programmable divider, offering /1, /2, /3, /4, /5, /6, /10, and /12 divisions. The input to this, can therefore be 4, 8, 12, 20, 24, 40, or 48Mhz. Then in front of this, is the *4 PLL, allowing operation from 1, 2, 3, 4, 5, 6, 10, 12, 20 24, 40 or 48Mhz sources. _With any of these sources in use, the internal clock can be set to the 4MHz needed for the high speed USB!..._. To run the USB in low speed mode, 6MHz is required internally instead, and another similar set of possible crystal frequencies exist.
Now, taking your 12MHz crystal, this can be used with fuses as follows:

HS (it is above 4MHz)
PLL3 (divide the input by 3, to give 4MHz)

At this point, 4Mhz is present feeding the USB circuitry, and 96Mhz is present internally from this. This clock is divided by 2, and fed into the CPU divider.

CPUDIV1 (would run the CPU at 48MHz)

or

CPUDIV2 (would run the CPU at 24MHz)

or

CPUDIV3 (would run the CPU at 16Mhz)

or

CPUDIV4 (would run the CPU at 12MHz).

In each case, you would need to set the 'clock' statement, to match the selected CPU frequency. You could also run the CPU from the secondary oscillator instead (with another crystal), or from the internal oscillator, which would allow 31KHz, 125K, 250K, 500K, 1M, 2M, 4M, and 8M, to be selected instead (but with lower accuracy), while leaving the USB still clocked off it's required frequency!.
It is a 'fun' clock...

Best Wishes
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Wed Dec 06, 2006 3:32 pm     Reply with quote

Many thanks for your responce, Its much clearer now.

I always thought that the pic and usb ran togather using the same clocking frequency ( I assumed this is why it was so fussy).

So, the internal PLL conditions the clock for the USB and the PIC can tap into this if needed. did I understand that correctly?

Considering this you stated:
Quote:
The input to this, can therefore be 4, 8, 12, 20, 24, 40, or 48Mhz. Then in front of this, is the *4 PLL, allowing operation from 1, 2, 3, 4, 5, 6, 10, 12, 20 24, 40 or 48Mhz sources.

Does this mean I can use any of the first set of number or the bottom set? I have a 10Mhz crystal oscillator I would like to use. I personally prefere not to fiddle with the 2 legged crystals and caps.

once again, many thanks for the reply
Ttelmah
Guest







PostPosted: Thu Dec 07, 2006 6:30 am     Reply with quote

Potentially yes.
However I think you are talking about a 10MHz _oscillator module_, rather than a '10Mhz crystal'?. The problem is that to use 10MHz, requires running in PLL mode (so the input frequency gets multiplied by 4, to give 40MHz, which is then divided by 10 to give the 4Mhz. The PLL, is designed to run off a crystal, not an oscillator module, and can be fussy if overdriven (which can happen from an external oscillator).
The setup to use the oscillator module, would be:
HSPLL (feed the signal into OSC1)
PLL10

Then use the CPUDIV setting to give the required processor frequency.
I'd suggest trying with a 200R resistor, in the line from the oscillator module to the OSC1 input.

Best Wishes
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Fri Dec 08, 2006 10:13 pm     Reply with quote

Hi there,
I gave that a try, I built a circuit with PIC18F4550, MAX232.
I just want to test the thing its running before I connect the USB connector. I'm using a 12Mhz crystal with 2x 33pf caps to ground each side.

When I hook it all up I get lots of repeated garble on the serial port monitor. the baud and parity all matches the serial port monitors settings.

here is the code I used (from your direction)
Code:
#include <18F4550.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT_NOSL            //Brownout enabled during operation, disabled during SLEEP
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES PLL3                     //Divide by 3
#FUSES CPUDIV3                  //Divide USB clock by 3 (16Mhz)

#use delay(clock=16000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);
   while (true)
   {
      printf("This is a test!!!\n\r");
      delay_ms(100);
   }
}


I think i read somewhere that garbled RS232 means the clock isnt matched to the specified speeed., have I declared it correctly?

any ideas?
Ttelmah
Guest







PostPosted: Sat Dec 09, 2006 5:18 am     Reply with quote

Figures for CPUDIV are wrong. It gets so confusing, that divider changes according to whether the internal or external clock is selected, so the /2 divider for the internal USB clock is called CPUDIV1 etc.. CPUDIV3, gives /8, so your CPU is running at 12MHz, not 16. Sorry. Use CPUDIV2, to give 16MHz

Best Wishes
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Sat Dec 09, 2006 8:11 pm     Reply with quote

Hi There,
I set it to CPUDIV2 with 16Mhz set in clock setting but it still was garbles, I had a fiddle with different numbers and finally got CPUDIV1 with 12Mhz and it appears to be working.

many thanks for your help, next step for me now is to put some a button on and 2 leds and try the usb keyboard demo.

I will post the results here
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Sun Dec 10, 2006 12:07 am     Reply with quote

Hi there,
now got ex_usb_kbmouse.c example compiles (with alterations) to work on my 4550. I had to fiddle around with the PLL and CPUDIV settings but I got it working.

Here are the fuses I used, do keep in mind I'm using a 12Mhz crystal.
Code:
 #fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //,hspll
 #use delay(clock=12000000)


It connects up nicely and enumerates, when I push the button on the board (pin A4 as specified in the source) it indeed does move the mouse a little but then it tells me its un-enumerated then it re-enumerates. if I hold the button down I get an unrecognised usb error in windows. and I dont appear to be getting any letters appearing in the notepad window (which is focused)

The main thing for me is that it now works. anyone have any idea of how to tweak the demo to make it work "normally"?[/code]
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Sun Dec 10, 2006 12:34 am     Reply with quote

update:

just did another fiddle with numbers and have this configuration going:
Code:
 #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //,hspll
 #use delay(clock=48000000)


but unfortunatly still does problems as above.
Guest








PostPosted: Sat Oct 06, 2007 10:24 pm     Reply with quote

I just bought a set of 20Mhz crystals from Digikey along with some 33pF caps and my 4550 and 2550 code is working nicely, well apart from doing the bulk mode stuff as I dont have the demo for that.

anyone done anything using delphi that is willing to share some sort of example?
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