PDA

View Full Version : Unusual problem



Srigopal007
- 15th November 2004, 21:32
Hello Folks, I have been working on this for quite a while now and am not getting the results that I wanted to acheive. I am using a a few switches to control the sequence of LEDs through PWM. I have pasted my code here ... Hope someone can help me find the solution to this.

==============Code starts here===================
start:

'Values of Variables being used
Cycle1 = 2
Save = 0


'start of sequence
For Duty = 0 to 2000 'All OFF ---> BLUE
If(Stop_Switch = 0) Then Flag1 = 1 'Set FLAG to Zero because Ext. Int. Occurred

if(Flag1 = 1) then
Duty = Duty - 1 'PAUSE PWM actually occurs here

'jumping to subroutines

if(INC_BRIGHT = 0) then GoSub INCREASE_BRIGHT 'bright switch

endif

if(Resume_Switch = 0) && (Stop_Switch = 1) then 'if resume switch is pressed then return back and continue
Flag1 = 0 'Since Pausing is no longer performing, Flag is refreshed back to 1
Endif 'to resume PWM where it was left off


For cycles = 0 to Cycle1 'if Nothing is pressed then Perform normal PWM sequence
Portb = %00000000
Pauseus 2000 - Duty
Portb = %11100000
Pauseus Duty

Next cycles
Next Duty

'start of subroutine

INCREASE_BRIGHT:
While(INC_BRIGHT = 0)
if(Save = 0) then 'making sure whether INCreasing or DECreasing
'Global Brightness switch is pressed first
A = 0 'if Increasing switch is pressed first then start
'with initial values
else 'OtherWise
A = Save 'if the Decrease global Switch is pressed first, then
'start with the values stored in the variable SAVE
Endif 'ending the condition loop
For i = A to 100 'if increasing switch is pressed continuously then
PortB.4 = 0 'Normal PWM
Pauseus 100 - i
PortB.4 = 1
Pauseus i
Save = i 'saving the value of the PWM state just incase the DEC-GlOB is pressed

if(INC_BRIGHT = 1) && (DEC_GLOB_BRIGHT_SWITCH = 1) then 'if button is let go then return
Return
endif

Next i 'if switch is kept pressed then proceed to next increment
Wend

==============================================

I am performing a simple software pwm with three leds connected to PORTB.7, PORTB.6, and PORTB.5 ... Here is what I want to do with the code I have attached above. When the increase brightness switch is pressed (at random) I want the brightness to change without noticing the flickering. I notice that once the code jumps into the subroutine the LEDs become full brightness, and does not stay at a random (PWM VALUE) this is because the last thing that is executed in the main loop is
PORTB = %11100000 this keeps the three LEDs full brightness.

For example lets say that the button is pressed when all of the three LEDS are 40% ON. and for the amount of time the button is pressed, the leds increases in brightness. How do I prevent the behavior I see from my code. Any help on this would be greatly appreciated. many Thanks in advance

srigopal

mister_e
- 16th November 2004, 05:48
Hi srig, sorry if i didn't work on this since a lot of time but i just have one suggestion to avoid flickering.


1. have a PIC with 1 internal PWM
2. Use this output with a transistor or mosfet to source voltage to LEDS


so that will be more easy to create your code. The only thing you will have to work with is with HPWM.

better like that ?

Srigopal007
- 16th November 2004, 17:16
For Hardware PWM .. lets say you want the pwm to go from 0 to 100% so lets say that i have the folllowing in my code


hpwm 1, 256, 1000

Will this command ramp the LED from 0 brightness slowly to 100% brightness

or


will this command just make the LED full Bright, all the time


srigopal

mister_e
- 16th November 2004, 17:27
For a ramp from 0-100%



For z=0 to 255
HPWM 1,z,1000
;
;
next


where 0 is full time OFF and 255 is full time ON

Srigopal007
- 16th November 2004, 17:46
Now will this use up any of the CPU execution cycles or will it run in the background..?

mister_e
- 16th November 2004, 17:48
Since it's an Hardware function of your PIC, it will run in background without any effect caused by others.

Srigopal007
- 16th November 2004, 17:58
but what about the
For z = 0 to 255
HPWM 1, Z, 1000
Next Z


The For loop will take some cycles away from the CPU wouldn;t it?.. the only reason why I am asking this is because whenever I run a FOr loop I have come across some flickering. but never tried the hardware modules before. maybe it works different. Can you explain more why this does not work

and are there any other DEFINES I need to incorporate into my code to use the HPWM command or that is all I need to do to run the HARDWARE PWM module

mister_e
- 16th November 2004, 18:35
The For loop will take some cycles away from the CPU wouldn;t it?.. the only reason why I am asking this is because whenever I run a FOr loop I have come across some flickering. but never tried the hardware modules before. maybe it works different. Can you explain more why this does not work

The major differerence is because it's an hardware function. In your previous code, you must wait untill key is press/depress this is what cause the flickering. Your pseudo PWM wait for executing the next function. If it's waiting, LED will be Full on or Full of depending where you get the pushbutton action in your FOR TO NEXT loop.


In case of using HPWM, it can have a little flickering when changing duty value... must have great eyes to see it since it's done really fast by PIC.

for the DEFINE, it's usually use to redirect PWM to different PINS of PIC when this one have the ability of.