Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

Understanding Fosc (PIC18F4620)

Status
Not open for further replies.

Neyolight

Full Member level 5
Joined
Aug 29, 2011
Messages
306
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
World
Activity points
3,624
Hi ALL

I am about to loose my patience with this PIC18F4620 MCU. :-x I just dont understand why my code doesn't work. Oh and btw the code is just a simple USART from Pic dem 2 plus board to my laptop.

So I feel my problem lies in the frequency /baud rate area.

Question 1 : The max PIC18f4620 can go is 8MHZ. So if i do not touch the OSCCON register, 8MHz should be the default frequency my chip is running at? Am I correct?


Question 2: If I do touch the OSCCON register and prescale the frequency to 4MHZ, is this (4MHZ) my new Fosc now? Going by the clock diagram in the datasheep, the Fosc is 8Mhz and the rest prescaled frequencies are called "peripharal frequency". So what frequency do i use to calculate Baud rate- 4 or 8 Mhz?


I have absolutely NO IDEA why this tiny-easy-piece ( according to some) code doesnt WORK :-x

HELP :-?
 

PIC18f4620 Configuration Bits

Hi ALL


I hope i get a reply to this post :wink:

SO ya in order to establish USART between my PIC18f4620 on a picdem 2 board and my Laptop, what configuration bits do I need to set and to what value?

For example:
OSC , FCMEN , IESO , PWRT, BOREN, BORV , WDT , WDTPS , MCLRE , LPT1OSC , PBADEN, CCP2MX , STVREN , LVP , XINST

My simple "Hello world" program isnt working and I belive its to do with these config bits. :roll:

Thanks in advance =)
 
I am about to loose my patience with this PIC18F4620 MCU. :-x I just dont understand why my code doesn't work. Oh and btw the code is just a simple USART from Pic dem 2 plus board to my laptop.

So I feel my problem lies in the frequency /baud rate area.

It maybe due to several factors. You'll need to post you complete code before a more comprehensive solution can be offered.

Question 1 : The max PIC18f4620 can go is 8MHZ. So if i do not touch the OSCCON register, 8MHz should be the default frequency my chip is running at? Am I correct?

Actually the maximum FOSC of the PIC18F4620 is 40MHz, not 8MHZ, this is due to not only the internal oscillator which you are referring, but the existence of a Phase Lock Loop (PLL) as well. With the use of the internal oscillator a maximum FOSC of 32MHz can be achieved.

Reference PIC18F2525/2620/4525/4620 Datasheet, 2.5 PLL Frequency Multiplier, pg 25

2.5 PLL Frequency Multiplier

A Phase Locked Loop (PLL) circuit is provided as an
option for users who wish to use a lower frequency
oscillator circuit or to clock the device up to its highest
rated frequency from a crystal oscillator. This may be
useful for customers who are concerned with EMI due
to high-frequency crystals or users who require higher
clock speeds from an internal oscillator.


2.5.2 PLL AND INTOSC

The PLL is also available to the internal oscillator block
in selected oscillator modes. In this configuration, the
PLL is enabled in software and generates a clock
output of up to 32 MHz. The operation of INTOSC with
the PLL is described in Section 2.6.4 “PLL in INTOSC
Modes”.

And 2.6 Internal Oscillator Block, pg 26:

2.6.4 PLL IN INTOSC MODES

The 4x frequency multiplier can be used with the
internal oscillator block to produce faster device clock
speeds than are normally possible with an internal
oscillator. When enabled, the PLL produces a clock
speed of up to 32 MHz.
Unlike HSPLL mode, the PLL is controlled through
software. The control bit, PLLEN (OSCTUNE<6>), is
used to enable or disable its operation.
The PLL is available when the device is configured to
use the internal oscillator block as its primary clock
source (FOSC3:FOSC0 = 1001 or 1000). Additionally,
the PLL will only function when the selected output frequency
is either 4 MHz or 8 MHz (OSCCON<6:4> = 111
or 110). If both of these conditions are not met, the PLL
is disabled.
The PLLEN control bit is only functional in those internal
oscillator modes where the PLL is available. In all
other modes, it is forced to ‘0’ and is effectively
unavailable.

Question 2: If I do touch the OSCCON register and prescale the frequency to 4MHZ, is this (4MHZ) my new Fosc now? Going by the clock diagram in the datasheep, the Fosc is 8Mhz and the rest prescaled frequencies are called "peripharal frequency". So what frequency do i use to calculate Baud rate- 4 or 8 Mhz?

