electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

PIC18F2550, C18 - What setting to choose for CPUDIV?


Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> PIC18F2550, C18 - What setting to choose for CPUDIV?
Author Message
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post19 Oct 2009 22:28   

PIC18F2550, C18 - What setting to choose for CPUDIV?


MPLAB>Help>Topics>Language Tools>PIC18 Config Settings wrote:
Code:
PLL // Prescaler Selection bits:
PLLDIV = 1   // No prescale (4 MHz oscillator input drives PLL directly)
PLLDIV = 2   // Divide by 2 (8 MHz oscillator input)
PLLDIV = 3   // Divide by 3 (12 MHz oscillator input)
PLLDIV = 4   // Divide by 4 (16 MHz oscillator input)
PLLDIV = 5   // Divide by 5 (20 MHz oscillator input)
PLLDIV = 6   // Divide by 6 (24 MHz oscillator input)
PLLDIV = 10   // Divide by 10 (40 MHz oscillator input)
PLLDIV = 12   // Divide by 12 (48 MHz oscillator input)

// CPU System Clock Postscaler:
CPUDIV = OSC1_PLL2   // [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
CPUDIV = OSC2_PLL3   // [OSC1/OSC2 Src: /2][96 MHz PLL Src: /3]
CPUDIV = OSC3_PLL4   // [OSC1/OSC2 Src: /3][96 MHz PLL Src: /4]
CPUDIV = OSC4_PLL6   // [OSC1/OSC2 Src: /4][96 MHz PLL Src: /6]

// USB Clock Selection bit (used in Full Speed USB mode only; UCFG:FSEN = 1):
USBDIV = 1   // USB clock source comes directly from the primary oscillator block with no postscale
USBDIV = 2   // USB clock source comes from the 96 MHz PLL divided by 2

// Oscillator Selection bits:
FOSC = XT_XT      // XT oscillator, XT used by USB
FOSC = XTPLL_XT   // XT oscillator, PLL enabled, XT used by USB
FOSC = ECIO_EC   // External clock, port function on RA6, EC used by USB
FOSC = EC_EC      // External clock, CLKOUT on RA6, EC used by USB
FOSC = ECPLLIO_EC   // External clock, PLL enabled, port function on RA6, EC used by USB
FOSC = ECPLL_EC   // External clock, PLL enabled, CLKOUT on RA6, EC used by USB
FOSC = INTOSCIO_EC   // Internal oscillator, port function on RA6, EC used by USB
FOSC = INTOSC_EC   // Internal oscillator, CLKOUT on RA6, EC used by USB
FOSC = INTOSC_XT   // Internal oscillator, XT used by USB
FOSC = INTOSC_HS   // Internal oscillator, HS used by USB
FOSC = HS      // HS oscillator, HS used by USB
FOSC = HSPLL_HS   // HS oscillator, PLL enabled, HS used by USB


I have two matters to consider:
1) I will use a 20MHz crystal for external oscillator.
2) I will use USB feature of the PIC

I read the datasheet of the PIC which says (at page 32) that for the case of using a 20MHz crystal these are the settings which I have to choose:
1) PLL Division (PLLDIV2:PLLDV0): +5 (100)
2) Clock Mode (FOSC3:FOSC0): HSPLL, ECPLL, ECPIO
3) MCU Clock Division (CPUDIV1:CPUDIV0): +4 (10)

I'm totally lost!
The PIC datasheet and MPLAB help file are talking in different languages.

I know I must add some "#pragma config xxx=yyy" lines at the beginning of my code, but I don't know what to add. The thing I want to learn most is what the CPUDIV value mean; what does "[OSC1/OSC2 Src: /2][96 MHz PLL Src: /3]" mean? I googled for an hour and couldn't find any relevant result. Can anyone please help me add the required "pragma" lines in my code?
Back to top
wp100



Joined: 15 May 2009
Posts: 327
Helped: 45
Location: England


Post20 Oct 2009 9:17   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Hi,

Cannot help you directly with the C code, but have a look at this link to Microchips USB section - see the Software section for the USB Framework which might show you some code / config examples to work with.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1486
Back to top
elrayes



