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 =)
 

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


And 2.6 Internal Oscillator Block, pg 26:



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


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


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

BigDog

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


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: 88
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:


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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…