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?
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”.
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 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 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.
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 =)
#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!
Please post your code for this task as well.
BigDog
You can simply make sure your osc frequency by checking a blinking led
#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);
}
}
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!
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?