Log in

View Full Version : 20MHZ External Oscillator Settings



ShaneMichael
- 22nd October 2011, 00:20
I'm using an 18F2550, and having trouble figuring out the oscillator settings. Everything worked fine with 4Mhz Crystal, not so much with 20Mhz. I don't even get the crystal to work when looking at it with a scope. Ideally I need AD values sampled about 300 times in 10ms. I could go to 200-300 samples in 20 ms or slower, but what I have is too slow. With the 4Mhz XTAL I was getting about 30 samples in 20ms.

I'm using the bootloader, I programmed the PIC with the 20mhz.hex file using a programmer.
Then I try loading my code and I'm not getting any where. The boat loader was all working with 4Mhz XTAL.


I believe the issue is the CONFIG1H, or a multiplier that I'm not setting or uderstanding HS vs XT.

Here is all the configuration etc. I'm sure they can be optimized past my understanding, so comment on anything that should be different/better. The ADCON settings are at the bottom, I'll also include my A to D code, I'm doing it the recommended way from other posts I found here for A to D (not using the ADCIN). I'm not having any issue with the code, just including it so you have a more complete picture of what I'm doing.
Thanks,

Shane




DEFINE LOADER_USED 1

Define OSC 20

'Define LCD Port
DEFINE LCD_DREG PORTB 'Port used for LCD Data
DEFINE LCD_DBIT 4 'First Pin connected to LCD DB4
DEFINE LCD_RSREG PORTB 'Port used for LCD RS line
DEFINE LCD_RSBIT 3 'Pin used for LCD RS line
DEFINE LCD_EREG PORTB 'Port used for LCD E Reg
DEFINE LCD_EBIT 0 'Pin used for LCD E line
DEFINE LCD_BITS 4 'Four bit comm mode
DEFINE LCD_LINES 2 '2 Line LCD
DEFINE LCD_COMMANDUS 750 'Delay between sending LCD commands
DEFINE LCD_DATAUS 5 'Delay time between data sent


DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 164 ' Enable brg, Enable transmit, BRGH = 1

'DEFINE HSER_SPBRG 12 ' 19200 Baud @ 4MHz, 0.16% pg 245 spbrg vals
DEFINE HSER_SPBRG 64 ' 19200 Baud @ 20MHz, 0.16% pg 245 spbrg vals
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

'Re-Configure default values from the PBP 18F2550.inc and MCSP/MPASM .inc
'These are commented out in the .inc file

'__CONFIG _CONFIG1H ,_FOSC_XT_XT_1H ' Works with 4Mhz
'_CONFIG1H ,_FOSC_XTPLL_XT_1H '20Mhz?

asm
__CONFIG _CONFIG1H ,_FOSC_XTPLL_XT_1H
__CONFIG _CONFIG2L ,_BOR_ON_2L & _BORV_2_2L


endasm

INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
INCLUDE "Elapsed_INT-18.bas" ; Elapsed Timer Routines

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ClockCount, PBP, yes
INT_Handler TMR0_INT, _ToggleLED1, PBP, yes
INT_Handler RX_INT, _Check_Command, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE TMR1_INT
@ INT_ENABLE TMR0_INT
@ INT_ENABLE RX_INT

OSCCON = %01101100

ADCON0 = %00000000 'Setup
ADCON1 = %00001010 'Port A0 Analog, rest to digital

ADCON2 = %10101000 'A/D Right Justified for 10 bit, 8 bit needs Left.
'12 Tad, Osc/2
PortC = $00







'****************************CheckVoltage Sub*****************************
CheckVoltage:
MaxV1ADVal = 0
MaxV2ADVal = 0
MaxV3ADVal = 0
For V1i = 0 to 19
Gosub GetV1
Gosub GetV2
Gosub GetV3
'Pauseus 25
Next V1i

GoSub CheckState

