CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help me control brightness Led using PWM module
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 11:34 am     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Aug 26, 2009 6:32 pm     Reply with quote

Thank you very much ! Very Happy
But, I want the PWM Module to run automatically by using loop statement to drive value of cycle duty !
Thank you !
See you soon Cool
_________________
Begin Begin Begin !!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 7:22 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Aug 27, 2009 9:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 27, 2009 10:06 pm     Reply with quote

Quote:
it is very slow

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

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Sep 02, 2009 1:03 am     Reply with quote

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

View user's profile Send private message

hey try this
PostPosted: Wed Sep 02, 2009 8:04 am     Reply with quote

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 Laughing
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Sep 03, 2009 3:51 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Sep 08, 2009 10:18 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Sep 08, 2009 10:19 pm     Reply with quote

oh no, i can control all port with PWM ?
Question Question Question
_________________
Begin Begin Begin !!!
quocchien276



Joined: 08 Jul 2010
Posts: 2

View user's profile Send private message

PostPosted: Thu Jul 08, 2010 11:47 am     Reply with quote

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. Very Happy
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

View user's profile Send private message

PostPosted: Fri Jul 09, 2010 5:00 am     Reply with quote

Tieuchuan,
Please help me.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 09, 2010 12:08 pm     Reply with quote

Tienchuan is gone from the forum.

The PWM code to control the brightness of an LED was posted near
the start of this thread. Here is the link:
http://www.ccsinfo.com/forum/viewtopic.php?t=40007&start=1
alex_ccs



Joined: 27 Oct 2010
Posts: 3

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 9:46 am     Reply with quote

Hi PCM programmer,

I have some question.
1. Can I simulate your source code on Proteus?
2. POT 1K is ok?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 3:51 pm     Reply with quote

I don't have Proteus. I don't know very much about it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group