Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

newb question on defineing crystal speed

Status
Not open for further replies.

large_ghostman

Well-Known Member
Most Helpful 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
 
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.
 
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.
 
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.
 
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
 
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.
 
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
 
Thanks Ian, The reason no Libs is I am doing a article! the 30F is almost done, the next one is totally different. I picked Silicon Labs and Kiel up much quicker. No idea why, However I have switched over to MPLAB X which is more like S Labs Studio. That might make it easier. I prefer pic datasheets tho.
I got a whole load of crystals from china a while back. I picked them using baudcon program, so I have loads of strange values to give me exact baud rates higher up. Also I got some samples of rs232 chips that are meant to be able to handle 1Mbsp!!!
I doubt they will but I want to see how ugly it gets the higher you go! Also spending most of my time trying to get radio stuff together. Building a dipole but waiting for connectors etc.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top