1
votes

Today I started a project to try a few things with the PIC18F45K22. I had some timing problems with previously written code for other controllers. It turned out that there was something wrong with the clock frequency of the controller.

As the datasheet tells me, I should have a default clock frequency of 1MHz when using the internal oscillator. According to my oscilloscope I measured a clock frequency of 250kHz on the OSC2 (RA6) pin. So I started sniffing around in the datasheet and came across table 2-3 from the datasheet. This table says OSC2 pin will be 'Clock/4' when the controller is in SLEEP mode, which could be the source of my problem as 1MHz/4 = 250kHz. According to chapter 3.0 the controller will start with PRI_RUN mode when the device is reset, unless the Two-speed Start-up is enabled. So to be sure I disabled this mode by using the following configuration line:

#pragma config IESO = OFF

This line will disable the internal/external oscillator switchover bit, which will disable Two-speed Start-up according to paragraph 2.12.1.

Right now I tried multiple settings of the OSCCON, OSCCON2, OSCTUNE registers, which all failed. At this point I can't think of anything else what is causing this 'issue'. I hope someone will spot my error so I can run the device on the specified clock frequencies.

Currently I set these configuration bits:

#pragma config WDTEN = OFF
#pragma config FOSC = INTIO7 // Make clock visible on OSC2 pin
#pragma config LVP = OFF 
#pragma config DEBUG = OFF
#pragma config IESO = OFF // Disable Two-speed Start-up 

// Entry of application
int main(int argc, char** argv)
{
    OSCCONbits.IRCF = 0b011; // default frequency
    // Tried different OSCCON; OSCCON2; OSCTUNE settings, without succes
}

Datasheets(1): http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf

1
As it says in section 2.6.1 of the datasheet, CLKOUT will be Fosc/4 when used with the internal oscillator. If you've configured a 1MHz oscillator then you should expect to see 250KHz on CLKOUT, which represents the rate at which instructions are executed (4 clock cycles per instruction on these PICs). Everything is working as expected.Roger Rowland
@RogerRowland, I totally missed that critical piece of information! Thanks for pointing it out, the clock is indeed correct then. With that I noticed another error I made while converting the previously written code I tried on this controller, with that solved everything is working now.LimpSquid

1 Answers

1
votes

Roger Rowland answered my question:

As it says in section 2.6.1 of the datasheet, CLKOUT will be Fosc/4 when used with the internal oscillator. If you've configured a 1MHz oscillator then you should expect to see 250KHz on CLKOUT, which represents the rate at which instructions are executed (4 clock cycles per instruction on these PICs). Everything is working as expected.