Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

PIC18F2550, C18 - What setting to choose for CPUDIV?

Status
Not open for further replies.

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
MPLAB>Help>Topics>Language Tools>PIC18 Config Settings said:
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?
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

elrayes

Full Member level 5
Joined
Jun 3, 2008
Messages
246
Helped
20
Reputation
40
Reaction score
7
Trophy points
1,298
Location
Egypt/Canada
Activity points
2,780
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
 

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

daviddlc

Full Member level 5
Joined
Feb 11, 2007
Messages
260
Helped
35
Reputation
70
Reaction score
9
Trophy points
1,298
Location
USA
Activity points
2,709
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.
 

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
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:


Is there any mistake in my design?
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,717
Helped
4,810
Reputation
9,637
Reaction score
4,590
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
125,363
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.
 

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
betwixt said:
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.
 

anandpv2009

Advanced Member level 1
Joined
Aug 11, 2009
Messages
407
Helped
60
Reputation
120
Reaction score
40
Trophy points
1,308
Location
God's Own Country, Kerala
Activity points
3,340
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...
 

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
anandpv2009 said:
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...
 

anandpv2009

Advanced Member level 1
Joined
Aug 11, 2009
Messages
407
Helped
60
Reputation
120
Reaction score
40
Trophy points
1,308
Location
God's Own Country, Kerala
Activity points
3,340
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

[/img]
 

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
betwixt said:
I doubt this is the problem but try using LATB instead of PORTB when you output the data.
Didn't changed anything...

anandpv2009 said:
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:
----------------------------------------------------------------------
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
 

anandpv2009

Advanced Member level 1
Joined
Aug 11, 2009
Messages
407
Helped
60
Reputation
120
Reaction score
40
Trophy points
1,308
Location
God's Own Country, Kerala
Activity points
3,340
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).?
 

    hkBattousai

    points: 2
    Helpful Answer Positive Rating

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
anandpv2009 said:
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.
 

IsaacJn

Newbie level 5
Joined
Nov 14, 2009
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
UK
Activity points
1,344
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.
 

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
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;
		}
	}	
}
 

IsaacJn

Newbie level 5
Joined
Nov 14, 2009
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
UK
Activity points
1,344
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)
{

}
}
 

hkBattousai

Advanced Member level 4
Joined
Jun 16, 2007
Messages
118
Helped
5
Reputation
10
Reaction score
1
Trophy points
1,298
Location
Turkey
Activity points
2,182
^ 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.
 

IsaacJn

Newbie level 5
Joined
Nov 14, 2009
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
UK
Activity points
1,344
^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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top