Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] LED ON/OFF based on PWM using PIC

Status
Not open for further replies.

prmurthy

Full Member level 5
Joined
Feb 13, 2009
Messages
243
Helped
38
Reputation
76
Reaction score
34
Trophy points
1,308
Activity points
2,770
Hello,

I need help or source code in C for LED on/Off which based in PWM. I was able to make fade in fade out but struggling to make the LED on/off, may be I have not understood the pwm properly. What I need is the rate of on and off should be based on pwm duty cycle. Please help me I need to complete my school project.

Thanks

---------- Post added at 07:40 ---------- Previous post was at 07:30 ----------

Please I can use Mikro C for this project as it has built PWM module. But I need to understand the PWM function in a better way, hence I request if somebody can help me in building up the code in Hi-Tech C, Thanks
 

PWM is not normally used to control the rate at which an output is turned on and off, if that's what you really want to do it would be better to use a timer or delay and simply set a port bit high or low periodically.
The normal use of PWM is to keep the rate constant but slice the period into an on portion and off portion. You change the relative sizes of these to control the average level of output level.
Brian.
 
Hi,
What do you mean by on/off? For simple on/off, why would you use PWM? You could achieve this simply by writing 1 or 0 to the pin. If you mean, you want the LED to blink at a fixed rate, then, have the duty cycle set at 50% and the period at something that can be detected by the human eye. For this purpose, the frequency has to be in the order of a few Hz at max. So, using the PWM module would be unwise, as this would require you to drastically reduce the operating frequency. You better use delay or timer interrupt for this purpose.

Hope this helps.
Tahmid.
 
Hi Brian and Tahmid.
Thanks for your replies.
The PWM is the period of on's and off's if I understood correctly. So if I say 50% duty cycle of 1s that means say 0.5s on and 0.5s off. I know that this can be done with the help of timer but what exactly I need is the LED should be on and off as per this PWM duty cycle and if I change the duty cycle to say 25% the LED on and off should be at 25% and if I change the duty cycle to 75% the LED on off should be at 75% rate. I also would like to understand wheather it is possible to select the ON cycle and OFF cycle at differnet rates using PWM ( I think the answer should be no). I mean is let's say ON @25% and off @ 75%. In short My target is to explain my collegues the PWM function in terms of ON and OFF as shown in theroy.

#include <pic.h>

__CONFIG(LVPDIS & INTIO & PWRTEN & WDTDIS & MCLRDIS );

static int fade;

void interrupt isr(void){
if(T0IF)
{
T0IF=0;


if(fade)
{
CCPR1L++;
if(CCPR1L == PR2)
fade=0;
}
else
{
CCPR1L--;
if(CCPR1L == PR2)
fade=1;
}

}
}

void main(void)
{
fade=0;

IRCF2 = 1;
IRCF1 = 1;
IRCF0 = 1;
OPTION = 0b00000011;
TRISC = 0;
TRISB = 0;
TRISA = 0;

PR2=255;
CCPR1L=100;
CCP1CON=0x04|0x08;
T2CON=0x00;
T2CKPS0=1; // Set timer 2 prescaler to 1:16.
T2CKPS1=0; // These bits are in T2CON.
TMR2ON=1; // Enable timer 2.

// Set up timer0 interrupt
T0CS=0;
PSA=0;
PS2=0; PS1=0; PS0=1;
INTCON=0;
GIE=1;
T0IE=1;
TMR0=0;

while(1); // Loop forever
{


}


}

I am able to fade in and fade out with the above code but I am unable to put the same to make the LED on and OFF.

Thanks
 

Hi,
It is completely possible. But not with the CCP module's PWM part. You can use the compare module or you have to use timers for PWM here, because 1s means 1Hz which is not achievable with the CCP module's PWM part at normal MHz range clock frequency.

Hope this helps.
Tahmid.
 
Hi,

I have used the compare module I have used for accelarometer where I had to show the tilt position, though I am not expert. Regards the freq, what do you think the freq range for various pwm duty cycle should be where we can see the LED on off using PWM where it is visible to human eye. Can you help
me on variable PWM either basic idea or some example code.
Thanks
 

