View previous topic :: View next topic |
Author |
Message |
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
Help me control brightness Led using PWM module |
Posted: Wed Aug 26, 2009 12:22 am |
|
|
I have learn about pic16f877a last month. I have problems with PWM with PIC16F877A, and I don't myself understand it. Bad Bad Bad, I think I wrong with learning of me.
Can you have help me to control brightness of led using PWM Module of Pic 16F877A ?
Thanks you all you !
P/S: I'm vietnamese, so english skill of me very bad !!!! _________________ Begin Begin Begin !!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 26, 2009 11:34 am |
|
|
Quote: |
Can you have help me to control brightness of led using PWM module
of Pic 16F877A ? |
Here is demonstration program.
This program creates a PWM output signal on the CCP1 pin.
It reads a trimpot voltage (from 0v to +5v) on pin A0, and
converts it to a PWM duty cycle value. Connect an LED to
(and 470 ohm series resistor) to the CCP1 pin. As you turn
the trimpot, the LED brightness will change.
Code: |
#include <16F877A.h>
#device adc=8
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
//================================
void main()
{
int8 value;
setup_ccp1(CCP_PWM);
// Set the PWM frequency for 244 Hz (with a 4 MHz crystal)
setup_timer_2(T2_DIV_BY_16, 255, 1);
setup_port_a(AN0);
setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
set_adc_channel(0);
delay_us(20);
while(1)
{
value = read_adc();
set_pwm1_duty(value);
}
}
|
|
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Wed Aug 26, 2009 6:32 pm |
|
|
Thank you very much !
But, I want the PWM Module to run automatically by using loop statement to drive value of cycle duty !
Thank you !
See you soon _________________ Begin Begin Begin !!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 26, 2009 7:22 pm |
|
|
1. Delete all 4 lines of code that setup the ADC.
2. Delete the line that reads the ADC in the while() loop.
3. Above the while() loop, set the 'value' variable to 0.
4. Inside the while loop, increment the 'value' variable by 1
after the set_pwm1_duty() statement.
5. Inside the while loop, add a delay_ms(10) statement at the end
of the loop. |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Thu Aug 27, 2009 9:54 pm |
|
|
First, thanks PCM programmer help me !
This code after edit with manual of you !
But it is run no correct. It is very slow, although I change value of cycle_duty!
Can you help me ?
Code: |
#include <16f877a.h>
#include <def_877a.h>
#fuses nowdt,noprotect,put,xt,hs
#use fast_io(c)
#use delay(clock=12000000)
void main()
{
int8 i,value;
trisc=0x00;
output_low(PIN_C1);
setup_ccp1(CCP_PWM);
// Set the PWM frequency for 244 Hz (with a 4 MHz crystal)
setup_timer_2(T2_DIV_BY_4, 255, 1);
//code nay dung dieu khien PWM bang bien tro mac o chan AN0 theo kieu doc ADC
//setup_port_a(AN0);
//setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
//set_adc_channel(0);
//delay_us(20);
while(1)
{
portc=0x00;
value =0;
for (value=1;value<=200;++value)
{
set_pwm1_duty(value);
delay_ms(30);
}
}
}
|
thanks you verymuch !
SEE YOU SOON ! _________________ Begin Begin Begin !!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 27, 2009 10:06 pm |
|
|
Your code is different from my description. That might be the reason why
it's slow.
You have done several other changes from my original program.
Some of the are not needed. Other changes might be a mistake. |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Wed Sep 02, 2009 1:03 am |
|
|
you can manual me do !
I only have a pic16f877a !
thank you ! _________________ Begin Begin Begin !!! |
|
|
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
hey try this |
Posted: Wed Sep 02, 2009 8:04 am |
|
|
this works fine in my hardware
Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=14000000)
void main(){
int data;
delay_ms(50);
setup_timer_2(T2_DIV_BY_1,254,1);
setup_ccp1(ccp_pwm);
while(1){
data=data+5;
if(data==255){data=0;}
delay_ms(50);
set_pwm1_duty(data);
}
}
|
Try this dude it is simple |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Sep 03, 2009 3:51 pm |
|
|
You really want to use hardware PWM though. It's free hardware dedicated to the task of toggling and timing.
This is good and efficient hardware usage.
Regards,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Tue Sep 08, 2009 10:18 pm |
|
|
bkamen wrote: | You really want to use hardware PWM though. It's free hardware dedicated to the task of toggling and timing.
This is good and efficient hardware usage.
Regards,
-Ben |
you can tell clearly, i don't think xxx ? _________________ Begin Begin Begin !!! |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Tue Sep 08, 2009 10:19 pm |
|
|
oh no, i can control all port with PWM ?
_________________ Begin Begin Begin !!! |
|
|
quocchien276
Joined: 08 Jul 2010 Posts: 2
|
|
Posted: Thu Jul 08, 2010 11:47 am |
|
|
Welcome tieuchuan.
I am Vietnamese and this should be written in English.
I also like you, am having trouble at school before the new day.
Using the pic16f877a for PWM is too difficult.
Can you can send me some code examples to light up an LED gradually ?
Because then my problem will be settled.
Thank you very much
--------
chào bạn tieuchuan.
là người Việt lun nên khỏi cần viết tiếng Anh.
Mình cũng đang gặp khó khăn giống bạn lúc trước đây.mình mới học pic16f877a được 1 tháng.Đến phần PWM thì gặp khó khăn quá.
Bạn có thể gửi cho mình code ví dụ làm cho mấy led sáng dần rùi tối dần dần được ko?Vì vấn đề này chắc hẳn bạn đã giải quyết được rồi. |
|
|
quocchien276
Joined: 08 Jul 2010 Posts: 2
|
|
Posted: Fri Jul 09, 2010 5:00 am |
|
|
Tieuchuan,
Please help me. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
alex_ccs
Joined: 27 Oct 2010 Posts: 3
|
|
Posted: Wed Oct 27, 2010 9:46 am |
|
|
Hi PCM programmer,
I have some question.
1. Can I simulate your source code on Proteus?
2. POT 1K is ok? |
|
|
|