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 CCS Technical Support

Pwm frequency problem

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



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

Pwm frequency problem
PostPosted: Sat Sep 05, 2009 9:46 am     Reply with quote

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;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 05, 2009 2:16 pm     Reply with quote

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 ?
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 3:46 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Mon Sep 07, 2009 4:28 am     Reply with quote

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
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 7:02 am     Reply with quote

Thank you for your help but I didn't understand this line what does it mean /* ?
pwmval=adval/*2+2;
Ttelmah
Guest







PostPosted: Mon Sep 07, 2009 7:30 am     Reply with quote

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
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 7:38 am     Reply with quote

ok it is worked thank you
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