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.

    Ok I programmed and there are a couple of issues.

    There needs to be a time delay between each step up or down that the button is pressed. Right now, pressing either button and it is too fast to set a desired level. This delay cant be put into the loop though as the response of the regulator will be to slow.

    Also it doesnt fully turn off and seems to settle at a very low duty cycle when it should be zero.

    Code:
    IF but_0 then
    pause 75
    eNDIF
    if !but_0 then
             if SETPOINT>0 then
                SETPOINT=SETPOINT-1
               endif
               
        endif
        
    IF but_1 then
    PAUSE 75
    ENDIF    
        if !but_1 then
                if SETPOINT<19 then
                SETPOINT=SETPOINT+1
            endif
            
        endif
    This button code used before worked well.

  2. #2
    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.

    Code:
     If UpButton THEN
        PAUSE 75
        Setting = Setting + 1              ' Increment setting
        If Setting = 6 THEN Setting = 5    ' Make sure it doesn't go beyond 5
        SettingChanged = 1                 ' Set flag indicating new setting
      EndIf
    
    
      If DnButton THEN
        PAUSE 75
        Setting = Setting - 1
        If Setting = 255 THEN Setting = 0  ' Make sure it doesn't go below 0
        SettingChanged = 1                 ' Set flag indicating new setting
      EndIf
    Adding the PAUSE 75 fixed the run away issue and gives a quick response to a button press while still being easy to use.

    Still havent figured out why at DutyCycle=0 there is still a very low pwm signal that lasts for just a few uS. Or while at setting 5 it rolls over to setting 1.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    There's a mistake in my code.
    The Setting variable can go from 0 to 5 (6 settings) but there's only five entries in the first LOOKUP statement, the one that sets the initial dutycycle.

  4. #4
    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.

    I did actually catch that And yes, it does work. Or it did. Now I think I either messed it up or there is a hardware issue

  5. #5
    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.

    Code:
    Define OSC 20
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3
    
    
    PORTA = %00000000       ' Turn off all PORTA 
    PORTB = %00000000       ' Turn off all PORTB
    ADCON0 = %00000001
    ADCON1 = %01111000
    ADCON2 = %10000111
    
    TRISA = %11100111
    TRISB = %11000011
    
    
    UpButton VAR PortB.0   			' Active high button
    DnButton VAR PortB.1   			' Active high button
    
    Setting VAR BYTE
    SettingChanged VAR BIT
    DutyCycle VAR WORD
    SETPOINT var byte  'Output current level in 0.1A increments)100 = 1A)
    
    CURRENT VAR WORD
    LEDCURRENT VAR WORD
    LEDCURRENT1 VAR WORD
    high_duty var word
    low_duty var word
    
    
    
    Main:
    
      If UpButton THEN
      pause 75
        Setting = Setting + 1              ' Increment setting
        If Setting = 6 THEN Setting = 5    ' Make sure it doesn't go beyond 5
        SettingChanged = 1                 ' Set flag indicating new setting
      EndIf
    
    
      If DnButton THEN
      pause 75
        Setting = Setting - 1
        If Setting = 255 THEN Setting = 0  ' Make sure it doesn't go below 0
        SettingChanged = 1                 ' Set flag indicating new setting
      EndIf
    
      If SettingChanged THEN
        LOOKUP Setting, [0,100,150,200,240,254], DutyCycle
        LOOKUP Setting, [0, 2, 5, 9, 14, 17], SetPoint ' Assign value to setpoint variable 
        SettingChanged = 0                 ' Clear flag
      EndIf
      
    ADCIN 0, CURRENT
    LEDCURRENT = CURRENT * 49 /100 *2
    LEDCURRENT1 = LEDCURRENT/10
    
    CCP1CON = %00111100
    PR2 = 99
    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
    T2CON = 4
    
    if LEDCURRENT1 < setpoint then
        dutycycle=dutycycle+1
        if dutycycle => 300 then
        dutycycle = 300
    endif
    ENDIF
    if LEDCURRENT1 > setpoint then
        dutycycle=dutycycle-1
        IF dutycycle > 301 then
        dutycycle = 0
        
    ENDIF
    endif
    pause 100
    Goto Main
    Ok seem to be stuck. It no longer responds to button presses, turns off the LED if I raise the input voltage to 22V - should operate from 19 - 42V, and the pwm output is a constant low %. I have no idea what is causing this.

    The pause 100 at the end is just to slow things down so I can see whats happening on the scope.

  6. #6
    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.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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