Microcontroller's oscilation

Thread Starter

LauraB

Joined Dec 15, 2011
43
Hi

I have a fast question. If I would use a crystal 11.0592Mhz. And set mC to work with 8Mhz or 20Mhz... What would happen?
 

t06afre

Joined May 11, 2009
5,934
You can not set a micro with a 11.0592 MHz crystal to work with 8 or 20 MHz clock speed. However you may use compiler directives in C. To inform your compiler which clock speed you are using. This may be used to calculate say software delays
 

tshuck

Joined Oct 18, 2012
3,534
If you set the microcontroller to use a crystal, it will use the crystal. Just make sure to set the configuration for the proper frequency range for the crystal (see the datasheet).
 

tshuck

Joined Oct 18, 2012
3,534
That preprocessor directive just tells any function calls that the clock is a specific value ( what t06afre was describing for delays), it does not set the clock frequency for the device, just what speed the code on the device thinks the device is running at.
 

John P

Joined Oct 14, 2008
2,025
You'd get a working program, but any operations that require use of timing would be off proportionally to the speed error, so if you used an 11.0592MHz crystal and said "#define _XTAL_FREQ 20000000" and then you had a "delay(1msec)" command, that would give a delay which would be too long by a factor of 20/11.0592. And if you used the built-in UART with the compiler's control commands, you'd never get the right baud rate.
 

MrChips

Joined Oct 2, 2009
30,707
OK, so I should almost always choose the same speed as crystal?
This is not a hard and fast rule. There are some pieces of information missing.

What microcontroller chip are you using?
What compiler are you using?

The external xtal sets the clock frequency of the mcu.
The #define _XTAL_FREQ tells the complier what this frequency is if it needs this in its mcu configuration or timer calculations. Places where you would expect the compiler to use this information is in any delay( ) functions.

Whether or not the compiler/config uses this information correctly, you can expect that certain functions will not work correctly. For example the baud rate of the UART may not be accurate enough and hence cause data errors.
 

Thread Starter

LauraB

Joined Dec 15, 2011
43
I'm using PIC16F887. I'm switching between microC and HiTech.
I think I get it. I just need to find out how to calculate the delays etc.
I can't decide if I like more microC or Mplab. In microC there are pre-defined functions. It's very helpful. I don't need to know how to implement for example LCD. I can just focus on the rest of the code. But, on the other hand. If I write the function on my own, I now it better, so i can adjust it as I need. Should I keep learning microC or stay with more difficult Hitech?
 

t06afre

Joined May 11, 2009
5,934
OK, so I should almost always choose the same speed as crystal?
Some micros like the PIC18F series have a PLL that can double or quadruple the oscillator frequency. In such a cases you should use the system operating clock speed. And not the crystal frequency
:D
 

t06afre

Joined May 11, 2009
5,934
I'm using PIC16F887. I'm switching between microC and HiTech.
I think I get it. I just need to find out how to calculate the delays etc.
I can't decide if I like more microC or Mplab. In microC there are pre-defined functions. It's very helpful. I don't need to know how to implement for example LCD. I can just focus on the rest of the code. But, on the other hand. If I write the function on my own, I now it better, so i can adjust it as I need. Should I keep learning microC or stay with more difficult Hitech?
The HI-Tech C compiler comes with a lot of examples. And here you will also find a good LCD example
 

Thread Starter

LauraB

Joined Dec 15, 2011
43
Are you saying I should choose Hi-Tech?
edit.
Before I was able to find many examples. Now i cant. Does anyone know where are they?
 

tshuck

Joined Oct 18, 2012
3,534
Some micros like the PIC18F series have a PLL that can double or quadruple the oscillator frequency. In such a cases you should use the system operating clock speed. And not the crystal frequency
:D
Good point. I didn't read the question thoroughly...
 

t06afre

Joined May 11, 2009
5,934
Are you saying I should choose Hi-Tech?
edit.
Before I was able to find many examples. Now i cant. Does anyone know where are they?
I know in later versions after Microchip took over Hi-Tech C, and the compiler was renamed to the Microchip XC8 compiler. The examples was taken out. If the compiler is installed into a folder named XC8. Like "C:\Programfiles\Microchip\xc8\v1.12" You will probably not have the samples folder. But else look in the "C:\Programfiles\HI-TECH Software\PICC\9.83\samples" folder. Anyway I include the LCDdemo files here
 

Attachments

Top