The default of OSCCON: OSCILLATOR CONTROL REGISTER Bits IRCF2:IRCF0 is "100" or 1MHz Internal Oscillator Frequency.

The Internal Oscillator Frequency Select bits
(IRCF2:IRCF0) select the frequency output of the
internal oscillator block to drive the device clock. The
choices are the INTRC source, the INTOSC source
(8 MHz) or one of the frequencies derived from the
INTOSC postscaler (31.25 kHz to 4 MHz). If the
internal oscillator block is supplying the device clock,
changing the states of these bits will have an immediate
change on the internal oscillator’s output. On
device Resets, the default output frequency of the
internal oscillator block is set at 1 MHz.

The SCS1:SCS0 bits and FOSC3:FOSC0 configuration bits should be set appropriately as well:

The System Clock Select bits, SCS1:SCS0, select the
clock source. The available clock sources are the
primary clock (defined by the FOSC3:FOSC0 Configuration
bits), the secondary clock (Timer1 oscillator) and
the internal oscillator block. The clock source changes
immediately after one or more of the bits is written to,
following a brief clock transition interval. The SCS bits
are cleared on all forms of Reset.

Please post your code so that one of us can examine it.

BigDog

---------- Post added at 03:55 ---------- Previous post was at 03:45 ----------

Hi ALL


I hope i get a reply to this post :wink:

SO ya in order to establish USART between my PIC18f4620 on a picdem 2 board and my Laptop, what configuration bits do I need to set and to what value?

For example:
OSC , FCMEN , IESO , PWRT, BOREN, BORV , WDT , WDTPS , MCLRE , LPT1OSC , PBADEN, CCP2MX , STVREN , LVP , XINST

My simple "Hello world" program isnt working and I belive its to do with these config bits. :roll:

Thanks in advance =)

The method to achieve this depends on the compiler or assembler used to produce your program.

What compiler or assembler are you using?

Also please post your program so that one of us can offer specific advice. You'll need to specific several of the configuration bits to have your program perform as expected, including Oscillator, Watchdog to mention a few.

BigDog
 
Hey thanks for that, Im using MPLAB C18 complier for Pic18 !

Here's my code:

Code:
#include <p18f4620.h>
#include <stdio.h>
#include <delays.h>
//#include <spi.h>
#include <usart.h>
 

#pragma config OSC = HS, LVP=OFF ,MCLRE =ON , WDT=OFF
 
void setup(void)
{
	/* Port Set Up*/
	ADCON1 = 0b00001111; //set all pins to digital mode
	TRISD = 0x00;
	TRISA = 0x00;
	TRISB = 0b00000000; 
	TRISC = 0b10000000; // RX is an input, TX is output
 	PORTBbits.RB0=1;      //Turn on the 4 LEDs
 	PORTBbits.RB1=1;
 	PORTBbits.RB2=1;
 	PORTBbits.RB3=1;
 	
	/* Interrupt Setup */
	INTCON = 0x00; /* Clear Registers */
	PIR1 = 0x00;
	PIE1 = 0x00;
	TMR1L = 0x00;
	TMR1H = 0x00;
	T1CON = 0x00;
	RCON = 0x00; 
 
             /* RS232 Enable */
	RCSTA = 0b10000000;
	TXSTA = 0b00100000;
	BAUDCON = 0b00000000;		
	//SPBRG = 51; //FOSC = 8.000 MHz,SYNC = 0, BRGH = 0, BRG16 = 0
	OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51);
 
 
 
	/* Clock Setup*/ 
	OSCCON = 0b01110110; //select 8 MHz clock
}	
 

#pragma code
void main(void)
{
	setup();
		
	while (1)
	{
		putrsUSART("Hello");
	//	while(BusyUSART());
		Delay1KTCYx(1000);
		
	}
}
 
Are you using the Microchip C18 Compiler?

BigDog

Yes I am ! Microchip C18 ToolSuit!

Im ALSO trying to turn the 4 leds on, but that doesnt work either- Not sure where I am going wrong!
 

Yes I am ! Microchip C18 ToolSuit!

Im ALSO trying to turn the 4 leds on, but that doesnt work either- Not sure where I am going wrong!


