Altering a variable in a loop.


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Aug 2012
    Location
    Comodoro Rivadavia - Patagonia Argentina
    Posts
    51


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    not quite understand the idea if you're needing, but I work with a MAP for a car, what I did was to implement a hardware PWM (in the background) and a TMR0 interrupt.
    Within the interrupt, which is to read the ADC and modified the pwm ...
    With all this, I required no implementing a LOOP and my program was much more efficient

    Note: (do not know if the translation is fine >> background = the 2nd plane?)

  2. #2
    Join Date
    Aug 2012
    Location
    Comodoro Rivadavia - Patagonia Argentina
    Posts
    51


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    would fail to correct the fuses and setup the interruption, but more or less the idea was so (I have to find where I have the complete original program that works well) ... ¨ I understood correctly?

    Code:
    'OPTION_REG =%00111111
    PORTA=0:TRISA=%00000001
    PORTB=0:TRISB=%00000000
    PORTC=0:TRISC=%00000000
    PORTD=0:TRISD=%00000000
    ADCON1= %10001110 '
    T2CON = %00000101
    PR2=249
    '-----------------------------------------------------------------
    @ DEVICE PIC16F877
    @ DEVICE PIC16F877, WDT_OFF
    @ DEVICE PIC16F877, PWRT_OFF
    @ DEVICE PIC16F877, PROTECT_OFF
    @ DEVICE PIC16F877, XT_OSC
    
    DEFINE OSC 4
    '**** configuracion canal ADC (ADC chanel setting) ***********
    DEFINE ADC_BITS 10 
    DEFINE ADC_CLOCK 1 
    DEFINE ADC_SAMPLEUS 3 
    '*********** Configuracion HPWM (HPWM PRESETING) *************
    DEFINE CCP1_REG PORTC 
    DEFINE CCP1_BIT 2 
    DEFINE CCP2_REG PORTC 
    DEFINE CCP2_BIT 1 
    
    HAM var word
    X VAR WORD
    DUTY VAR BYTE
    CCP1CON.2=1
    CCP1CON.3=1
    
       On Interrupt Goto int_TIMER0
       INTCON = %10100000
    
    
    Menu:
    DUTY=50
    GOSUB salida
    GOTO Menu
    
    salida:
    PR2=((1024-HAM)*/5625)/100+24
    X=(PR2+1)* DUTY /25
    CCP1CON.4=X.0
    CCP1CON.5=X.1
    CCPR1L=x>>2
    RETURN
    
    Disable
    
    
    int_TIMER0:
    ADCIN 0,HAM
    INTCON = %10100000
    Resume 
    Enable
    regards

  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.

    jmgelba,
    The key is to only execute the lookup (or other) code once each time you actually CHANGE the setting - not all the time. If you insist on having it inside a big loop you need a flag to indicate that the setting has changed and that the dutycycle needs to be reset to a new base value.

  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.

    That's exactly it. New current level gets set, pwm is set to a beginning level at the start of the loop. After one loop the pwm is set by the feedback, ignoring the initial start level.

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

    Here's the basic principle of using the flag method:
    Code:
    UpButton VAR PortB.0   			' Active high button
    DnButton VAR PortB.1   			' Active high button
    
    Setting VAR BYTE
    SettingChanged VAR BIT
    DutyCycle VAR BYTE
    
    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
    
    
      
      ' Your regulator code here...
      ' Your regulator code here...
      ' Your regulator code here...
    
    Goto Main
    /Henrik.

  6. #6
    Join Date
    Aug 2012
    Location
    Comodoro Rivadavia - Patagonia Argentina
    Posts
    51


    Did you find this post helpful? Yes | No

    Default Re: Altering a variable in a loop.

    very good your contribution! I find it very helpful to see the logic with other people who program to learn more every day... Thanks 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