Return
'***************************Get V1****************************************
GetV1:
ADCON0 = %00000000 ' Select Channel AN0
ADCON0.0 = 1 'Turn on A/D
GoSub GetADVal
V1ADVal.HighByte = ADRESH 'PLACES THE HIGH AND LOW BYTE
V1ADVal.LowByte = ADRESL 'INTO VAR CHAN0
MaxV1ADVal= V1ADVAL Max MaxV1ADVal
Return
'***************************Get V2****************************************
GetV2:
ADCON0 = %00000100 ' Select Channel AN1
ADCON0.0 = 1 'Turn on A/D
GoSub GetADVal
V2ADVal.HighByte = ADRESH 'PLACES THE HIGH AND LOW BYTE
V2ADVal.LowByte = ADRESL 'INTO VAR CHAN0
MaxV2ADVal= V2ADVAL Max MaxV2ADVal
Return
'***************************Get V3****************************************
GetV3:
ADCON0 = %00001000 ' Select Channel AN2
ADCON0.0 = 1 'Turn on A/D
GoSub GetADVal
V3ADVal.HighByte = ADRESH 'PLACES THE HIGH AND LOW BYTE
V3ADVal.LowByte = ADRESL 'INTO VAR CHAN0
MaxV3ADVal= V3ADVAL Max MaxV3ADVal
Return
'***************************Get A/D Val*************************************
GetADVal:
ADCON0.1 = 1 'Start Conversion
WHILE ADCON0.1=1 'Waite for it to Complete
WEND
RETURN

circuitpro
- 22nd October 2011, 04:11
Well, I don't know about the PLL, but I expect we should get your crystal oscillating first. I am using a 20MHz crystal with a PIC18F6722 and it oscillates fine using:

@ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H

try that, and see if at least the crystal wakes up.

circuitpro
- 22nd October 2011, 04:30
Forgot to mention... I assume you have 2 suitable size caps from the crystal leads to ground. Somewhere near 18-22 pF depending on the crystal.

mackrackit
- 22nd October 2011, 04:43
Yes, get rid of the PLL.

Charles Linquis
- 22nd October 2011, 04:56
If you are using the PLL then you should be using a 5Mhz XTAL to get 20Mhz. The PLL multiplies by 4, so that will get you 20.

If you have a 20Mhz Xtal, set the oscillator for HS and turn the PLL off as macrakit says.

ShaneMichael
- 22nd October 2011, 07:58
Thanks for the advice, I do have a 20Mhz Xtal and 20pf caps for it, It's set up just like the 4Mhz, as far as the hardware side goes. Is the PLL only for the USB, or does it speed up the Pic's code execuition also? I keep reading myself in circles in the data sheet trying to figure that one out. Should the OSCCON settings be removed?
I'll try the configuration from circuitpro's post.
Thanks,

mackrackit
- 22nd October 2011, 08:23
I did not notice the OSCCON before. It is not needed for your setup.

The PLL is for anytime you need to run the chip at a different speed than the main OSC. USB is not the only reason.
The 2550 can be set to run at
1, 1.33, 2, 4, 16,32, or 48 MHz with a 4 MHz external.

You will need to DEFINE OSC x to what ever the actual chip speed is if you PLLed it.

ShaneMichael
- 23rd October 2011, 08:30
I still don't have the 20Mhz XTAL resonating, these are my settings now.
I have this commented out in the include file:


;__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
;__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H

In my program I removed the OSCCON settings.
I'm setting the HS in my program:


asm
__CONFIG _CONFIG1H ,_FOSC_HS_1H
__CONFIG _CONFIG2L ,_BOR_ON_2L & _BORV_2_2L


endasm

I can jumper the 4Mhz XTAL into the circuit and it will resonate, I move the jumpers to the 20Mhz XTAL, I don't get anything.

I verify with a 200Mhz Fluke scope, and I also have an LCD that gives me a read out. The LCD works with the 4Mhz XTAL jumpered in and I have a nice clean wave form on the scope. The LCD cycles slow and I get garbage sometimes. My guess is that's because I still have the OSC DEFINE 20.

I though maybe something was up with the 20Mhz XTAL, so I tried another one with some short leads soldered on so it makes a good connection in the breadboard. Second 20Mhz XTAL does't resonate.

I tried 15, 17 and 20pf caps for the XTAL, no difference.

So I'm stumped, I do have a question on Mackrackit's Post, saying it can run at:
"1, 1.33, 2, 4, 16,32, or 48 MHz with a 4 MHz external"

It looks like all the settings divide the external XTAL speed, I don't see how I would get 16 or 32Mhz? If I can get 16 or 32Mhz with my 4Mhz XTAL I may have enough speed. At least it's worth a try.

Thanks, Let me know where I'm going wrong.
Shane

mackrackit
- 23rd October 2011, 08:59
I do not use crystals so I can not comment on that.
Here are the configs to run at 48 MHz with a 4 MHz resonator.


DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L

ShaneMichael
- 24th October 2011, 09:06
Thanks for the settings, that allowed me to follow the table in the data sheet and make sence of the settings in the .inc files. This is what I have figured out so far:

