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.

    Henrik,

    Thank you very much for your example. I will give this a try today and post results. Sorry I did not respond earlier. I spent the last few days helping out with a charity walk to raise money for a little girl with type 1 diabetes. Sure was nice to be out in the open air walking by a river instead of being stuck in the dungeon working

  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.

    Ok, so this combines the working crude regulator code with Henriks example code. I havent programmed the pic yet but it does compile.

    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 BYTE
    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
        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
        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, [12,23,34,45,56], DutyCycle
        SettingChanged = 0                 ' Clear flag
      EndIf
    
      If Setting = 0 THEN 
      SETPOINT = 0
      endif
      
      If SETTING = 1 then
      Setpoint = 2
      endif
      
      If Setting = 2 then
      SETPOINT = 5
      endif
      
      IF setting = 3 then
      SETPOINT = 9
      endif
      
      IF Setting = 4 then
      SETPOINT = 14
      endif
      
      If Setting = 5 then
      Setpoint = 17
      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
    
    Goto Main

  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.

    Hi,
    I think that should work. However, since Setpoint doesn change if Setting doesn't change there's no need to assign a value to SetPoint each time thru the loop.
    You can move it into the If SettingChanged section, something like:
    Code:
    If SettingChanged THEN
        LOOKUP Setting, [12,23,34,45,56], DutyCycle            ' Assign inital value to DutyCycle variable
        LOOKUP Setting, [0, 2, 5, 9, 14, 17], SetPoint         ' Assign value to setpoint variable    
        SettingChanged = 0                 ' Clear flag
    EndIf
    Or you could do something like:
    Code:
    IF SettingChanged THEN
      Select Case Setting
        Case 1
           DutyCycle = 12   'Initial dutycycle for setting 1
           SetPoint = 0
       Case 2
           DutyCycle = 23
           SetPoint = 2
       Case 3
           '.......
           '.......
    End Select
    Try it, see which works best, compiles to the smallest code, is easiest to maintain and understand etc etc.

    Good luck!
    /Henrik.

  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.

    Good idea! I like your first example because there are less lines to scroll through, and it looks cleaner.

  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.

    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.

  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.

    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.

  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.

    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.

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