![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
theasus
Joined: 31 May 2009 Posts: 79
|
Pwm frequency problem |
Posted: Sat Sep 05, 2009 9:46 am |
|
|
I want to change the frequency of PWM by changing analog input value. And I want duty cycle to be constant. I searched this problem on this site but I couldn't solve it. Could you have post any sample code here?
Here is my circuit;
![](http://img238.imageshack.us/img238/9154/pwm.th.jpg) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 05, 2009 2:16 pm |
|
|
Quote: |
I want to change the frequency of PWM by changing analog input value. |
There are several possible methods to do this.
1. What frequency range do you want for the pwm output signal ?
(Example: 10 Hz to 100 Hz)
2. What frequency step size do you want in the output signal ?
Example(1 Hz, or 1 KHz, or a percentage such as 1%, etc.)
3. What duty cycle do you want ?
4. What is the overall purpose of this project ? |
|
![](templates/subSilver/images/spacer.gif) |
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Mon Sep 07, 2009 3:46 am |
|
|
1- 100Khz - 200Khz
2- it can be %1 or %5
3- %30
4- To determine the suitable frequency which will be ignited the
fluorescent lamb. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Sep 07, 2009 4:28 am |
|
|
You show a PIC16F877. Maximum clock rate supported 25MHz. So, given the available clock rates step in fosc/4 increments, /31 divider, gives 201612Hz, while /32 gives 195312Hz. This is the smallest step achievable at the top end of the frequency range, and also shows that exactly 200KHz, is not possible from this master clock. If you go slower (say to 24.8MHz), then you can get 200K exactly, but the step size increases, with the next frequency being 193750Hz. Still less than your 5%.
Then sticking with the 25MHz master clock, the nearest divider to 100K, is 63, giving 99206Hz.
So, total 'range, of just 32 steps, or 31, to go just inside the frequency limit at the top.
Simplest but crude solution:
Code: |
int8 adval;
int16 pwnval;
setup_ccp1(CCP_PWM);
while(true) {
adval=read_adc(); //obviously, adc channel will need to have been
//selected etc.. Also assume adc is in 8bit mode.
adval/=8; //adval, now is 0 to 31
adval+=31; //minimum divider 32 - value is divider-1
//adval now 31 to 62
pwmval=adval/*2+2; 64 to 126, stored in a _long_ to give 50% duty
setup_timer2(T2_DIV_BY_1, adval, 1); //select the frequency
set_pwm1_duty(pwmval);
delay_ms(20); Avoid looping too fast.....
}
|
Obviously this needs fuses etc., to actually work, but from a 25MHz clock, will give square wave output, at frequencies from 99KHz, to 196KHz. The step size grows at the top of the range.
Beware, if you use very high frequency drive of flourescent tubes, you can get the gas becoming more ionised than is normal, and get unexpected radiation at frequencies above the normal output. Your drive needs to be carefully designed to avoid this.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Mon Sep 07, 2009 7:02 am |
|
|
Thank you for your help but I didn't understand this line what does it mean /* ?
pwmval=adval/*2+2; |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Sep 07, 2009 7:30 am |
|
|
Just a typing error.
I was originally going to divide adval by 2 (hence adval/=2), then realised that I needed to add 2 for the result to be correct.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Mon Sep 07, 2009 7:38 am |
|
|
ok it is worked thank you |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|