View previous topic :: View next topic |
Author |
Message |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
how Configuration PWM in Dspic33ep |
Posted: Wed Sep 18, 2019 9:08 am |
|
|
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
|
|
Posted: Wed Sep 18, 2019 11:15 am |
|
|
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
|
Configuration PWM in Dspic33ep |
Posted: Wed Sep 18, 2019 11:22 am |
|
|
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
|
|
Posted: Wed Sep 18, 2019 11:49 am |
|
|
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
|
|
Posted: Thu Sep 19, 2019 3:51 am |
|
|
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
|
Configuration PWM in Dspic33ep |
Posted: Thu Sep 19, 2019 5:50 am |
|
|
It was very useful
When using this command
set_compare_time(1, 499, 999);
how calculated pwm frequency or period? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Thu Sep 19, 2019 8:16 am |
|
|
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 |
|
|
|