I can use DEFINE OSC for 4, 16 and 20Mhz. Anything else I get a compile error, " Integer not previously defined.."

So I used the old reliable 4Mhz XTAL and configured for 16Mhz CPU instead of the 48Mhz in Mackrackit's reply. That worked, but then my DT timer was running at about 1/4 speed. If I put Define OSC back to 4, my timing was fixed.

Did a few more tests and found that I could set the configs to 48, 32,24 or16Mhz, the number of A/D conversions seems to be the same. I always seem to need the Define OSC 4.


Next, was to get the 20Mhz XTAL working, using what I learned from the last 2 hours of testing, I was sure I could plug the 20Mhz in and be set to get back to coding. Nope-not that lucky!

After more time I got it working, then I connected the scope and it stopped working. Seems that connecting the scope to the 20Mhz signal destroys it, don't know if its the extra capacitance from the probes?

The tool that was supposed to help me figure out why I wasn't resonating was causing the issue...go figure.

I also timed the execution speed (number of A/D conversions per second) for different cpu speeds, they all seem the same ( 48, 32 24 and 16Mhz). I left the DEFINE OSC 20 and the DT timer looks spot on.

Seems that the (CPUDIV1:CPUDIV0) must only affect the USB speed? At least for this model of PIC?

Here is what I ended up with:



__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H


And it's working, although I'm still confused on the execution speed not changing when changing the (CPUDIV1:CPUDIV0) settings?

Thanks for your help, let me know if you have any advice regarding my current confusion.

Shane

mackrackit
- 24th October 2011, 18:58
I am not sure if measuring the ADC time is the best test. There are other things that effect the ADC speed.

You might try setting up an LED to blink at 1 Hz then test.

The settings are for the MCU speed, not just USB.

Charles Linquis
- 25th October 2011, 02:04
I guess that no one reads my posts. I have mentioned numerous time that you can do a superb job of measuring your PICS actual speed by
using a routine such as:

DEFINE OSC X

LEDON

For X = 1 to 60
PAUSE 60000
Next X

LED OFF

Of course, you don't have to time for a full hour, but the accuracy gets better the longer you wait.
If have

If you use DEFINE OSC 20 and the LED stays on for 31 minutes, you are actually running at

(60/31) * 20MHz = 38.7096Mhz.

Just make sure you don't have interrupts running when you do the timing.

cncmachineguy
- 25th October 2011, 15:53
charles I read them :) with great intrest!!

ShaneMichael
- 26th October 2011, 00:22
Thanks for the speed test, I'll give it a try and post my results.

ShaneMichael
- 26th October 2011, 07:34
I tried the speed test to check code execution time at 24Mhz and 48Mhz. I left the DEFINE OSC 20 for both tests. I used pause loops like Charles suggested, and used 10 minute long tests. I know longer would be better, but I think 10 minutes should show any difference. Both times the LED was on for 10 minutes, the best I could measure with a clock second hand.


I only changed the _CONFIG1L setting "_CPUDIV_OSC1_PLL2_1L" and _CPUDIV_OSC3_PLL4_1L between the tests. So I'm still scratching my head, seems if the CPU was running at 48Mhz but my define is at 20Mhz, it should have taken 4.1 minutes. So it seems the clock speed for code execution is derived directly from the external XTAL?

My A/D sampling frequency is now fast enough for my needs, as long as I keep the rest of my code in check I should be OK. I'd still like to understand why this is happening (or not happening)???
Thanks
Shane

Here is the code I used:


'_CPUDIV_OSC1_PLL2_1L '48 Mhz
'_CPUDIV_OSC3_PLL4_1L '24 Mhz

DEFINE LOADER_USED 1
asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L ,_BOR_ON_2L & _BORV_2_2L


endasm

DEFINE OSC 20

X VAR Byte

TRISC.0 = 0

Portc.0 = 0

For X = 1 to 10 'Turn LED on for 10 min
PAUSE 60000
Next X

Start:
Portc.0 = 1 'Start Toggling LED
For X = 1 to 10
PAUSE 100
Next X
Portc.0 = 0
For X=1 to 10
Pause 100
Next X
Goto START

mackrackit
- 27th October 2011, 01:44
http://www.picbasic.co.uk/forum/showthread.php?t=5806&p=68359#post68359
(http://www.picbasic.co.uk/forum/showthread.php?t=5806&p=68359#post68359)