18F4520 and internal osc & PLL


Closed Thread
Results 1 to 8 of 8
  1. #1
    pwhitt's Avatar
    pwhitt Guest

    Default 18F4520 and internal osc & PLL

    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.

  2. #2
    anmproj's Avatar
    anmproj Guest


    Did you find this post helpful? Yes | No

    Default Code for UART

    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.

  3. #3
    pwhitt's Avatar
    pwhitt Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Change OSCCON = $72 to OSCCON = $70 (%01110000).
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    pwhitt's Avatar
    pwhitt Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    pwhitt's Avatar
    pwhitt Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks so much for clearing that up Bruce - excellect.

  8. #8
    juliogbox's Avatar
    juliogbox Guest


    Did you find this post helpful? Yes | No

    Default Bug in MPLAB?

    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):



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

Similar Threads

  1. OSCON setting for 18F4520
    By Dennis in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 29th November 2009, 15:18
  2. blink.bas for 18f4520 for newbie - HELP
    By Dennis in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th November 2009, 18:45
  3. Comparators on 18F4520
    By RacerX10 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 27th August 2009, 23:33

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts