|
|
View previous topic :: View next topic |
Author |
Message |
Ted Larson Guest
|
Hardware PWM??? |
Posted: Fri Jul 11, 2003 1:39 pm |
|
|
I have just tried to use hardware PWM using CCP1, and CCP2, on a PIC18F252 for the first time...and I am not having much luck.
I am using a 40mhz PLL clock. Everything else on the PIC seems to be working fine...flashing LED's..etc.
From the examples, I created this code.
I want approx. a 20Khz signal with a 50\% duty cycle.
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 127, 1);
set_pwm1_duty(520);
set_pwm2_duty(520);
Anyway....doesn't seem to work. The CCP1 and CCP2 pins, are always high, and no pulses.
Am I missing something? Is there something additional I should be doing?
I also recognize that CCP2 is multiplexed, and you should select either RC1 or RB3, but couldn't figure out how to do this with the CCS directives. Or do I have to fiddle with the registers by hand to make sure it is selected?
Any ideas would be greatly appreciated.
Thanks,
- Ted
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515887 |
|
|
R.J.Hamlett Guest
|
Re: Hardware PWM??? |
Posted: Fri Jul 11, 2003 2:51 pm |
|
|
:=I have just tried to use hardware PWM using CCP1, and CCP2, on a PIC18F252 for the first time...and I am not having much luck.
:=
:=I am using a 40mhz PLL clock. Everything else on the PIC seems to be working fine...flashing LED's..etc.
:=
:=From the examples, I created this code.
:=I want approx. a 20Khz signal with a 50\% duty cycle.
:=
:=setup_ccp1(CCP_PWM);
:=setup_ccp2(CCP_PWM);
:=setup_timer_2(T2_DIV_BY_4, 127, 1);
:=set_pwm1_duty(520);
:=set_pwm2_duty(520);
:=
:=Anyway....doesn't seem to work. The CCP1 and CCP2 pins, are always high, and no pulses.
:=
:=Am I missing something? Is there something additional I should be doing?
:=
:=I also recognize that CCP2 is multiplexed, and you should select either RC1 or RB3, but couldn't figure out how to do this with the CCS directives. Or do I have to fiddle with the registers by hand to make sure it is selected?
:=
:=Any ideas would be greatly appreciated.
:=
:=Thanks,
:=
:=- Ted
The problem is the setting of the two duty cycle values. The PWM, drops to low, when the Timer count, reaches the value in the PWM register. You are setting the maximum count to 127, which will then never result in an output drop. The 'key' is that the accuracy you can specify for the PWM, is directly proportional to how high you can get the value in the timer register (up to the maximum that the PWM register allows). You want a total division of 2000 from the master clock (for 20kHz from 40MHz), and there is a built in /4 allready there. Hence I'd suggest:
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
//This gives the required /2000 division
setup_timer_2(T2_DIV_BY_1, 499, 1);
//Now set these to half the total period
set_pwm1_duty(250);
set_pwm2_duty(250);
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515889 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Hardware PWM??? |
Posted: Fri Jul 11, 2003 7:54 pm |
|
|
Isn't Timer2 8bit? Will 499 work?
:=:=I have just tried to use hardware PWM using CCP1, and CCP2, on a PIC18F252 for the first time...and I am not having much luck.
:=:=
:=:=I am using a 40mhz PLL clock. Everything else on the PIC seems to be working fine...flashing LED's..etc.
:=:=
:=:=From the examples, I created this code.
:=:=I want approx. a 20Khz signal with a 50\% duty cycle.
:=:=
:=:=setup_ccp1(CCP_PWM);
:=:=setup_ccp2(CCP_PWM);
:=:=setup_timer_2(T2_DIV_BY_4, 127, 1);
:=:=set_pwm1_duty(520);
:=:=set_pwm2_duty(520);
:=:=
:=:=Anyway....doesn't seem to work. The CCP1 and CCP2 pins, are always high, and no pulses.
:=:=
:=:=Am I missing something? Is there something additional I should be doing?
:=:=
:=:=I also recognize that CCP2 is multiplexed, and you should select either RC1 or RB3, but couldn't figure out how to do this with the CCS directives. Or do I have to fiddle with the registers by hand to make sure it is selected?
:=:=
:=:=Any ideas would be greatly appreciated.
:=:=
:=:=Thanks,
:=:=
:=:=- Ted
:=The problem is the setting of the two duty cycle values. The PWM, drops to low, when the Timer count, reaches the value in the PWM register. You are setting the maximum count to 127, which will then never result in an output drop. The 'key' is that the accuracy you can specify for the PWM, is directly proportional to how high you can get the value in the timer register (up to the maximum that the PWM register allows). You want a total division of 2000 from the master clock (for 20kHz from 40MHz), and there is a built in /4 allready there. Hence I'd suggest:
:=
:=setup_ccp1(CCP_PWM);
:=setup_ccp2(CCP_PWM);
:=//This gives the required /2000 division
:=setup_timer_2(T2_DIV_BY_1, 499, 1);
:=//Now set these to half the total period
:=set_pwm1_duty(250);
:=set_pwm2_duty(250);
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515895 |
|
|
Chang-Huei Wu Guest
|
Re: Hardware PWM??? |
Posted: Fri Jul 11, 2003 9:02 pm |
|
|
<font face="Courier New" size=-1>:=I have just tried to use hardware PWM using CCP1, and CCP2, on a PIC18F252 for the first time...and I am not having much luck.
:=
:=I am using a 40mhz PLL clock. Everything else on the PIC seems to be working fine...flashing LED's..etc.
:=
:=From the examples, I created this code.
:=I want approx. a 20Khz signal with a 50\% duty cycle.
:=
:=setup_ccp1(CCP_PWM);
:=setup_ccp2(CCP_PWM);
:=setup_timer_2(T2_DIV_BY_4, 127, 1);
:=set_pwm1_duty(520);
argument range = 0 ~ (127+1)*4 = 0 ~ 512 (or 511 ?)
so, set_pwm1_duty(512) will give you 100\% duty,
since that your '520' is asking for 101.6\% duty, you got HIGH all the time.
set_pwm1_duty(256) should generate 50\% duty in your setting.
Best wishes
Warning: set_pwm1_duty(250) will generate 100\% duty ! ?
and set_pwm1_duty((int16)250) will generate 49\% duty ! ?
check CCS_Help (manual) and try it (I forgot).
CCS_Help: Writes the 10-bit value to the PWM to set the duty. An 8-bit value may be used if the least significant bits are not required. If value is an 8 bit item it is shifted up with two zero bits in the lsb positions to get 10 bits. </font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515898 |
|
|
R.J.Hamlett Guest
|
Re: Hardware PWM??? |
Posted: Sat Jul 12, 2003 2:23 am |
|
|
:=Isn't Timer2 8bit? Will 499 work?
No, sorry. One of the new chips has 16bit timers on all channels, and I have been playing with this, and forgot you were talking about the 18F252 (problem generally when dealing with all the different versions...). Drop your prescaller to 'T2_DIV_BY_2', set the divisor to 249 (which gives 20kHz - 127, gives just over 19kHz, and setting the value to 500, should give the required result. :-)
Best Wishes
:=:=:=I have just tried to use hardware PWM using CCP1, and CCP2, on a PIC18F252 for the first time...and I am not having much luck.
:=:=:=
:=:=:=I am using a 40mhz PLL clock. Everything else on the PIC seems to be working fine...flashing LED's..etc.
:=:=:=
:=:=:=From the examples, I created this code.
:=:=:=I want approx. a 20Khz signal with a 50\% duty cycle.
:=:=:=
:=:=:=setup_ccp1(CCP_PWM);
:=:=:=setup_ccp2(CCP_PWM);
:=:=:=setup_timer_2(T2_DIV_BY_4, 127, 1);
:=:=:=set_pwm1_duty(520);
:=:=:=set_pwm2_duty(520);
:=:=:=
:=:=:=Anyway....doesn't seem to work. The CCP1 and CCP2 pins, are always high, and no pulses.
:=:=:=
:=:=:=Am I missing something? Is there something additional I should be doing?
:=:=:=
:=:=:=I also recognize that CCP2 is multiplexed, and you should select either RC1 or RB3, but couldn't figure out how to do this with the CCS directives. Or do I have to fiddle with the registers by hand to make sure it is selected?
:=:=:=
:=:=:=Any ideas would be greatly appreciated.
:=:=:=
:=:=:=Thanks,
:=:=:=
:=:=:=- Ted
:=:=The problem is the setting of the two duty cycle values. The PWM, drops to low, when the Timer count, reaches the value in the PWM register. You are setting the maximum count to 127, which will then never result in an output drop. The 'key' is that the accuracy you can specify for the PWM, is directly proportional to how high you can get the value in the timer register (up to the maximum that the PWM register allows). You want a total division of 2000 from the master clock (for 20kHz from 40MHz), and there is a built in /4 allready there. Hence I'd suggest:
:=:=
:=:=setup_ccp1(CCP_PWM);
:=:=setup_ccp2(CCP_PWM);
:=:=//This gives the required /2000 division
:=:=setup_timer_2(T2_DIV_BY_1, 499, 1);
:=:=//Now set these to half the total period
:=:=set_pwm1_duty(250);
:=:=set_pwm2_duty(250);
:=:=
:=:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515903 |
|
|
|
|
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
|