PIC 18F4520 clock frequency calculations

Discussion in 'Embedded Systems and Microcontrollers' started by ddonn, Mar 2, 2011.

  1. ddonn

    Thread Starter New Member

    Feb 10, 2010
    I am trying to calculate the number of clock cycles at a clock frequency of 8MHz that corresponds to 2 bits at 9600 baud.
    Any suggestions?

    Also, what is the scaler value for 9600 baud @ 8 MHz and is there a relationship between the two?

    Thanks in advance.
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    I am not sure what you are talking about here. Do you want to setup your uart to fit 9600 baud using a 8MHz clock speed? Also which compiler do you use?
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    PIC controllers use 4 clock cycles per instruction cycle so at 8MHz 1 instruction cycle takes 500nSec. Data at 9600 baud takes 104.17uSec per bit (1/9600, that's what baud rate means) so 2 bits will take 208.33uSec.

    A 8MHz clock will never give you exactly 9600baud, the closest it gets is 9615 baud. That is an error if 0.16% which is close enough for most applications.

    If you simply must have exactly 9600 baud then you you will need to change your crystal to either 4.9152Mhz or 9.8304MHz, there are other frequencies you can use but those 2 will divide down to provide all the common communication baud rates.
  4. ddonn

    Thread Starter New Member

    Feb 10, 2010
    Cheers Alex, most helpful.