Wrong Frequency


Closed Thread
Results 1 to 6 of 6

Thread: Wrong Frequency

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Wrong Frequency

    Hi Everyone;

    I've got this code, and it's working fine, however the frequency of the pwm mesured with the oscilocope it's half the frequency configured.
    It is suposed to have 5Khz, however i've got 2.5Khz.

    Can anybody tell me what i'm doing wrong in the code?

    PIC12F683
    Code:
    Define OSC 8
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF
    
      
    '                                PINOUT 1 = IN; 0 = OUT
    ' ====================================================================
                  '76543210
     TRISIO     = %00000001
     
                  '76543210
       GPIO     = %00000000
       
     ANSEL = 1  'GP0 ANALOG             
     VRCON = 0  'VREF OFF
     CMCON0 = 7 'COMPARATOR OFF             
     ADCON0 = %10000000  'RIGHT JUSTIFY - 128
     
     PR2     = %00011000 'SET 24 - 5000Hz - 8Bits
     T2CON   = %00000110 'TIMER2 ON - PRESCALER 1:16 - Max Duty Value 100 - 1% Step
     CCPR1L  = %00000000
     CCP1CON = %00001100 'PWM ON
    Thanks
    Thanks and Regards;
    Gadelhas

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    You have to set the OSCCON register, since by default is set to 4 MHz and you define the osc as an 8 MHz one.

    Cheers

    Al.
    All progress began with an idea

  3. #3
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Thank you aratti for the fast answer to my problem. I changed the OSCCON register, and it worked at the desired frequency.

    Thank you once again!
    Thanks and Regards;
    Gadelhas

  4. #4
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Hmmm , I thought the DEFINE OSC x takes care of the frequency registers. I'm just curious, will it work if the internal oscillator is defined first and then the frequency is defined after that? In the next code, I just swapped the order of the first two lines in gadelhas original code.

    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF
    
    Define OSC 8
      
    '                                PINOUT 1 = IN; 0 = OUT
    ' ====================================================================
                  '76543210
     TRISIO     = %00000001
     
                  '76543210
       GPIO     = %00000000
       
     ANSEL = 1  'GP0 ANALOG             
     VRCON = 0  'VREF OFF
     CMCON0 = 7 'COMPARATOR OFF             
     ADCON0 = %10000000  'RIGHT JUSTIFY - 128
     
     PR2     = %00011000 'SET 24 - 5000Hz - 8Bits
     T2CON   = %00000110 'TIMER2 ON - PRESCALER 1:16 - Max Duty Value 100 - 1% Step
     CCPR1L  = %00000000
     CCP1CON = %00001100 'PWM ON
    Robert
    Last edited by rsocor01; - 5th December 2010 at 03:37.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    The configs and the OSCCON register if using an internal OSC is for setting the hardware.
    The PIC will then run at that speed. Run at the speed of the external OSC or the internal setup.

    All
    DEFINE OSC X
    does is to tell PBP how fast the chip is running. DEFINE OSC X does not set the chips speed.

    Because the two are completely different things the order does not matter.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    The configs and the OSCCON register if using an internal OSC is for setting the hardware.
    The PIC will then run at that speed. Run at the speed of the external OSC or the internal setup.

    All
    DEFINE OSC X
    does is to tell PBP how fast the chip is running. DEFINE OSC X does not set the chips speed.

    Because the two are completely different things the order does not matter.
    Ooh, I see. Dave thank you for your help.

    Robert
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

Members who have read this thread : 0

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