Altering a variable in a loop.


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    At the start of the program before the loop, I made sure Setting = 0. Works very well now. Still a minor issue of the pwm signal never turning off though, even on a setting of 0.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    Hi,
    I think the problem is here:
    Code:
    CCP1CON = %00111100            '<-- Here you actually set the two LSB's of the dutycycle register.
    PR2 = 99                                  ' <--There's no need to set PR2 every time thru the loop.
    high_duty=(dutycycle>>2)        'high 6 bits in CCPR1L
          low_duty=(dutycycle<<6)         'low two bits for CCP1CON 
          'low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>  
          low_duty.4=0                     'PWM configuration bit 
          low_duty.5=1                     'PWM configuration bit
    CCPR1L=high_duty
    CCP1CON = $0C                       '<- Then you reset the two LSB's of the dutycycle register.
    T2CON = 4                               '<- No need to set this every time
    So, the glitch probably comes from the fact that even when your dutycycle variable is 0 you actually set the dutycycle to 4 (the two LSBs in CCP1CON being set by CCP1CON = %00111100) and then back to 0 again by CCP1CON = $0C.

    Set up the module (ie CCP1CON and PR2) at the beginning of the program. If you need to use the lower two LSBs of the dutycycle then assign those bits in CCP1CON individually each time thru the loop. Don't mess with PR2 or T2CON every time thru the loop.

    Finally, you do a lot of bit shifting an dflippin on the Low_Duty variable but you never actually USE it to set the dutycycle, you're only using the 8 high bits in CCPR1L.

    /Henrik.

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