PDA

View Full Version : 18F26K22 - Unable to get 64MHz Internal clock speed



Balachandar
- 16th July 2014, 11:39
One of the attractions of the PIC 18F26K22 is its ability to execute code at 64MHz with its internal clock. But I am unable to get either 32MHz or 64MHz despite enabling the PLL.

The simple test program to blink an LED at 1Hz is shown below.

'--------------------------------------------
DEFINE OSC 64 'Clock Speed: 64MHz

'Fuse setting to select the Internal Oscillator, to enable PLL and to enable Primary Clock
ASM
__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H
ENDASM

OSCCON = %11110010 'Internal oscillator is set to 16MHz

OSCTUNE = %11000000 'HFINTOSC selected; PLL enabled; Factory calibrated frequency

ADCON0.0 = 0 'Disable ADC
ANSELA = 0
ANSELB = 0
ANSELC = 0

CM1CON0.7 = 0 'Disable comparator1
CM2CON0.7 = 0 'Disable comparator2

'Port direction
TRISA = 0 'All are Outputs
TRISB = 0 'All are Outputs
TRISC = 0 'All are Outputs

START:

PortC.3 = 1 : pause 500 'Turn the LED on and wait for half a second
PortC.3 = 0 : pause 500 'Turn the LED off and wait for half a second

GOTO START

END
'--------------------------------------------

I use PBPro 2.60C to compile and PICkit2 to program. There are no issues in compiling this code and programming a 18F26K22. But the code runs at one fourth the speed. 'Pause 500' statement produces a 2 second delay. I face the same problem if I want to run it at 32MHz (4 x 8MHz). In other words, it is as if the PLL does not get turned on. After compiling, the hex file opened in MeLabs programmer shows the status of the PLL as Enabled. If I change the first line of the code to DEFINE OSC 16, everything works fine at 16MHz; enabling or disabling the PLL in the code does not make any difference.

I have gone through the Oscillator section of the data sheet several times and I think I am doing everything required, but the PIC refuses to run at 64MHz speed. I will highly appreciate any help/suggestions to solve the problem.

Regards,
Bala

Demon
- 16th July 2014, 12:37
I also use PBP 2.60C on a 18F24K22 (same family of PICs). Looks like you're missing one register, try this:


' PIC 18F24K22 int osc 64MHz

asm
__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_ON_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
__CONFIG _CONFIG2H, _WDTEN_OFF_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _HFOFST_OFF_3H & _T3CMX_PORTB5_3H & _P2BMX_PORTC0_3H & _MCLRE_EXTMCLR_3H
__CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm

' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.

' Define LOADER_USED 1
DEFINE OSC 64

;INCLUDE "AllDigital.pbp"
;DEFINE SHOWDIGITAL 1

OSCCON = %01110000 ' I didn't need to set bit 7 of OSCCON for SLEEP
OSCCON2 = %10000100
OSCTUNE = %11000000

ADCON0 = %00000000 ' Disable ADC
ANSELA = 0
ANSELB = 0
ANSELC = 0

' Your code

CM1CON0.7 = 0 'Disable comparator1
CM2CON0.7 = 0 'Disable comparator2

'Port direction
TRISA = 0 'All are Outputs
TRISB = 0 'All are Outputs
TRISC = 0 'All are Outputs

START:

PortC.3 = 1 : pause 500 'Turn the LED on and wait for half a second
PortC.3 = 0 : pause 500 'Turn the LED off and wait for half a second

GOTO START

END


I just noticed I didn't set the MFINTOSC Frequency Stable bit in OSCCON, don't know the impact of that. I'd start by adding OSCCON2 first, I know I put that there for a reason, just can't remember - I got this code running back in January.

Robert

Dave
- 16th July 2014, 12:54
Here is what I have used in the past:
' WRITTEN BY DAVID PUROLA 09/18/2012
'
' WRITTEN FOR USE W/18F26K22 E/P @ 64 Mhz.
'
DEFINE OSC 64

#CONFIG
;----- CONFIG1H Options --------------------------------------------------
__config _CONFIG1H, _FOSC_INTIO7_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_ON_1H & _IESO_OFF_1H
;----- CONFIG2L Options --------------------------------------------------
__config _CONFIG2L, _PWRTEN_ON_2L & _BOREN_ON_2L & _BORV_190_2L
;----- CONFIG2H Options --------------------------------------------------
__config _CONFIG2H, _WDTEN_OFF_2H & _WDTPS_32768_2H
;----- CONFIG3H Options --------------------------------------------------
__config _CONFIG3H, _CCP2MX_PORTB3_3H & _PBADEN_OFF_3H & _HFOFST_OFF_3H & _T3CMX_PORTB5_3H & _MCLRE_EXTMCLR_3H
;----- CONFIG4L Options --------------------------------------------------
__config _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
#ENDCONFIG

OSCCON = %01111000 'ENTER SLEEP MODE ON SLEEP,16 MHZ,FODC<3:0>
OSCTUNE = %01000000 'ENABLE PLL FOR 8 OR 16 MHZ.
OSCCON2 = %00000000

Balachandar
- 16th July 2014, 16:53
Thanks a lot, Robert. On checking your code I noticed that bits <1:0> of register OSCCON are cleared in your code whereas I had entered them as 10 to select the Internal Oscillator Block. I corrected them to 00 to select Primary Clock as determined by CONFIG1H [FOSC<3:0>]. Register OSCCON2 settings do not seem to play any role in enabling PLL.

Now my 18F26K22 is running at 64MHz!

Thanks to Dave Purola too. In his code again, the last two bits of OSCCON are cleared.

My revised code is given below.

DEFINE OSC 64 'Clock Speed: 64MHz

asm
__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_ON_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_OFF_2L & _BORV_285_2L
__CONFIG _CONFIG2H, _WDTEN_NOSLP_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _CCP3MX_PORTB5_3H & _HFOFST_OFF_3H & _T3CMX_PORTB5_3H & _P2BMX_PORTC0_3H & _MCLRE_INTMCLR_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
__CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
__CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
__CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
__CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
__CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
__CONFIG _CONFIG7H, _EBTRB_OFF_7H
ENDASM

OSCCON = %11110000 'Internal oscillator is set to 16MHz; Primary Clock is selected as system clock.
OSCTUNE = %11000000 'HFINTOSC selected; PLL enabled; Factory calibrated frequency

ADCON0.0 = 0 'Disable ADC
ANSELA = 0
ANSELB = 0
ANSELC = 0

CM1CON0.7 = 0 'Disable comparator1
CM2CON0.7 = 0 'Disable comparator2

'Port direction
TRISA = 0 'All are Outputs
TRISB = 0 'All are Outputs
TRISC = 0 'All are Outputs

START:

PortC.3 = 1 : pause 500
PortC.3 = 0 : pause 500

GOTO START

end


- Bala