Joined: 03 Jun 2008
Posts: 164
Helped: 8
Location: Egypt


Post20 Oct 2009 9:53   

PIC18F2550, C18 - What setting to choose for CPUDIV?


since you are using a 20 MHz crystal then you will need to pre-PLL divide by 5 to become 4MHz then the PLL will get it to 96 MHz, for USB it will be divided by 2 to get 48 MHz needed for USB.
I think you should use
PLLDIV = 5 // Divide by 5 (20 MHz oscillator input)
however i think the CPU clocking is separate from the USB clocking system i.e. I don't think both of them must be at the same clock, they can be different, at least you can make both work on the same frequency, so use this:
CPUDIV = OSC1_PLL2 // [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]

good luck
Back to top
daviddlc



Joined: 11 Feb 2007
Posts: 196
Helped: 13


Post20 Oct 2009 17:29   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Elrayes is correct, these are the settings you need for the USB.

PLLDIV = 5 //20MHz / 5 = 4 MHz, input for the 96 MHz PLL
CPUDIV = OSC1_PLL2 //CPU Clock = 96 MHz/2 = 48 MHz
USBDIV = 2 //96MHz PLL/2 = 48 MHz for USB clock
FOSC = HSPLL_HS //Osc type: High Speed Crystal/Resonator with PLL enabled
VREGEN = ON //USB Internal Voltage Regulator enabled

The above settings works for me when using 20 MHz XTAL.
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post23 Oct 2009 8:55   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


I modified my code just as you suggested me to do. But still my circuit does not work. Here is my new code:
Code:
#include <p18f2550.h>
#include <delays.h>

#pragma config PLLDIV   = 5         // need 5 for 20MHz xtal
#pragma config CPUDIV   = OSC1_PLL2   // CPU Clock = 96 MHz/2 = 48 MHz
#pragma config USBDIV   = 2         // 96MHz PLL/2 = 48 MHz for USB clock
#pragma config FOSC      = HSPLL_HS   // High Speed Crystal / Resonator with PLL enabled
#pragma config WDT      = OFF      // no watchdog
#pragma config LVP      = OFF      // LVP MUST be off
#pragma config BOR      = OFF      // no brown out reset
#pragma config MCLRE   = ON      // MCLR in use (so 10k to +5v essential)
#pragma config PWRT      = ON      // power up timer on
#pragma config PBADEN   = OFF      // disable PORTB a/d converter
#pragma config VREGEN   = ON      // USB Internal Voltage Regulator enabled

void main (void)
{
   TRISB = 0;
   PORTB = 0;
   
   while (1)
   {
      PORTB = 0;
      Delay10KTCYx(0);
      PORTB = 0xFF;
   }
}


And this is my circuit:
PIC18F2550, C18 - What setting to choose for CPUDIV?

Is there any mistake in my design?
Back to top
betwixt



Joined: 04 Jul 2009
Posts: 388
Helped: 64
Location: Wales, UK


Post23 Oct 2009 9:13   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Cough cough.....

With a layout like that it's a wonder anything works at all.
You MUST place capacitors across the supply lines as close to the IC as possible and you MUST ensure the ground connection to the IC is as low impedance as possible to the ground side of the crystal loading capacitors.

You oscillator could be on any frequency at the moment and there will be so much noise on the supply it is very unlikely it will be stable enough to run reliably if at all.

Brian.
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post23 Oct 2009 9:42   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


betwixt wrote:
Cough cough.....

With a layout like that it's a wonder anything works at all.
You MUST place capacitors across the supply lines as close to the IC as possible and you MUST ensure the ground connection to the IC is as low impedance as possible to the ground side of the crystal loading capacitors.

You oscillator could be on any frequency at the moment and there will be so much noise on the supply it is very unlikely it will be stable enough to run reliably if at all.

Brian.


Thank you for the advice.

I designed a smaller circuit, with capacitors close to PIC. Checked for signals at the pin connections of the PIC and didn't see so much of noise to disturb the operation of PIC. I have to make clear that I successfully made this very same circuit work with assembly, and now it doesn't work with C code.

