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

how Configuration PWM in Dspic33ep

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



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

how Configuration PWM in Dspic33ep
PostPosted: Wed Sep 18, 2019 9:08 am     Reply with quote

I used EX_PWM_PCD.C that is example of CCS.
Configuration PWM in this example is same below.
Code:

setup_timer1(TMR_INTERNAL | TMR_DIV_BY_1,1000 ); //1000=> fpwm=59.90 khz
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER1);

but it does not work.
I need to know configuration pwm for fpwm=60 KHz. (or any frequency without wizard!)

MCU=dspic33ep256mc504, 60 MIPS
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 11:15 am     Reply with quote

First, you won't get an output till the OC1 pin is setup with PPS.
Then you won't get an output till you set a duty cycle.
Your frequency calculation seems slightly wrong. Remember a value
of 1000, gives division by 1001. For 1/1000, you need a count of 999.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

Configuration PWM in Dspic33ep
PostPosted: Wed Sep 18, 2019 11:22 am     Reply with quote

Code same below, everything is ok but not working pwm.
Code:

#pin_select OC1 = PIN_B3
.
.
.
main(){
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_1,1000 ); //1000=> fpwm=59.90 khz
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER1);
.
.
.
while(1){
 set_pwm_duty(1, 500);
.
.
.
 }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 11:49 am     Reply with quote

Remember analog functions have to be deselected before you can
use a pin for digital. setup_adc_ports(NO_ANALOGS); or whatever pins
you do want to use for analog.
Also don't have the duty setting in the loop.
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Thu Sep 19, 2019 3:51 am     Reply with quote

I think there are several things being 'missed' in your understanding of
the OC module. The COMPARE_PWM in a basic DsPIC, mode is different
to the COMPARE_PWM_EDGE mode in the PIC33.

As posted, the timer in this will not be resetting when timer1 resets.
To make it reset with the timer requires you to set this as the SYNC
source. So:

setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER1 | COMPARE_TRIG_SYNC_TIMER1);

In the PIC30, the actual comparison is directly 'to' the selected timer
on the PIC33, this is not the case. The output compare module has it's
own internal timer. This timer can be used completely on it's own and
you don't then have to waste a timer. You can 'use' a timer as a sync
to this internal timer, but it is really just wasting a timer.

The way to setup the internal timer, is to use:

setup_compare(1, COMPARE_PWM_EDGE | COMPARE_SYSTEM_CLOCK | COMPARE_TRIG_SYNC_SELF);

Which says to run this from the system clock, and synchronise and
trigger from itself.

Then you have to set the actual timer period:

set_compare_time(1, 499, 999);

Which sets both the duty, and period in a single operation. Once the
period is set with this you can just use the pwm_duty function to change
the duty cycle.

Then if you look at the pin 'priority', the highest priority function is the
analog input, then the comparator input, followed by the USB interface,
then the peripheral, and the normal I/O. You need to ensure the
higher priority functions are disabled. So I'd have:

setup_adc_ports(NO_ANALOGS);
setup_comparator(2,NC_NC);

before trying to use pin B3.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

Configuration PWM in Dspic33ep
PostPosted: Thu Sep 19, 2019 5:50 am     Reply with quote

It was very useful Very Happy

When using this command

set_compare_time(1, 499, 999);

how calculated pwm frequency or period?
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Thu Sep 19, 2019 8:16 am     Reply with quote

Exactly the same way as your earlier calculation.

Fperiph=Fosc/2

Division is by OCxRS(third number)+1.

So Fosc=120MHz
Fperiph=60MHz

Count of 999, gives division by 1000, so

Fpwm = 60000000/1000 = 60000Hz
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