Having to define XTAL

Discussion in 'Embedded Systems and Microcontrollers' started by portista, Jan 20, 2016.

  1. portista

    Thread Starter New Member

    Jan 20, 2016
    2
    0
    Hi folks,
    I am new into eletronics and I'm currently programming a pic18f4685. However since I will be using delays, I cam up to this: after searching I found out that I can use __delay_ms(x) . However it asks to be defined a XTAL frequency. I had already instructed pic to use the internal oscilator and stated that OSCCONbits.IRCF0 will be 1 Mhz.
    Why do I have to define XTAL too?

    Greetings
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Because the PIC and the compiler are two different things. XTAL is a number the compiler uses independent of the hardware. If it is wrong everything still works, but on a different time scale. Why on earth would you use delays -- do you enjoy sitting around doing absolutely nothing?
     
  3. portista

    Thread Starter New Member

    Jan 20, 2016
    2
    0
    Well, using a display need delays, right? for making a led blink delays are necessary right? If not please advice me.

    Greetings
     
  4. thumb2

    Member

    Oct 4, 2015
    66
    4
    Because the delay_ms and similar functions do not use hardware timers as TMR0, TMR1, TMR2 etc.

    These function calculate the delay using a user defined frequency reference, which is defined through the XTAL macro.
    Hardware timers use the internal oscillator as frequency reference.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Using a display does not necessarily need delays. Many of them have the blink function already built in. You can also use a periodic timer to schedule events, thus freeing the processor to do other things instead of busy waiting in do-nothing loops.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Using functions such as __delay_ms(x) is perfectly fine.

    One day you will be working on something with 147 things all happening at once and wish you could squeeze a little more power out of your device. Come that day you will want to use the hardware to make delays, and not a time wasting loop.

    So for now you have to tell the compiler how fast the hardware is so it can run the software at the correct speed.

    And welcome to the forums!
     
  7. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    If you use external crystal, the frequency is unknown to the chip, and you would have to specify it anyway.
     
    Papabravo likes this.
Loading...