I also check signals at two pins of crystal and there is no clock pulse there. In my previous circuit (the working one programmed with assembly language) had clock pulses at the crystal pins. Therefore, I think that the oscillator circuit of my PIC is not working. I changed the crystal and capacitors but it is still the same.

Can anyone tell me if I have anything wrong with my code which I added in my previous message?
Thanks in advance.
Back to top
anandpv2009



Joined: 11 Aug 2009
Posts: 63
Helped: 4
Location: India


Post23 Oct 2009 20:14   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Here is the HEX file that works fine with PIC18F2550 with 20mhz (xtal)...
This code will on and off led connected to port B.
I were tested it and it works fine...



Sorry, but you need login in to view this attachment

Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post23 Oct 2009 22:33   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


anandpv2009 wrote:
Here is the HEX file that works fine with PIC18F2550 with 20mhz (xtal)...
This code will on and off led connected to port B.
I were tested it and it works fine...


Thank you, but your zip file contains only a hex file.

I don't want to flash a led.
I want to learn how to flash a led.

I need a C18 source code, or someone to tell me what is wrong with my code...
Back to top
betwixt



Joined: 04 Jul 2009
Posts: 388
Helped: 64
Location: Wales, UK


Post23 Oct 2009 23:28   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


I doubt this is the problem but try using LATB instead of PORTB when you output the data.

Brian.
Back to top
anandpv2009



Joined: 11 Aug 2009
Posts: 63
Helped: 4
Location: India


Post24 Oct 2009 10:10   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Code:
#include <18F2550.h> 
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
 
void main()
{
set_tris_b(0);
while(1)
{
   output_b(0xff);
delay_ms(100);
   output_b(0x00);
delay_ms(100);
}
   
}


Why you are searching this simple code.....
This is for CCS compile..

I think your next switch on will be a success

ha..am i correct

PIC18F2550, C18 - What setting to choose for CPUDIV?[/img]
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post24 Oct 2009 11:34   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


betwixt wrote:
I doubt this is the problem but try using LATB instead of PORTB when you output the data.
Didn't changed anything...

anandpv2009 wrote:
Code:
#include <P18F2550.h> 
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN   // LINE #34
#use delay(clock=20000000)
 
void main()
{
set_tris_b(0);
while(1)
{
   output_b(0xff);
delay_ms(100);
   output_b(0x00);
delay_ms(100);
}
   
}


Why you are searching this simple code.....
This is for CCS compile..

I think your next switch on will be a success

ha..am i correct