A few Hz maximum. Just choose 1Hz if it's for demonstration or maybe 0.5Hz.

Hope this helps.
Tahmid.
 
Thanks

Let me try different options and experiments.

Thanks for help
 

@prmurthy
Can you explain exactly the output state of the led you are looking for. Do you want to make it blink or do you want to maintain an average voltage level across the led. If its the second one, then varying the duty cycle pretty much varies the average voltage level at the pwm output pin for a wide range of frequency. I could not understand the use of pwm for your purpose.
 

Hi Natraj

The objecive is very simple. As the above code listed is for fade in fade out for and LED at certain PWM duty cycle. Is it possible to make the same way on and off. The on and off frequency ( not in Hz, in terms of times) depending on the PWM duty cycle. Like 100% duty cycle the on and off should be for 1s or so and 25% should be for 0.25s etc etc.
Thanks
 

Could you split up the last sentence for 100% and 25% duty cycle in terms of Ton and Toff for a particular time period say 1s.
 

prmurthy, what you are asking for in your last message is for the frequency to change according to the duty cycle. The two things are not related, you can have any duty cycle at any frequency. I think what you are trying to do is control the brightness of the LED. If you just change the frequency it doesn't matter if it's on for 1 second and off for one second or if it's on for 0.25 seconds then off for 0.25 seconds, all you get is the same brightness but flashing at a different speed.

What I think you should be doing is using a fixed frequency, at least 100HZ and preferably nearer 1KHz. This is too fast for the human eye to perceive as flashing. Now you change the relative time within each cycle in which the LED is on and it is off. For example using a fixed 10mS period (100Hz) you would get 25% brightness if it was on for 2.5mS and off for 7.5mS and if you wanted 50% brightness you would turn it on for 5mS and off for 5mS. Note that the cycle is (on time + off time) and it stays the same as you change the relative proportion of on/off times. Is this what you are trying to do?

Brian.
 

@Brian,

tats the reason i asked him to explain the exact application and his understanding of the Ton + Toff
 
  • Like
Reactions: betwixt

    betwixt

    Points: 2
    Helpful Answer Positive Rating
Hello Brian, Natraj, Sillo

Actually I didn't wanted to revive this post, becuase I could not get proper answer. May be I have not put correctly so that you can understand. May be I not understood properly about PWM and it's function.

I think this time I will try to put my question in a different way.

FIRST I AM NOT LOOKING FOR THE BRIGHTNESS CONTROL. FOR THIS I HAVE ALREADY POSTED THE CODE ABOVE. THE ABOVE CODE IS FOR FADE IN FADE OUT WHICH CAN BE MODIFIED FOR BRIGHTNESS CONTROL.

As Brian said the in his first post "PWM is not normally used to control the rate at which an output is turned on and off". That underlined words is what I am looking at the LED should be controlled at a rate so that I can see the speed of on and off if I change the rate.

Another way :

I have a potentiometer connected to uC input and I have a LED connnected to it's output. Now what I want is as I change the POT the LED on/off should vary. In other words if the POT's wiper is in the middle the LED on/off should be at 1s and when the POT is on the left side(min) the LED on/off should be 2s and when the POT is on the right side(max) the LED on/off should be 0.25s.

Now this thing can be done without the POT also right. That means if I don't use POT I will have to give constant value in the code or I can use a button to get different values, right. THIS IS WHAT I WANT TO BE DONE USING PWM.

I think I cannot explain better than this.

Thankyou all.
 

OK, what you want is NOT to use PWM.

If you used PWM , using your "another way..." when the pot was at min setting you would get the LED on for say 1.5s then off for 0.5s, in the middle it would be on for 1s and off for 1s and at max it would be on for 0.25s and off for 1.75s.

What you are asking for is simply that the flash rate changes according to the voltage from the pot or a variable in your program.
The way to do this is:
1. read the pot using the ADC and scale the result if necessary.
2. turn the LED on
3. wait a period specified by the ADC value
4. turn the LED off
5. wait the same period as in step 3
6. go back to step 1.

Instead of step 1 you could use a variable derived from the program.

