PDA

View Full Version : Fade out an LED using PWM?



RossW
- 4th August 2004, 16:11
I'm trying to code a circuit that will gradually 'fade' out an LED. This is an illusion, mind you, as naturally and LED is either high or low. Someone mentioned that they can simulate this using a 5 channel PWM circuit, but I haven't a clue what they're talking about.

Does anyone have any ideas, or even better, some sample code?

Cheers,
Ross

Dwayne
- 4th August 2004, 18:01
Hello RossW,

Ross>>I'm trying to code a circuit that will gradually 'fade' out an LED. This is an illusion, mind you, as naturally and LED is either high or low. Someone mentioned that they can simulate this using a 5 channel PWM circuit, but I haven't a clue what they're talking about.

Does anyone have any ideas, or even better, some sample code?<<

Well a PWM just varies the width of a pulse between 0 and 256 (256 being steady output). And the PWM can work while other things are going on in your chip...

You can simulate a PWM, or you can use a PWM... A easy way to make a dimmer, is to vary the number of times the 5 volts is applied to the light.

LED var byte.
Counter var byte
Counter2 var byte
Counter3 var bye


For Counter= 256 1 step -1

For Counter2=1 to 256
For Counter3=1 to 256
LED high 'turn on LED
Pause Counter 'wait a while
LED Low 'turn off LED
Next counter3
Next counter2

Next Counter


Though I have not check this out, I think it will work for you.

It is based upon the principal that your eye is not as fast as we wish. You can change the variables to words and save some code.. and get rid of a for-next statement. I am sure there are other ways to accomplish this... but my mind is tired, and this was the easiest way I thought of off the top of my head.

Dwayne

CBUK
- 4th August 2004, 19:12
when im in the office ill check with the book, but theres an actual pwm command that does it all for you, im looking to do the same but with RGB leds (hence my posting on the 3 hardware pwm pics) to fade through the rainbow... i shall try an example shortly.

Chris

RossW
- 4th August 2004, 19:33
Dwayne - I'll try that out tonight.
Chris - I'd love to see your sample code!

Ross

RossW
- 5th August 2004, 02:57
Dwayne,

The LEDs do not appear to be 'fading out' - in fact, they appear to be steady on. Here's the code exactly as it's compiled:

LEDs VAR BYTE
Counter VAR BYTE
Counter2 VAR BYTE
Counter3 VAR BYTE

LEDs = 3 'Light RB0 & RB1 pins
TRISB = 0 'Initialize all pins on PORTB to output

Loop:

For Counter = 255 TO 0 STEP -1
For Counter2 = 0 TO 255
For Counter3 = 0 TO 255
PORTB = LEDs
Pause Counter 'wait a while
PORTB = 0
Next Counter3
Next Counter2
Next Counter

Pause 3000
GoTo Loop

I think part of the problem is that there is no pause after going low, so they never appear to be off. I don't understand why it doesn't finish the 3 loops and exit with the LEDs off, wait 3 seconds, and then start again?

NavMicroSystems
- 5th August 2004, 10:58
Ross,

there is an easy way to achieve that "fading effect"
even on a PIC with no Hardware PWM, use Software PWM:

Duty VAR BYTE
Cycle VAR BYTE
LED VAR PortB.1

' changing the value for "Cycle" will change the time it takes
' to fade from 100% to 0%

Cycle=10

Loop:
For Duty=255 to 0 Step -1
PWM LED,Duty,Cycle
Next
Pause 3000
GOTO Loop


rgds

CBUK
- 5th August 2004, 13:23
yes using the PWM command is by far the better way to get pulse width modulation without the worry for the individual for next loops...

Dwayne
- 5th August 2004, 14:43
Hello RossW,

RossW>>The LEDs do not appear to be 'fading out' - in fact, they appear to be steady on. Here's the code exactly as it's compiled:<<

I am in the process of moving, so I have no test equipment to set up... but I htink tonight I will have some stuff up and running. I am answering you from my work place <g>.

What that does, is simulate a PWM... I could have a line or two out of place. What I will do, I try it at home and send you the code...

I may have a line or two out of position. Like I said before, I had not checked the program out, because I typed it in at work. But I will try to get it going for you. It works great with chips that don't have PWM, or need that PWM for something else...

Dwayne

NavMicroSystems
- 5th August 2004, 15:52
Originally posted by Dwayne
....But I will try to get it going for you. It works great with chips that don't have PWM, or need that PWM for something else...


Dwayne,

have I missed somethig?

why re-invent the wheel?

If there is no PWM-Hardware or the PWM Port is already in use
you can use PBP's PWM command.
(see my example)

However, HPWM would be the best choice as it runs in background.

I mean if you would like to display something on an LCD
you would use the PBP LCDout command wouldn't you?

Or would you set the LCD-Bits individually and clock the LCD manually?

best regards

Ralph

Dwayne
- 5th August 2004, 17:00
Hello Ralph,


Ralph>>have I missed somethig?

why re-invent the wheel?<<

ROFL!! No my friend... *I* missed something...I was so focused on the PWM on the chip. I was thinking HPWM the whole time...

Anyhow... I like to reinvent the wheel <g> Makes my life more complicated.

I was sitting at my desk here, and figured out why that example did not work...I needed a "0" pause that was exactly opposite of the "1" .

Loop
High LED
Pause Length
Low LED
Pause 256-Length
goto Loop.


Ralph >>If there is no PWM-Hardware or the PWM Port is already in use you can use PBP's PWM command.
(see my example)<<

Superb example... Just caught me a little brain dead...<g>

Ralph >>I mean if you would like to display something on an LCD
you would use the PBP LCDout command wouldn't you?<<

You really don't want the answer to this do you???<g>
No, I do not use the LCDout command to display and control my LCD.

Ralph>>Or would you set the LCD-Bits individually and clock the LCD manually?<<

You asked.... Yes.. I do.


Your friend,

Fred Flinston <g>


Thanks Ralph.

Dwayne

RossW
- 5th August 2004, 17:13
How do I find out if my chip has hardware pwm? I'm thinking of using a 12F629 or a 16F628 ...

