Having to define XTAL

Thread Starter

portista

Joined Jan 20, 2016
2
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
 

Papabravo

Joined Feb 24, 2006
21,225
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?
 

Thread Starter

portista

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

Greetings
 

thumb2

Joined Oct 4, 2015
122
Why do I have to define XTAL too ?
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.
 

Papabravo

Joined Feb 24, 2006
21,225
Well, using a display need delays, right? for making a led blink delays are necessary right? If not please advice me.

Greetings
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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!
 
Top