Error given:
Quote:
----------------------------------------------------------------------
Debug build of project `C:\Development\PIC\Projects\18F2550 Hello World\Project.mcp' started.
Language tool versions: MPASMWIN.exe v5.34, mplink.exe v4.34, mcc18.exe v3.34
Preprocessor symbol `__DEBUG' is defined.
Sat Oct 24 14:06:43 2009
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Development\PIC\MPLAB C18\bin\mcc18.exe" -p=18F2550 /i"C:\Development\PIC\MPLAB C18\h" "main.c" -fo="C:\Documents and Settings\<profilename>\Desktop\main.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
C:\Development\PIC\Projects\18F2550 Hello World\main.c:34:Error: syntax error
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Development\PIC\Projects\18F2550 Hello World\Project.mcp' failed.
Language tool versions: MPASMWIN.exe v5.34, mplink.exe v4.34, mcc18.exe v3.34
Preprocessor symbol `__DEBUG' is defined.
Sat Oct 24 14:06:43 2009
----------------------------------------------------------------------
BUILD FAILED
Back to top
anandpv2009



Joined: 11 Aug 2009
Posts: 63
Helped: 4
Location: India


Post24 Oct 2009 16:18   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


You should use CCS compiler to work the above program...
Please specify your compiler name.? I think your compiler is not CCS.


You can download free version of ccs from net.
One more thing, Whats about my previously posted hex (worked or not).?
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post24 Oct 2009 16:57   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


anandpv2009 wrote:
You should use CCS compiler to work the above program...
Please specify your compiler name.? I think your compiler is not CCS.


You can download free version of ccs from net.
One more thing, Whats about my previously posted hex (worked or not).?

IDE: MPLAB 8.40 Academic Version
Compiler: MPLAB C18 3.34 Academic Version

Your hex file works wonderful.
I don't want to change my compiler. I can't find enough documentation and tutorial even for most popular compilers.
I don't think things will be any better if I switch to CCS.
Thank you anyway.
Back to top
IsaacJn



Joined: 14 Nov 2009
Posts: 4
Location: UK


Post14 Nov 2009 13:22   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


Dear hkBattousai

Did you get your program to work? I am also having the same problem except that I am able to switch on the leds on porta, but I cannot manage to flash them.
Back to top
anandpv2009



Joined: 11 Aug 2009
Posts: 63
Helped: 4
Location: India


Post14 Nov 2009 13:47   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


I need more details about your problems to help you (if i can)....
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post14 Nov 2009 16:02   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


I made it work, thank you for your concern.

Here is a working blink-a-led example:
Code:
#include <p18f2550.h>
#include "delays.h"
#pragma config WDT      = OFF
#pragma config MCLRE   = ON
#pragma config DEBUG   = OFF
#pragma config LVP      = OFF
#pragma config PLLDIV   = 5         // need 5 for 20MHz xtal
#pragma config CPUDIV   = OSC1_PLL2   // CPU Clock = 96 MHz/2 = 48 MHz
#pragma config USBDIV   = 2         // 96MHz PLL/2 = 48 MHz for USB clock
#pragma config FOSC      = HSPLL_HS   // High Speed Crystal / Resonator with PLL enabled

void blinkLed(unsigned char led);

void main (void)
{
   ADCON0=0x00;
   ADCON1=0x0f;
   CMCON=7;
   
   TRISAbits.TRISA0 = 0;
   TRISAbits.TRISA1 = 0;
   TRISAbits.TRISA2 = 0;
   TRISAbits.TRISA3 = 0;
   TRISAbits.TRISA4 = 0;
   
   LATAbits.LATA0 = 0;
   LATAbits.LATA1 = 0;
   LATAbits.LATA2 = 0;
   LATAbits.LATA3 = 0;
   LATAbits.LATA4 = 0;
   
   blinkLed(0);
   blinkLed(1);
   blinkLed(2);
   blinkLed(3);
   blinkLed(4);
   
   while(1);
}

void blinkLed(unsigned char led)
{   
   int j; for (j=0; j<10; j++)
   {
      switch (led)
      {
         case 0:
            LATAbits.LATA0 = 1;
            Delay10KTCYx(0);
            LATAbits.LATA0 = 0;
            Delay10KTCYx(0);
            break;
         case 1:
            LATAbits.LATA1 = 1;
            Delay10KTCYx(0);
            LATAbits.LATA1 = 0;
            Delay10KTCYx(0);
            break;
         case 2:
            LATAbits.LATA2 = 1;
            Delay10KTCYx(0);
            LATAbits.LATA2 = 0;
            Delay10KTCYx(0);
            break;
         case 3:
            LATAbits.LATA3 = 1;
            Delay10KTCYx(0);
            LATAbits.LATA3 = 0;
            Delay10KTCYx(0);
            break;
         case 4:
            LATAbits.LATA4 = 1;
            Delay10KTCYx(0);
            LATAbits.LATA4 = 0;
            Delay10KTCYx(0);
            break;
         default:
            LATAbits.LATA0 = 0;
            LATAbits.LATA1 = 0;
            LATAbits.LATA2 = 0;
            LATAbits.LATA3 = 0;
            LATAbits.LATA4 = 0;
            break;
      }
   }   
}
Back to top
IsaacJn



Joined: 14 Nov 2009
Posts: 4
Location: UK


Post14 Nov 2009 20:17   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


I have a problem with configuring the external oscillator. The program i have written only switches the led, it supposed to be flashing.

#include <p18f4550.h>
#include <delays.h>

#pragma config PLLDIV = 5 // need 5 for 20MHz xtal
#pragma config CPUDIV = OSC1_PLL2 // CPU Clock = 96 MHz/2 = 48 MHz
#pragma config USBDIV = 2 // 96MHz PLL/2 = 48 MHz for USB clock
#pragma config FOSC = HSPLL_HS // High Speed Crystal / Resonator with PLL enabled
#pragma config WDT = OFF // no watchdog
#pragma config LVP = OFF // LVP MUST be off
#pragma config BOR = OFF // no brown out reset
#pragma config MCLRE = ON // MCLR in use (so 10k to +5v essential)
#pragma config PWRT = ON // power up timer on
#pragma config PBADEN = OFF // disable PORTB a/d converter
#pragma config VREGEN = ON // USB Internal Voltage Regulator enabled

//------------------------------------------------------
//----Interrupt toggle prototype------------------------
void led_toggle (void);

//-------------------------------------------------------
// -------High priority interrupt vector-----------------
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void) {
_asm
goto led_toggle //jump to interrupt routine
_endasm
}

//----------------------------------------------------------
// -----------High priority interrupt routine---------------
#pragma code
#pragma interrupt led_toggle
void led_toggle () {
if (INTCONbits.TMR0IF) { //check for TMR0 overflow
LATAbits.LATA0 = ~LATAbits.LATA0;
// PORTA = 0x00;
TMR0L = 0x00;
INTCON = 0x20; // Set T0IE, clear T0IF
}
}

//----------------------------------------------------------------
//-------Main code-------------------------------------------------
#pragma code
void main (void)
{

/* Configure output pin for LED */
TRISA = 0xFC; //two LSB configured to be output
LATA = 0x00;

//Initialise Timer0 and its interrupt */
INTCON = 0x20; // disable global and enable timer interrupt
INTCON2 = 0x84; // TMR0 high priority
RCONbits.IPEN = 1; // enable priority levels
TMR0H = 0x00; // clear timer
TMR0L = 0x00; // clear timer
T0CON = 0xc5; // set up timer0, 16-bit, 1:64 prescaler
INTCONbits.GIEH = 1; // enable interrupts

while (1)
{

}
}
Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post14 Nov 2009 20:28   

PIC18F2550, C18 - What setting to choose for CPUDIV?


^ I don't know much about PIC interrupts, but it looks like your interrupt is running only once. I don't think you have an oscillator configuration problem. If you insist that you have a configuration problem then, I suggest, try running something simpler; comment out all those interrupt part of your code and copy/paste my blink-a-led example above. If it works then your code indeed has an interrupt calling problem, not something related to configuration.
Back to top
IsaacJn



Joined: 14 Nov 2009
Posts: 4
Location: UK


Post14 Nov 2009 21:33   

Re: PIC18F2550, C18 - What setting to choose for CPUDIV?


^I have tried your program and it works as expected.

I am able to flash one led with my program (refer my previous post) with FOSC = INTOSC_HS. I dont know why I cant with FOSC = HSPLL_HS.
Back to top
Google
AdSense
Google Adsense




Post14 Nov 2009 21:33   

Ads




Back to top
hkBattousai



Joined: 16 Jun 2007
Posts: 100
Helped: 4
Location: Turkey


Post14 Nov 2009 22:19   

PIC18F2550, C18 - What setting to choose for CPUDIV?


Try replacing all of your "#pragma config ..." directives with mine.
If it works, try changing my directives with yours one by one. This way, can find the directive which is causing the problem.
Back to top
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> PIC18F2550, C18 - What setting to choose for CPUDIV?
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
[Microchip C18/30] What is LATB? (2)
Hitech picc18 VS. MPLAB C18 what is the better? (7)
What embedded OS to choose? (9)
VB or LabView what to choose (3)
What tools should I choose? (7)
C code for interfacing DS1302 with PIC18F2550 (2)
What kind of ADC should I choose? (14)
Orcad Layout - what does this setting mean? (5)
which is fast? slew rate setting or exponential setting? (3)
What I choose? LeCROY WS434 vs. TEKTRONIX TDS3034B... (9)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS