PDA

View Full Version : Problem with intensity.. plz help



Srigopal007
- 5th October 2004, 21:29
Hi, I am trying to control the intensity of an LED through pbp. Here is what I wanted to achieve, to set the intensity of the light to be 50% constant for 10 sec and then 25% constant for another 10 sec. Here is what I was trying to do, but not sure if this is correct pbp style of programming.... any help will be greatly appreciated. thank you. Not sure if the below code will work for my description.
===============================================sta rt of program==================================

include "modedefs.bas"
LED1 VAR PORTB.4
LED2 VAR PORTB.5
LED3 VAR PORTB.6
TRISB = %00000000

start:

Loop:

For i = 0 to 10
LED1 = 1111 'full intensity

Pause 1000

LED2 = 0111 'Half intensity

Pause 1000

LED3 = 0011 'quarter intensity

PAUSE 1000

Next i
Goto Loop

Dwayne
- 5th October 2004, 21:56
Hello Srig,

Srig>>Hi, I am trying to control the intensity of an LED through pbp. Here is what I wanted to achieve, to set the intensity of the light to be 50% constant for 10 sec and then 25% constant for another 10 sec. Here is what I was trying to do, but not sure if this is correct pbp style of programming.... any help will be greatly appreciated. thank you. Not sure if the below code will work for my description. <<

It depends on how you want to do it....

If you want to keep the intensity goin while the chip is doing different things, you must use the HPWM. the hardware PWM will put out a steady stream as the chip does different things. If you use more than one LED, you will need two pins that have PWM on them.

If you only care about one pin at a time, and you do not care about the LED being shut off as the other LED is working, you can use *any* Chip and use the software PWM command.


Or you can make your own PWM routine.. Which is quite easy.

Dwayne

NavMicroSystems
- 6th October 2004, 01:22
See:

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=622&highlight=fade

regards

Dwayne
- 6th October 2004, 18:17
Hello Srig,

Srig>>I am performing software pwm . the following works on multiple pins at
the same time doing software pwm.

For Duty = 0 to 999

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

but what I was trying to do is make three separate pins turn on at the
same time but all three of them need to be on a different intensities.
for instance one has to be on at LED1 = (20%)
LED2 = (30%)
LED3 = (80%)

here is what I have so far.. not sure if it is working but just need
someone to verify that this will work. I was hoping that you would do
this for me.

<<

To make 3 separate pins to turn on at the same time with different intensities, you must use a PIC chip that has three PWM's built into the chip. That I know of, there is only one PIC chip out there that has 3 PWM's. (But could be more)

The software PWM will not work. It is only good for one pin at a time. You will need the chip that has 3 hardware PWM's.

Then your code will be something like the following:

x=256*.2
y=256*.3
z=256*.8


HPWM 1,x,2000
HPWM 2,y,2000
HPWM 3,z,2000


Dwayne

Melanie
- 7th October 2004, 00:26
Not strictly true Dwayne.

I shall play Devils Advocate... as I seem to have a habit of doing here...

A 4MHz PIC can easily handle the dimming of half a Dozen LED's, maybe more... a 20MHz PIC, pro rata can do a couple of Dozen independantly.

How?

It's all down to your eye's perception... your eye probably cannot distinguish anything over 25Hz and it blurrs into a constant. Providing you can scan your LED's at least at that rate, your eye will have the 'perception' of dimming.

There is a problem with LED's though... 80% lit might as well be 100% lit, because there's not much differentiating the two as far as your eyesight is concenerned. The biggest visible changes occur between zero and about 65%. The eye sees little change between 65% and 100% as the program demonstrates...


'
' Define LED Ports
' ----------------
LEDA var PortB.0
LEDB var PortB.1
LEDC var PortB.2
'
' Define Software Variables
' -------------------------
StatusA var Byte
DirectionA var Bit
StatusB var Byte
DirectionB var Bit
StatusC var Byte
DirectionC var Bit
CounterA var Byte
'
' Initialise PIC
' --------------
TRISB=%00000000
'
' Start Program
' -------------
StatusA=66
DirectionA=1
StatusB=66
DirectionB=0
StatusC=0
DirectionC=0
'
' LED Ramp-Up/Ramp-Down Loop
' --------------------------
Loop:
Gosub DimLEDS
If DirectionA=0 then
StatusA=StatusA+1
If StatusA=100 then DirectionA=1
else
StatusA=StatusA-1
If StatusA=0 then DirectionA=0
endif
If DirectionB=0 then
StatusB=StatusB+1
If StatusB=100 then DirectionB=1
else
StatusB=StatusB-1
If StatusB=0 then DirectionB=0
endif
If DirectionC=0 then
StatusC=StatusC+1
If StatusC=100 then DirectionC=1
else
StatusC=StatusC-1
If StatusC=0 then DirectionC=0
endif
Goto Loop

'
' Subroutine Displays LEDS as % of Brightness
' -------------------------------------------
DimLEDS:
For CounterA=0 to 100
If StatusA>0 then
High LEDA
If CounterA=>StatusA then
Low LEDA
endif
endif
If StatusB>0 then
High LEDB
If CounterA=>StatusB then
Low LEDB
endif
endif
If StatusC>0 then
High LEDC
If CounterA=>StatusC then
Low LEDC
endif
endif
PauseUS 20
Next CounterA
Return

End

Here the Hub of the program is the above subroutine DimLEDs. This subroutine takes 2mS to execute once (100 times 20uS). This 2mS period is our PWM period. The 2mS time period is subdivided into 100 slots of 20uS each. This determines the ON time of the LED's. The amount of 20uS slots (from zero to 100) that an LED is lit is dependant on the LED's StatusX variable. This variable when zero represents 0% PWM, and when the variable is 100 represents 100% PWM with all the steps intermediate. So when StatusC=50 for example, this means that LEDC will be on for fifty out of the one hundred 20uS slots, namely 50% of the 2mS time period or 1mS (calculating 50*20uS=1mS).

As you can see, this crude subroutine accounts for one period cycle of 2mS. Call this routine repeatedly (as we do in this example), and we have a vague 500Hz refresh rate (excluding any external calculation time - reality is a lot slower as I've not taken the program execution time into effect - but you'll see what I'm driving at without clouding the main issue). The secret is in keeping the external time outside the subroutine to a minimum as prolonged delays there can easily cause unacceptible flicker.

How do you think those cheapo Christmas lights get the dimming effect? They sure don't use microcontrollers with 3 or 4 hardware PWM channels.

If you build the circuit and run this code, you will get three LED's all sequencing up and down (equivallent of 120 degrees out of phase with each other) - and very smoothly too... and with this method you can control a lot more than three LED's at a time... just don't try to do anything too time-consuming outside that subroutine though!

Going back to Srigopal's requirement of 20%, 30% and 80%, you could execute it as...



SrigopalsLoop:
StatusA=20 ' 20%
StatusB=30 ' 30%
StatusC=80 ' 80%
Gosub DimLEDS
goto SrigopalsLoop

If you play with this then you will then discover that to your eye does not distingush brightness linearly. The program will faithfully ramp up and down those LED's from zero to 100% and back down again... but that's not how your eye sees it. But as they say... that's another story...

Melanie

Dwayne
- 7th October 2004, 16:27
Hello Melanie,


Melanie>>Not strictly true Dwayne.

I shall play Devils Advocate... as I seem to have a habit of doing here...<<

Melanie, IMO you just keep right on playing devils advocate. And this is exactly why I moved this message from my mailbox to the board. For more people to see and comment on so others may learn and for others to expand on things I tend to forget, leave out, or whatever.

You are correct... I gave up a long time ago on dimming lights wth software PWM's....Just because I tend to do many things inside the chip while the thing is attempting to run a light, motor, or other thing. Hardware PWM's are just the way to go for most applications I can use.

Dwayne (thanks for your support Melanie)

Srigopal007
- 7th October 2004, 17:17
WOW. Thanks you two, (Dwayne and Melanie). You two are wonderful and brilliant at the same time. This code works for the better part of the program, but I just need to not do any time-consuming things outside this subroutine so that I can avoid the flickering. Other than that this code is awesome. Thanks again Melanie and Dwayne for your contribution. :)..

*my next task is to control a stepper motor ... that will be funn and interesting project.... any suggestions on what stepper motor driver that are available out there.

srigopal

Melanie
- 7th October 2004, 17:25
Subject already been done ages ago...

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=101