# newb question on defineing crystal speed

Status
Not open for further replies.

#### large_ghostman

##### Well-Known Member
Hi
Just a quick question, I have been using different chips to pics lately, and I have kind of got used to the way they do things, however I need to switch back to pics for a project.
The project involves the delays lib, Uart and SPI.
now just for argument sake, say I am using the 18f4685 chip, and using C18 compiler.
how in the code do I tell it what frequency the crystal is running at? I normally use the internal OSC, and none of the code I have used actually says anywhere in it what speed the chip is running at! I dont normally use the delay libs.
However now I need to use a crystal of a non standard value, again it isnt settled but lets say the crystal is 36.864 MHz,
Can someone show me a couple of lines of where and how to tell the compiler the frequency of the crystal I am using?
Just for back ground the reason for strange values is, I am going to be running the Uart at fast speeds, so I have some crystals that were chosen to give low error %.
Thank you

#### MikeMl

##### Well-Known Member
Two parts. You have to tell the compiler what the clock rate is so it can figure out the delays. You have to tell the chip how to set the hardware. One is compiler directive; the other is a chip configuration setting.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Why does the compiler need to know the frequency?

If you need a baud for a specific crystal.. the baud can be defined with an equation.

#define FREQ 36864000

#define 9600BAUD baud equation

#### large_ghostman

##### Well-Known Member
hi
Thank you both. I will give it a try, I scraped the code. I was getting errors with the delay functions, they were well off! I have been using dads new ARM boards with Keil, the set up is easier! very very nice dev kits from silicon labs. There IDE makes it easy to get the chip to jump through hoops.

#### large_ghostman

##### Well-Known Member
I will post it as I go, part of it is based on mikes serial back pack. But first I need to finish something else. Mike I am hoping you will like my take on your serial back pack! its not a copy I have added some stuff. But the inspiration was from your one.

#### large_ghostman

##### Well-Known Member
what I have got used to is something like the following set up.
Code:
#define BAUDRATE     115200            // Baud rate of UART in bps

// SYSTEMCLOCK = System clock frequency in Hz
#define SYSTEMCLOCK       22118400L

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);
void PORT_Init (void);
void UART0_Init (void);

sofor clarity the C18 version is

Code:
#define FREQ 36864000

#define 9600BAUD baud equation

#### JonSea

##### Well-Known Member
One thing I've learned with Swordfish Basic (Hi. I'm Jon and I use Basic.)....

Swordfish uses the clock statement to define oscillator speed in MHz:

Clock = 20

....but this does not take into account any PLL multiplier settings. So clock might equal 80 with a 20 MHz crystal and a 4x PLL multiplier for example.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
LG... If you are using C18, why don't you use library USART functions?

The baud equation is:- SPRG = (FOSC / ( BRG / BAUD)) - 1

BRG can equal 64 or 16.. So if BRGH = 0 then let BRG = 64... If BRGH = 1 then let BRG = 16

So we will assume 64 first.. 22118400 / (64 / 115200) - 1 = 2... So SPRG = 2

With 16... 22118400 / (16 / 115200) - 1 = 12.. So SPRG = 11

There is no % error as you are using a crystal that yeilds exact BAUD rates... So both will give 115200

BRGH = 1.. use SPRG = 11
BRGH = 0.. use SPRG = 2