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

PWM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PWM
PostPosted: Wed Apr 03, 2013 11:21 am     Reply with quote

Hello,
I need to use PWM1 and PWM2.
I got confused about how to set the Duty Cycle. Nevertheless, I need to set it to be 50%.
I will use those functions :
Code:


setup_ccp1(CCP_PWM);
set_pwm1_duty(value);
setup_ccp2(CCP_PWM);
set_pwm2_duty(value);



If I am not wrong. PWM1 and PWM2 referred to the PWM in the data sheet of my pic
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Apr 03, 2013 11:37 am     Reply with quote

if using 10bit pwm must use int16 for value or L integers
8bit 'value' ok for 8 bit pwm resolution
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 11:50 am     Reply with quote

I'm using PIC 18F2455
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 12:01 pm     Reply with quote

I have written this code so far

Code:

      setup_ccp1(CCP_PWM);
      setup_ccp2(CCP_PWM);
      setup_timer_2(T2_DIV_BY_1, 127, 1);
      set_pwm1_duty(value);
      set_pwm2_duty(value);



But , I don't think this is how it works
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 12:14 pm     Reply with quote

Try a simple program for one PWM channel:
Code:

#include <18F2455.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1
#use delay(clock=4M)

//=================================
void main()
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 125, 1);
set_pwm1_duty(63);    // 50% duty cycle

while(1);
}
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 12:20 pm     Reply with quote

Ok, Thank you .

I also want to be sure that I used the right pins. and if it helps. I'm using DC motors
PIC 18F2455
C2 for PWM1
C1 For PWM2
....
trying now
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 12:26 pm     Reply with quote

Thank You so Much.
^_^
just as I wanted.
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 9:09 pm     Reply with quote

Ok thanks, but I have another question. How can I turn the PWM off and use the port as Digital output ???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 9:13 pm     Reply with quote

The program below runs the PWM on pin C2 for 2 seconds, then it
turns off the PWM. Pin C2 will be set as an output pin, and will put
out a logic low level (0v).
Code:

include <18F2455.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1
#use delay(clock=4M)

//=================================
void main()
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 125, 1);
set_pwm1_duty(63);    // 50% duty cycle

delay_ms(2000);

setup_ccp1(CCP_OFF);

while(1);
}
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Apr 03, 2013 10:58 pm     Reply with quote

PCM programmer...
I got confused about how to calculate the Duty. I used the CCS help and figured out the following :
1- when writing setup_ccp1(CCP_PWM)
Time = (1/clock) * 4 * 16 * (period+1)
In out case :
Time = 504 us.
That means the frequency = 1.984 kHz
So,
New frequency = 0.992 kHz which makes the time = 1.008ms
Then
Duty = Time / (16 * (1/clock))
= clock*time/16
= (16*10^6 * 1.008 * 10^-3)/16
= 1008

in the program
you set it 63
I don't know how that happened
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 11:16 pm     Reply with quote

This post has many links which explain PWM in CCS (for 16F and 18F
PICs). See this link on PWM duty cycle:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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