Snippet:Internal oscillator

From Curuxa

You always must setup one type of oscillator for your microcontroller to work. Most Curuxa examples use the internal oscillator.

Read section Oscillator configurations or Oscillator Module in the datasheet of your microcontroller.

Each instruction is executed every 4 clock pulses. For example, if you setup the internal oscillator to Fosc = 8MHz, then Tosc = 1/Fosc = 125ns (time between oscillator pulses), therefore Tcy = 4*Tosc = 4*125ns = 500ns (time between instructions). Fcy = 1/Tcy = Fosc/4 = 2MHz (2 million instructions per second). The higher the oscillator, the faster the CPU, but the more power consumption.

Use the internal oscillator

To configure your microcontroller to work with the internal oscillator (so you don't need external circuitry) you must setup the configuration bits as _INTRC_IO or _INTOSCIO (depending on the model).

Setup frequency

By default, the internal oscillator runs at 31.25kHz (MBP18) or 4MHz (MBP8, MBP14, MBP40). You can choose other speed changing the bits IRCFx.

See datasheet of your PIC for the list of available configurations.

Usual configurations:

(IRCF2, IRCF1, IRCF0 = frequency)

  • 111 = 8 MHz
  • 110 = 4 MHz
  • 101 = 2 MHz
  • 100 = 1 MHz
  • 011 = 500 kHz
  • 010 = 250 kHz
  • 001 = 125 kHz
  • 000 = 31 kHz
//Set the internal oscillator to 31.25kHz
void SetIntosc31kHz(){
	IRCF2=0;
	IRCF1=0;
	IRCF0=0;
}
//Set the internal oscillator to 8MHz
void SetIntosc8MHz(){
	IRCF2=1;
	IRCF1=1;
	IRCF0=1;
}