PDA

View Full Version : trouble with FLAGs



Srigopal007
- 9th November 2004, 19:03
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

Bruce
- 9th November 2004, 21:03
Try something like this.


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.

Srigopal007
- 9th November 2004, 22:35
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

Bruce
- 9th November 2004, 22:54
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?

Srigopal007
- 9th November 2004, 23:27
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

Bruce
- 10th November 2004, 02:26
Hi srigopal,

There are quite a few ways to do this. Here's one. This version just stops your PWM change.


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.

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.