BenWhitby
Joined: 09 Dec 2008 Posts: 2
|
PWM on DSPIC30F |
Posted: Tue Dec 09, 2008 7:10 am |
|
|
I am currently working on a project to control a Buck Converter. To do this I am trying to generate a PWM signal at a fixed frequency of 50 KHz and I require at least ten bits of resolution (1024 steps). I have attempted using the standard PWM Output Capture Module on the DSPIC30F2011 without much success. I have calculated the period register value using the following formula obtained from the DSPIC30F Reference Manual: PWM Period = [(PRy) + 1] _ TCY _ (TMRy Prescale Value). To clock the device I am using a 15MHz external crystal in the HS/2 w/PLL 16x mode. This should give me a frequency of 120Mhz. (30MIPS), yes? The timer 2 Prescale value is set to 1.
Code: |
1 / 50 KHz = (PR2 + 1) * Tcy * (Timer 2 Prescale Value)
20uSecs = (PR2 + 1) * 4 / 120MHz * (1) Solving this gives a PR2 value of 599
|
To obtain the resolution I have used the following formula: Code: | 1 / 50 KHz = PWM Resolution * 1/120MHz * 1 |
Working this through gives a resolution of 2400 steps or 11.23Bits So by my reasoning if I set the period register to 599 and the duty cycle to 2400 I should get a 50KHz square wave with a hundred percent duty cycle. However, I get nothing of the sort. In fact I cannot even get a square wave to oscillate at any frequency with any duty cycle. I am worried that the problem may be “oscillator related” as the CCS PCD compiler seems to prevent me from setting certain oscillator fuses on the DSPIC30F2011 (for example HS and XT). When I use #fuses it does not seem to set the correct configuration bits. Is this a known problem?
I am using the CCS C Compiler and the code is below. Are there any problems with the code below?
Code: |
#include <30F3012.h>
#fuses HS2_PLL16,NOPUT,NOBROWNOUT,NOWDT
#use delay(clock=120000000)
while (1)
{
setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,599); // SHOULD GIVE 50KHz assuming 120MHz Oscillator
set_pwm_duty(1, 2400); // Setup for 100% duty cycle
}
} |
Any help or advice would be much appreciated. |
|