PDA

View Full Version : 18F4520 and internal osc & PLL



pwhitt
- 29th July 2005, 18:47
I am attempting to run an 18F4520 at 32MHz with the internal oscillator and PLL (8MHz x 4) and am having problems. I notice because I am using the hardwae UART and the timing is exactly 1/4 what I code for. I use the "DEFINE OSC 32" statement, then "DEFINE HSER_BAUD 38400" and notice the output from the PIC is at 9600. This would make sense if the internall oscillator was running at 1/4 what I coded for.

The config statements I used are as follows:

'clock, PLL and port config
OSCCON = $7F
OSCTUNE.6 = 1
ADCON1 = $0F
CMCON = $07
DEFINE OSC 32

'harware UART settings
DEFINE HSER_RCSTA 90H
DEFINE HSER_TXSTA 20H
DEFINE HSER_CLROERR 1
DEFINE HSER_BAUD 38400

According to the datasheet, the PLL is enabled by OSCTUNE.6 and the first nibble of OSCCON being 0111 should result in an internal oscillator frequency of 8MHz. If someone could please explain the discrepency I would greatly appreciate it.

Thanks a lot.

anmproj
- 29th July 2005, 18:54
Would you mind posting your code for uart setup and commands? Also, how do you have it hooked up to a PC? Are you using a MAX232. Sorry I dont know the answer to your questions, but I think what you might have, may be able to help me with my problems. Thanks.

pwhitt
- 29th July 2005, 19:02
The commands I provided were the UART setup.

The commands used to send data are simply HSEROUT and HSERIN. In the interest of saving space and not distracting from the problem by including loads of code no one will need to see, here is a snippet of relevant code from my software, which I have run successfully at 40MHz on an 18F4220.

'clock & PLL config
OSCCON = $72
OSCTUNE.6 = 1
ADCON1 = $0F
CMCON = $07
DEFINE OSC 32

'harware UART settings
DEFINE HSER_RCSTA 90H
DEFINE HSER_TXSTA 20H
DEFINE HSER_CLROERR 1
DEFINE HSER_BAUD 38400

'various port settings
TRISA = $FF
TRISB = $00
TRISC.4 = 0
TRISC.5 = 0
TRISD = $FF

'' symbols have been left out to save space in post

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''
'' main
InitWave= 0
InitDA = 0
PORTB = 0
LOW CLKEN
PAUSE 100 'allow guys to settle
HSEROUT [10,13,"..."]
PAUSE 500
'start A/D converters
HIGH CLKEN

'' main menu prompt ''''''''''''''''''''''''''''''''''''''''''
HSEROUT [10,13,10,13,"Startup successful."]
DispMenu: 'menu display, falls through
HSEROUT [10,13,"blah blah blah"]
HSEROUT [10,13,"blah blah blah"]

---------

On this PIC, RC6 and RC7 are TX and RX respectfully. I am using a max232 and it is connected as illustrated in any common 232/uart app note. There are loads of examples if you search mikroelectronika or similar DIY sites.

Bruce
- 30th July 2005, 01:57
Change OSCCON = $72 to OSCCON = $70 (%01110000).

pwhitt
- 1st August 2005, 12:35
Hi Bruce,

Thanks very much. I actually got the same advice from PIC Basic support - although the $72 value was given to me by support earlier, and matched teh value I was using. This does indeed fix it - I see now that the PIC is configured to use the "main oscillator" rather than the internal - but could you please explain why? I was assuming the internal oscillator was everything internal and the main oscillator was an external crystal or RC.

To avoid any confusion to those that may notice - the first OSCCON value I posted had OSCCON=$7F. This was copied/pasted from a file in which I was thoughtlessly monkeying with the values attempting to fix it.

Bruce
- 1st August 2005, 16:00
The primary oscillators include the External Crystal and Resonator modes, the
External RC modes, the External Clock modes and the internal oscillator block.

You set the "primary" oscillator with FOSC3:FOSC0 configuration bits - so with
the internal oscillator block set in CONFIG1H, and 8MHz or 4MHz selected in
OSCCON, you need System Clock Select bits SCS1:SCS0 set for "primary" for
PLL to work.

The PLL is available only when it's configured to use the internal oscillator
block as its "primary" clock source (FOSC3:FOSC0 = 1001 or 1000).

It's not exactly hit-you-in-the-face clear from looking at the datasheet, but
that's the only way to turn on PLL when using the internal osc.

pwhitt
- 1st August 2005, 17:32
Thanks so much for clearing that up Bruce - excellect.

juliogbox
- 15th September 2008, 06:38
I had exactly the same problem with PIC18F1330. But I could fix it using the Bruce advice:
movlw B'0111000'
movwf OSCCON, A

But there is a strange behavior of the MPLAB-SIM. If I do this:
movlw B'0111011'
movwf OSCCON, A

The simulator do not write the two lower bits, as you can see in this image (click to enlarge):

http://img526.imageshack.us/img526/7776/screengv9.th.jpg (http://img526.imageshack.us/my.php?image=screengv9.jpg)

But in the hardware the two bits are written, that's the reason because the PLL hadn't worked in my prototype.