PDA

View Full Version : PWM conflict



Srigopal007
- 31st August 2004, 18:02
I have read the PWM listing that was posted earlier this month and have considered that in my code. I am trying to imitate the fading and slowly turning on effect using pbp using SOFTWARE PWM because the PIC that I am using does not have a HARDWARE PWM. I am running my PIC at 20Mhz. please help.. any advise is greatly appreciated... here is what I am doing....

==== program starts here====
include "modedefs.bas"
Duty VAR WORD
Cycle0 VAR BYTE
Cycle1 VAR BYTE
LED1 VAR PORTB.4
LED2 VAR PORTB.5
TRISB = %00000000

'How long it will take for the PWM to occur
Cycle0 = 10
Cycle1 = 10

'Turns Off the LEDS SLOWLY
For Duty = 255 to 0 Step -1 'going from ON to OFF state.
PWM LED1, Duty, Cycle4 'sends a string of pulses
PWM LED2, Duty, Cycle5 'sends a string of pulses
Next Duty
'Turns On the LEDS SLOWLY
For Duty = 0 to 255 'Going from OFF to ON STATE
PWM LED1, Duty, Cycle0 'sends a string of pulses
PWM LED2, Duty, Cycle1 'sends a string of pulses
Next Duty
GoTo start

==========program ends here=====

I am seeing the flickering of the LEDs instead of a smooth fading effect that I am suppose to see. I have read the manual and it says that the duty needs to be from 0 (0%) to 255(100%) I am not seeing the gradual fluctuation in the intensity of the light, but instead I see the LEDs flicker on and off very fast, with the same intensity when they are on. Any help with what I am doing wrong is greatly appreciated. Thanks in Advance

Srigopal

Srigopal007
- 31st August 2004, 18:23
ohh I forgot to include the
Cycle4 VAR Byte
Cycle5 VAR Byte
.
.
.

cycle4 = 10
cycle5 = 10

in the code that I posted ...

Dwayne
- 31st August 2004, 19:54
Hello Srig,

Srig>>I have read the PWM listing that was posted earlier this month and have considered that in my code. I am trying to imitate the fading and slowly turning on effect using pbp using SOFTWARE PWM because the PIC that I am using does not have a HARDWARE PWM. I am running my PIC at 20Mhz. please help.. any advise is greatly appreciated... here is what I am doing....<<

Ok, I will up load my Homemade PWM to you as soon as I get home tonight... It is already tested and working. Can be used on any output pin you want.

One other thing... A Software PWM is different than a Hardware PWM. A Hardware PWM is continous...your chip can be doing things while your PWM is outputting. On a Software PWM, this is not so... You have to complete your PWM before moving on to the next phase of your program.

I notice you are switching between two diodes... in the *middle* of your homemade PWM.... that could be a problem, and could explain your "Flickering". I did not sort through your code, I just noticed this.

Dwayne

Srigopal007
- 31st August 2004, 20:16
Hi Dwayne, Thanks for your reply. Here is the thing. when I only try to control one of the LED with PWM to turns it on and off smoothly and the program works fine.. but when I try to control two of them at the same time.. I am noticing flickering and not getting what I wanted. I dont know why this is the case. I konw I have the code fine with compliance with PBP. Can anyone help me with some answers.. thanksss in advance


srigopal

Dwayne
- 31st August 2004, 20:47
Hello Srig,

Srig>>Hi Dwayne, Thanks for your reply. Here is the thing. when I only try to control one of the LED with PWM to turns it on and off smoothly and the program works fine.. but when I try to control two of them at the same time.. I am noticing flickering and not getting what I wanted. I dont know why this is the case. I konw I have the code fine with compliance with PBP. Can anyone help me with some answers.. thanksss in <<

Ok, this explains everything.... Remember when I said in a Software PWM, you must wait until the entire command is done? What you are doing, is switching between two LED's and because of the time it takes to switch, you are getting this "Flickering" effect. In other words the Software PWM can Probably only handle 1 LCD at a time on the "Fade" effect.

If you had a hardware PWM, you would have no probems at all. Hardware PWMS operate continuosly, while your chip does other things. Thus, while your chip is switching loops, Jumping around in the program, your PWM is still outputting a continuous stream of Pulses. (Software PWM can't do this).

You *may* be able to adjust your "Cycle" to make the "Flickering" less noticeable", but between your Duty Cycle and your Cycling, it may be difficult to remove this "Flicker" to the eye using two LCD's... and the problem compounds itself, the more Pins you use to simulate a Software PWM...

Dwayne

Dwayne

Dwayne
- 1st September 2004, 04:19
Hello Srig,

here is the code:

@ DEVICE PIC12F675,INTRC_OSC_NOCLKOUT,WDT_ON,PWRT_OFF,BOD_O N,PROTECT_OFF,CPD_OFF,MCLR_OFF
ANSEL=%00000000
CMCON=%00010111
TRISIO=%00011000
ADCON0=%00000000
Counter var word
Counter2 var byte
Loop:
For Counter=0 to 250
For Counter2=0 to 150
GPIO.1=1
Pauseus Counter
GPIO.1=0
pauseus 250-Counter
Next Counter2
Next counter
GPIO.0=1
pause 1000
GPIO.0=0
Goto Loop
end

RossW
- 1st September 2004, 14:55
srigopal - I tried to do something similar to your code and ran into the same problem with flickering. If all you want to do is fade out (or in) 2 LEDs at the same rate, try using 1 output pin with 2 LEDs (each with a current limiting diode, of course). I have this working with red LEDs, but you may need a transistor for heavier current drawing ones like blue or white.

Dwayne - is your code an example of a hardware pwm? Isn't there a PicBasic command for this? Also, do you know how to use a hardware pwm signal to fade out LEDs?

Ross

Dwayne
- 1st September 2004, 15:18
Hello Ross,

Ross>>Dwayne - is your code an example of a hardware pwm? Isn't there a PicBasic command for this? Also, do you know how to use a hardware pwm signal to fade out LEDs?<<

No, my example is not a Hardware PWM. He emailed me with a private message, that he would like to have my code of my software PWM. I told him I would put it on the board when I got home.

Yes, there is a PBP command for this kind of PWM...I just write my own <g>

Yes, I know how to use hardware PWM to fade in/out LED's Since the Hardware PWM's are contiuous without intervention, the chip can be doing other things while it is streaming the pulses. That means a chip with 2 PWM's can operate two fading LED's

Dwayne