Please post your code for this task as well.


BigDog
 
I hadn't had a chance to look over your program, so I didn't realize you were referring to the same program.

BigDog
 

Start with making your PIC blink a led with minimum software and hardware! See whether there is a problem! You can simply make sure your osc frequency by checking a blinking led. If everything is ok, then add codes pertaining to uart. Try your code in simulators before working with real hardware. Go step by step.
 

I can make the LED blink separately, but when I add my UART code nothing seems to work!
 

You can simply make sure your osc frequency by checking a blinking led

hmmm how? Im always confused about what frequency my board is running at so checking the frequency in real would be really cool!
 

Your revised code posted below both successfully turns on the LEDs and transmits serially "Hello World!" at 9600 8-N-1. The code was tested using a PICDEM 2 PLUS with a PIC18F4620 installed configured to use the internal oscillator at 8MHz.

Code:
#include <p18f4620.h>
#include <stdio.h>
#include <delays.h>
#include <usart.h>
 
// INTIO67 configures the PIC for using internal oscillator
#pragma config OSC=INTIO67, LVP=OFF, MCLRE=ON , WDT=OFF
 
void setup(void)
{
	/* Clock Setup*/ 
	OSCCON = 0b01110010; //select 8 MHz clock 

	/* Port Set Up*/
	ADCON1 = 0b00001111; //set all pins to digital mode
	TRISD = 0x00;
	TRISA = 0x00;
	TRISB = 0b00000000; 
	TRISC = 0b10000000; // RX is an input, TX is output

 //	PORTBbits.RB0=1;      //Turn on the 4 LEDs
 //	PORTBbits.RB1=1;
 //	PORTBbits.RB2=1;
 //	PORTBbits.RB3=1;

 // Turn on the 4 LEDs
 // Microchip recommends writing to the PORT Latch rather than the PORT Pins 
	LATBbits.LATB0=1;
	LATBbits.LATB1=1;
	LATBbits.LATB2=1;
	LATBbits.LATB3=1;

 	
	/* Interrupt Setup */
	INTCON = 0x00; /* Clear Registers */
	PIR1 = 0x00;
	PIE1 = 0x00;
	TMR1L = 0x00;
	TMR1H = 0x00;
	T1CON = 0x00;
	RCON = 0x00; 

	

    /* RS232 Enable */
	// OpenUSART Configures The Next Three Lines
	//RCSTA = 0b10000000;
	//TXSTA = 0b00100000;
	//BAUDCON = 0b01000000;
		
	// USART 9600 8-N-1
	OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 12);
 
}	
 

#pragma code
void main(void)
{
	setup();
		
	while (1)
	{
		putrsUSART(" Hello World! ");
	//	while(BusyUSART()); Not need in this case
		Delay10KTCYx(100);
		
	}
}

I have tried to comment the changes I have made, however if you have any additional questions concerning the changes feel free to post them in this thread.

I have also attached the zipped Project Directory.

BigDog
 

Attachments

  • Neyolight.zip
    18.2 KB · Views: 58
What device are you using as interface? If that's anything like pk2, pk3 or icd2/3 then maybe you should try debugging mode to see where is gets screwed up. I can't quite think about any particular software fail at setting up uart which would make gpio not work
 

Thanks all and bigdogguru :) The code works absolutely how I want it to be =)
 

Ok I get this error " Unable to open COM4. Please check your port settings"

It worked before but giving me this error now. What could be the problem? I did not change anything in the code or any setting! Using 9600 8-N-1

I tried all COM Ports , Same error!
 
Last edited:

Ok I get this error " Unable to open COM4. Please check your port settings"

It worked before but giving me this error now. What could be the problem? I did not change anything in the code or any setting! Using 9600 8-N-1

I tried all COM Ports , Same error!

What terminal emulator are you using on the PC side of the serial connection?

It sounds like the terminal emulator is not releasing the COM4 Port. Have you made sure all instances of the terminal emulator program have been exited/terminated?

BigDog
 

Im using hyperterminal ! I will reinstall it and see if the problem still persists!
 
Last edited:

Try PuTTY. It's open source and very flexible.

PuTTY Download Page

**broken link removed**

As far as Hyperterminal not releasing the COM Port, just reboot your system and any lingering processes will be terminated.

BigDog
 

No luck with either putty nor with reinstalled hyperterminal!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top