trouble with FLAGs


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78

    Default trouble with FLAGs

    hello everyone, I am trying to do a PWM with a few leds connected to PortB and have a switch that is connected to one of the pin in PortC. I want to halt the design when I pressed the switch once.

    I have the following in my code.
    But I have to press the switch twice for the PWM to stop and twice for the PWM to continue. but I only want to halt the design when I press the switch once. I have set the flag = 1 at the top of my program... and when the switch is pressed I clear the flag.. any help on this will be greatly appreciated. Many thanks in advance

    srigopal



    ==============================================
    Flag = 1
    For Duty = 0 to 999

    If Switch = 0 Then
    Flag = ~Flag

    Endif

    if(Flag = 0) then
    Duty = Duty - 1
    Endif
    Flag = 1

    For cycles = 0 to Cycle1
    Portb = %00000000
    Pauseus 999 - Duty
    Portb = %00000010
    Pauseus Duty
    Next cycles

    Next Duty

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Try something like this.
    Code:
    IF Switch = 0 Then
        Flag = ~Flag           ' Invert Flag bit
        WHILE Switch = 0   ' Wait for button release
        WEND
    ENDIF
    This allows your Switch test routine to control toggling the Flag bit + Switch debounce. Then do whatever you need afterwards based on the Flag bit logic state.

    Note: Be sure to use a pull-up on your Switch input, and release the Switch quickly to avoid hanging in the test routine or odd results with a floating input.
    Last edited by Bruce; - 9th November 2004 at 21:07.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce, I have tried that.. but when I hold the switch down.. the output of the LEDs is not stable, it jumps to the NEXT LED lets say that I want to stop the program when the LED is 35% on .. when I have the switch pressed the LED seems to jump to full brightness .. and when I release it the LED comes back to where I wanted to stop it. How do I avoid that from happening..

    but when I take the while statement out of the program, the entire switch works but the flag does not refresh.. then the nest time I would have to press the switch multiple times to make it stop.. it;s very unpredictable.

    I even included the a resister between VCC to the Pin and then PIN to GROUND I have the switch. i dont think that my configuration is wrong is it?

    Hope there is another solution to this

    srigopal

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi srigopal,

    Can you give a little more detail on exactly what it is you're trying to do?

    Do you want this switch to "only" stop the duty cycle when pressed, or do you want the switch to provide a "stop" or "go" type action?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    here is what I plan to do... I want to include a switch that can stop the pwm just by pressing the switch once and letting it go. and then start it when I press it again and let it go. this is why the code does not work because the flags are not refreshed.

    But I need to do the same with another switch.. which only needs to be pressed to stop the pwm. so two switch is what I need really.


    one that only needs to be pressed once to stop the pwm.... and the other to be pressed whenever you want it to stop and go type of thing. (one pressed stop the pwm.. the other press continue where it left off )

    srigopal

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi srigopal,

    There are quite a few ways to do this. Here's one. This version just stops your PWM change.
    Code:
    Stop_Switch VAR PORTC.0  ' Whatever pin you prefer
    Go_Switch   VAR PORTC.1  ' Whatever pin you prefer
    Duty        VAR WORD     ' Duty cycle
    LowCycle    VAR WORD     ' Low cycle
    HighCycle   VAR WORD     ' High cycle
    Period      VAR WORD     ' Frequency period
    Cycles      VAR WORD     '  
    Active      VAR BIT      ' Active/Inactive flag bit
    YEP         CON 1        ' PWM will be Active 
    NOPE        CON 0        ' PWM will not be Active
    End_Cycles  CON 500      ' Increase value to slow PWM duty cycle change
    
    Active = YEP             ' PWM defaults to Active
    
    TRISC = %00000011        ' Make em inputs
    TRISB = 0                ' All outs
    
    Period = 500             ' Set frequency here. 500 = ~1.983kHz
                             ' 250 = ~3.825kHz, etc,
    Duty = 250               ' Set whatever you prefer here to start with
                             ' 250 = ~50% (1/2 of period)
    Main:    
        IF Go_Switch = 0 THEN    ' Go_Switch pressed?
           Active = YEP          ' Yes, toggle flag bit
        ENDIF
    
        IF Stop_Switch = 0 THEN  ' Stop_Switch pressed?
           Active = NOPE         ' Yes. toggle flag bit
        ENDIF
        
        IF Active = YEP THEN
           IF Duty < Period THEN
              Duty = Duty + 1
           ELSE
              Duty = 0           ' Limit Duty from 0 to 100%
           ENDIF
        ENDIF
        
        ' Compute high & low cycles of Period   
        LowCycle = Period - Duty
        HighCycle = Period - LowCycle
        
        For Cycles = 0 to End_Cycles
            Portb = %00000000 
            Pauseus LowCycle
            Portb = %00000001
            Pauseus HighCycle
        Next Cycles
        
        GOTO Main
    And if you want to stop PWM altogether, then a slight change in this routine would do it for you.
    Code:
       IF Active = YEP THEN
          For Cycles = 0 to End_Cycles
               Portb = %00000000 
               Pauseus LowCycle
               Portb = %00000001
               Pauseus HighCycle
           Next Cycles
       ENDIF
    Of course there are many other ways to do this, but this gives you a good idea of at least 1 way.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. trouble with microcode loader
    By meho in forum mel PIC BASIC
    Replies: 4
    Last Post: - 9th August 2012, 14:39
  2. LCD issue - what does FLAGS = 0 really do?
    By BrianT in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 27th February 2008, 05:26
  3. Trouble using int. osc. On 18f2455
    By KPDes in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th June 2007, 10:35
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 11:07
  5. 16f876 PortA set-up trouble?
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th February 2006, 19:31

Members who have read this thread : 1

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