If you really want to fade in and out, PWM is the way to do it though. If you want to turn the LED on and off while also using PWM, use the TRIS control for the port driving the LED. When set to input the LED will be off, set to output it will be able to provide/sink the LED current s it will turn on.

Brian.
 
I have a potentiometer connected to uC input and I have a LED connnected to it's output. Now what I want is as I change the POT the LED on/off should vary. In other words if the POT's wiper is in the middle the LED on/off should be at 1s and when the POT is on the left side(min) the LED on/off should be 2s and when the POT is on the right side(max) the LED on/off should be 0.25s.

Now this thing can be done without the POT also right. That means if I don't use POT I will have to give constant value in the code or I can use a button to get different values, right. THIS IS WHAT I WANT TO BE DONE USING PWM.

I think I cannot explain better than this.

Seems like you have a hard time explaining to our dumb brains!
Let me get this straight for the final time your POT example because when you say led on/off do you mean led should be on for 1s and off for 1s ?? That is
If the POT's wiper is in the middle, you need the led on for 1s and off for 1s.
When the POT is on the left side, the led should be on for 2s and off for 2s.
When the POT is on the right side, the led should be on for 0.25s and off for 0.25s.

And you are trying to do the above using pwm instead of a POT.
Is that correct ??
 

Seems like you have a hard time explaining to our dumb brains!

I never said or expected you all to be dumb brains. I said I cannot explain better than this which means this all I know how to explain as I am uneducated.
Thanks for understanding.

And you are trying to do the above using pwm instead of a POT.
Is that correct ??

Exactly that is what I am looking at.

Thankyou
 

Sorry man I totally misunderstood just follow betwixt help it will work. enjoy
 

@prmurthy

I just wanted to make sure of your application in your own words. I'm afraid you have not understood the concept of pwm. Please take a look at this pwm fundamentals.
Introduction to Pulse Width Modulation (PWM) | Netrino

FIRST I AM NOT LOOKING FOR THE BRIGHTNESS CONTROL. FOR THIS I HAVE ALREADY POSTED THE CODE ABOVE. THE ABOVE CODE IS FOR FADE IN FADE OUT WHICH CAN BE MODIFIED FOR BRIGHTNESS CONTROL.

First of all, what pwm is that - it is not an on/off control. It basically switches the led on and off at a rapid speed so that a fixed voltage at the led is maintained.

Tpwm = Ton + Toff

Tpwm - Time period of a pwm wave
Ton - Time during which the switch is on
Toff - Time during which the switch is off

Remember again, these times does not mean the led is made on and off. Instead the switch is made on and off very fast so that the led is kept ON all the time.
And by varying the time during which the switch is on (Ton) at the constant Time period (Tpwm), you actually vary the duty cycle and this is usually represented in terms of percentage of the time period (Tpwm). By varying this, you actually vary the fixed output voltage at the led. Even if your intention is not to control the brightness of the led (fading in/fading out is also the same), in pwm varying the duty cycle actually varies the brightness of the led and that is the primary purpose of the pulse width modulation.

Regards the freq, what do you think the freq range for various pwm duty cycle should be where we can see the LED on off using PWM where it is visible to human eye.
Thanks
If you wanted to use the pwm for led on/off control that is visible to human eye(very low switching frequency), it is basically the exact opposite of the primary purpose of pwm(high switching frequency).

The on and off frequency ( not in Hz, in terms of times) depending on the PWM duty cycle. Like 100% duty cycle the on and off should be for 1s or so and 25% should be for 0.25s etc etc.
Thanks

Now you would have understood your mistake in the above sentence - by varying the duty cycle, you do not vary the time period but the time during which the switch is on.
For ex, For the time period of 1s
50% duty cycle => Ton = 0.5s and Toff = 0.5s
25% duty cycle => Ton = 0.25s and Toff = 0.75s

Everyone was repeatedly questioning your thread because the thing is that you were trying to do a basic on/off control using PWM which you can do it very easily using simple timer delays and i could say there is effectively no purpose of using the PWM module in your case. If you would want to learn about pwm, try to generate waveforms at a desired frequency say 10 kHz with various duty cycles. You will get a good basic understanding while doing this!

Hope this helps!!!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top