Microcontroller's oscilation

Discussion in 'Embedded Systems and Microcontrollers' started by LauraB, Jan 3, 2014.

  1. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    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?
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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).
     
  4. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Whats the speed with this one and a crystal 11.0592?
    Thank you for the replay.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    To answer your question. The speed will be not be affected in any way, and your micro will run at a 11.0592MHz clock speed.
     
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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.
     
  8. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    OK, so I should almost always choose the same speed as crystal?
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    If you want your code to use accurate cycle timing to do anything, yes.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    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.
     
  11. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    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?
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The HI-Tech C compiler comes with a lot of examples. And here you will also find a good LCD example
     
  14. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    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?
     
  15. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Good point. I didn't read the question thoroughly...
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
Loading...