View previous topic :: View next topic |
Author |
Message |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
PWM |
Posted: Wed Apr 03, 2013 11:21 am |
|
|
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
|
|
Posted: Wed Apr 03, 2013 11:37 am |
|
|
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
|
|
Posted: Wed Apr 03, 2013 11:50 am |
|
|
I'm using PIC 18F2455 |
|
|
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Wed Apr 03, 2013 12:01 pm |
|
|
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
|
|
Posted: Wed Apr 03, 2013 12:14 pm |
|
|
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
|
|
Posted: Wed Apr 03, 2013 12:20 pm |
|
|
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
|
|
Posted: Wed Apr 03, 2013 12:26 pm |
|
|
Thank You so Much.
^_^
just as I wanted. |
|
|
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
|
Posted: Wed Apr 03, 2013 9:09 pm |
|
|
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
|
|
Posted: Wed Apr 03, 2013 9:13 pm |
|
|
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
|
|
Posted: Wed Apr 03, 2013 10:58 pm |
|
|
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
|
|
|
|