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

Different PWM frequency in PIC18F67K22

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



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

Different PWM frequency in PIC18F67K22
PostPosted: Sat May 21, 2016 1:32 am     Reply with quote

Dear All,

I use PIC18F46K22 (CCS V4.135) with timer 2 for PWM1 4Khz as follow
Code:

setup_timer_2(T2_DIV_BY_16, 2000, 1);
set_pwm1_duty(100);


Now I need to have second PWM with PWM3 with different frequency:
Code:

SETUP_CCP3(CCP_USE_TIMER3_AND_TIMER8);
setup_timer_3(T3_DIV_BY_2);
setup_timer_8(T8_DIV_BY_16, 100, 1);

SETUP_CCP3(CCP_PWM );
set_pwm3_duty(100);


I have always 4Khz PWM3 so can somebody suggest me solution to have different frequency in PWM3 ?

Thanks,
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Sat May 21, 2016 2:50 am     Reply with quote

You don't tell us your clock rate?.

However I'm surprised you get the frequency you expect with your timer2 setup.

The period register for timer2, is an 8bit register. Can't take '2000'. Should be accepting probably the low byte of the value you are giving, so 0xD0 (209 counts).

Then the big problem is that setup_ccp calls are not 'cumulative'. Your second setup_ccp call (CCP_PWM), turns off the timer selection setting.
Now, puzzled by your "CCP_USE_TIMER3_AND_TIMER8"

You need to read the data sheet. CCP3 defaults to using timer1 for capture and timer 2 for PWM. You then have modes that can switch it to using timer4, or timer 6 for PWM. I've loaded 4.135, and the defines available, are:
Code:
#define CCP_USE_TIMER1_AND_TIMER2       0x0000
#define CCP_USE_TIMER3_AND_TIMER4       0x0100
#define CCP_USE_TIMER5_AND_TIMER6       0x0200

Then read the note above this in the file. Note that it says the first is for compare, and the second for PWM.
So you need (assuming timer 6):
Code:

SETUP_CCP3(CCP_USE_TIMER3_AND_TIMER6 | CCP_PWM);
setup_timer_6(T6_DIV_BY_16, 100, 1);
set_pwm3_duty(50);


It does tell you in the include file:
"or (via |) together constants from each group"

Since you are using PWM, timer3 doesn't enter the equation at all.

Then (of course) since the PWM time constant is now 101 (always one greater than the value in the PR2 register), the duty cycle settings needs to be approximately half this for a near square wave.
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Mon May 23, 2016 9:39 am     Reply with quote

Thanks Ttelmah for your Help.

Clock Rate is 64 Mhz because I must use PLL in this application.

I tryed as you suggested me and I have a 9 Khz PWM in CCP3.

Now I have to work around to have 1 Khz PWM

Regards,
Fabri
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Mon May 23, 2016 10:06 am     Reply with quote

From 64MHz you can't do it.

The maximum division available is 4*16*255 = 16384
This gives 3.906KHz.

Normally you want PWM's to be fast. The hardware is designed to do this.
You could generate a timer based square wave, by just using an interrupt at 2KHz, or using a CCP to handle the actual pin.
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Mon May 23, 2016 10:31 am     Reply with quote

Yes, I'm working on now and I understand limit of my clock rate.

I'll work with firmware to have 1Khz pwm.

Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Mon May 23, 2016 11:10 am     Reply with quote

Why do you need this frequency?.
What does the PWM resolution need to be?.
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Mon May 23, 2016 11:38 am     Reply with quote

Have a look at this thread.

<http://www.ccsinfo.com/forum/viewtopic.php?t=50914&highlight=ccp>

I post code here for a 400Hz CCP based PWM, that can possibly be modified for your needs.
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Mon May 23, 2016 1:44 pm     Reply with quote

Thanks for new suggestion, I'll check that post.

Regards,
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