CBUK
- 5th August 2004, 17:19
find the data sheet, and it'll tell you what special functions the PIC's operate. with hardware PWM you assign the HPWM command to one of the hardware pins (again specified by the manufacture's data sheet)
the 16F877 of which is one of my common use pics has 2 hardware ports.

Chris

Dwayne
- 5th August 2004, 17:27
Hello Rossw

Rossw>>How do I find out if my chip has hardware pwm? I'm thinking of using a 12F629 or a 16F628 ...<<

The Data sheet will tell you...

The 16F628 has one on Port B3... Same as the 16F648A

Dwayne

PS. With the Built in PWM, your chip and be doing other functions as the chip is outputting the train.

NavMicroSystems
- 5th August 2004, 17:28
Dwayne,

we are a bit off topic but

you should immediately delete your installation of PBP from your harddisk and shred the manual.
(REAL men don't read instructions!)

Go assembler or even better delete that MPASM as well and use a HEX editor to create your Hex-Files directly.

This way you don't have to wait for the compiler and assembler to finish, your hex file is immediatly ready to be burned onto the PIC !

This reminds me of my very first PROM-programmer
( No Joke!)
it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.

Handling was pretty simple:
Use the DIP-Switches to set the Bits and use the Button generate a "PROGRAM" Pulse

This Type of Programmer can certainly be modified for use with a PIC.

So you do not even have to struggle with your Programmer Software, just delete it too, it is no longer needed!

Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!

And you gain a lot of free disk space.

But do you really need PICs and a PC?

a bunch of relays would do the job as well and if you like it "hitec" I still have some tubes in my cellar I could offer.

*LOL* *ROFL*

Ralph

Dwayne
- 5th August 2004, 17:58
Hello Ralph,

Ralph>>we are a bit off topic but<<

Hey! its about Micro-Chips right??

Ralph>>you should immediately delete your installation of PBP from your harddisk and shred the manual.<<

Hu humm.. That manual should be big enough to roast a hot dog. Best tasting 200 dollar hot dog I have ever had...


Ralph >>This reminds me of my very first PROM-programmer
( No Joke!) it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.<<

Yes!! Yes!! Did you include the UV light??? that takes 20 lousy min to erase your chip to zelch?

Ralph>>Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!<<

My hearts a beating...Where is it??? <chuckle>

Ralph>>And you gain a lot of free disk space.<<

My 8088 4.77mhz IBM hercules graphic card and 15 meg Hard drive is jumping for joy.... CoCo RS computer coughed, My CPM burped. My VIC20. rolled over. Now I am left with my LIttle TI99A

Dwayne

Dwayne
- 5th August 2004, 18:54
Hello Ralph,

Ralph>>we are a bit off topic but<<

Hey! its about Micro-Chips right??

Ralph>>you should immediately delete your installation of PBP from your harddisk and shred the manual.<<

Hu humm.. That manual should be big enough to roast a hot dog. Best tasting 200 dollar hot dog I have ever had...


Ralph >>This reminds me of my very first PROM-programmer
( No Joke!) it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.<<

Yes!! Yes!! Did you include the UV light??? that takes 20 lousy min to erase your chip to zelch?

Ralph>>Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!<<

My hearts a beating...Where is it??? <chuckle>

Ralph>>And you gain a lot of free disk space.<<

My 8088 4.77mhz IBM hercules graphic card and 15 meg Hard drive is jumping for joy.... CoCo RS computer coughed, My CPM burped. My VIC20. rolled over. Now I am left with my LIttle TI99A

Dwayne

RossW
- 5th August 2004, 19:54
So if I want to fade in, would I use:

For duty = 0 to 255
.
.
.


??

CBUK
- 5th August 2004, 20:29
indeed!

or use the seed command and a 255 limit loop to give a random brightness ;)

RossW
- 5th August 2004, 21:54
Seed command? What's that? I could really use something that gives random 'brightness' for another aspect of this project.

NavMicroSystems
- 5th August 2004, 22:24
What Chris wants to tell you is:

use the RANDOM command

See Manual Section 5.60

example:

RND VAR BYTE
LED VAR PortB.1


Loop:
RANDOM RND
PWM LED,RND,1000
GOTO Loop

rgds

CBUK
- 5th August 2004, 22:45
i think you will find there is also a seed command that is used in PBP ive used it and cannot determine the difference between that and the random command... apart from about 2 letters hehe

NavMicroSystems
- 5th August 2004, 22:57
@ CBUK

would you please give RossW (and all others) a reference to the Manual and a code example.

And please check your PM

CBUK
- 5th August 2004, 23:05
with reference to the manual its just under seed, in alphabetical order as is the whole manual, or do i have a different version. also if you check the 'help' from PBP it gives the same documentation with an example. i shall sort page numbers out in the office friday.
and my PM enlighten me to the way this forum works?

ta

chris

NavMicroSystems
- 5th August 2004, 23:20
@ CBUK

???

Dwayne
- 6th August 2004, 01:39
Hello Rossw

Here is the Code that I promised you... But, like Ralph said... PBP has a built in function, and using my code is like re-inventing the wheel... <g> But I promised it to you anyhow.... It is cycling just like the PWM...different timing, but principal is the same... I tested it on a LED, and the LED cycled... you may want to change the parameters to allow the LED to go all the way out...

@ 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 byte
Counter2 var word
Loop:
For Counter=0 to 255
For Counter2=0 to 62
GPIO.1=1
Pauseus Counter
GPIO.1=0
pauseus 255-Counter
Next Counter2
Next counter
GPIO.0=1
GPIO.0=0
Goto Loop
end


If you want to use the PWM on your 628... here is the code for it...It has some extra trash in it... but who cares <g>


C1 var byte
TrisA = %11111111
TrisB = %00000000
PortB = %0
PR2= $003F
CCPR1L = 0
CCP1CON = %00001100
TRISB.3 = 0
T2CON = %00000110
'I BYTE
CCPR1L=0
PAUSE 5000

LOOPIT:
For c1=0 to 60
CCPR1L=C1
PAUSE 1000
Next C1
GOTO LOOPIT
END

RossW
- 6th August 2004, 02:57
NavMicroSystems - it worked! With cycle = 4 I get the exact fade out appearance I was looking for. I tried the 'fade in' with For duty = 0 to 255 and that works beautifully, too.

I'm so glad I bit the bullet and got PBP - this makes programming chips so much easier than assembly.

RossW
- 6th August 2004, 19:27
Dwayne - in the second bit of code you provided, is that using a built-in hardware pwm on the 628? I'd like to try using a hardware pwm, too, to compare the differences.

Ross

NavMicroSystems
- 6th August 2004, 19:59
Ross,

to use HPWM on the 16F628 your LED must be connected to RB3
(This is the only Hardware PWM Port on the 628)

Duty VAR BYTE
Freq VAR Word
LED VAR PortB.3

' changing the value for "Duty" will change the brightness
Duty=127 ' about 50% Brightness

Freq=1000 '1 kHz


HPWM LED,Duty,Freq


The difference is once you have initialized in runs forever in Background while your PIC is doing other Things.

See PBP Manual Section 5.29 for reference.

regards

Ralph