📄 pwm12.c
字号:
#if defined(__dsPIC30F__)
#include <p30fxxxx.h>
#else
#error "Does not build on this target"
#endif
#include "pwm12.h"
#define MAX_PWM 1474
void ConfigIntMCPWM(unsigned int config)
{
/* clear the Interrupt flags */
_PWMIF = 0;
_FLTAIF = 0;
/* Set priority for the period match */
_PWMIP = (0x0007 & config);
/* Set priority for the Fault A */
_FLTAIP = (0x0070 & config)>> 4;
/* enable /disable of interrupt Period match */
_PWMIE = (0x0008 & config) >> 3;
/* enable /disable of interrupt Fault A.*/
_FLTAIE = (0x0080 & config) >> 7;
#ifdef _FLTBIF
/* clear the Interrupt flags */
_FLTBIF = 0;
/* Set priority for the Fault B */
_FLTBIP = (0x0700 & config)>>8;
/* enable /disable of interrupt Fault B.*/
_FLTBIE = (0x0800 & config) >> 11;
#endif
}
void set_PWM12(signed long PWM)
{
PWMCON2bits.UDIS=1;//disable update
PDC1=MAX_PWM-(PWM/2) ;//
PDC2=MAX_PWM+(PWM+1)/2 ;//
PWMCON2bits.UDIS=0;//enable update
}
void PWM12_init(void)
{
//config PWM interrupt
ConfigIntMCPWM(PWM_FLTA_DIS_INT & PWM_INT_DIS);
set_PWM12(0);
// PTPER = period;
PTPERbits.PTPER=MAX_PWM;//PWM Time Base Period Value
// SEVTCMP = sptime;
SEVTCMPbits.SEVTCMP=MAX_PWM;//Special Event
// PWMCON1 = config2;
PWMCON1bits.PMOD1=0;//0 = PWM I/O pin pair is in the complementary output mode
PWMCON1bits.PEN1L=1;//enalbe PWML1 pin
PWMCON1bits.PEN1H=1;//enable PWMH1 pin
PWMCON1bits.PEN2L=1;//enalbe PWML2 pin
PWMCON1bits.PEN2H=1;//enable PWMH2 pin
PWMCON1bits.PEN3L=0;//disalbe PWML3 pin
PWMCON1bits.PEN3H=0;//disable PWMH3 pin
DTCON1bits.DTA=0b010000;//Unsigned 6-bit Dead Time Value bits for Dead Time Unit A
DTCON1bits.DTAPS=0b00;//Dead Time Unit A Prescale Select 1 TCY
//FLTACONbits.FAEN1=1;//Fault Input A Enable
//FLTACONbits.FLTAM=0;//Fault A Mode 0 =latches all control pins to the programmed states in FLTACON<15:8>
//FLTACONbits.FAOV1L=0;//drive low when FAULT active
//FLTACONbits.FAOV1H=0;//drive low when FAULT active
//FLTBCONbits.FBEN1=1;//Fault Input A Enable
//FLTBCONbits.FLTBM=0;//Fault A Mode 0 =latches all control pins to the programmed states in FLTACON<15:8>
//FLTBCONbits.FBOV1L=0;//drive low when FAULT active
//FLTBCONbits.FBOV1H=0;//drive low when FAULT active
// PWMCON2 = config3;
PWMCON2bits.SEVOPS=0b0000;// PWM Special Event Trigger Output 1:1 Postscale Select
PWMCON2bits.UDIS=0;//enable update
PWMCON2bits.OSYNC=1;//override next TCY
// PTCON = config1;
PTCONbits.PTSIDL=1; //PTSIDL: PWM Time Base Stop in Idle Mode
PTCONbits.PTOPS=0b0000;//PWM Time Base Output Postscale Select bits
PTCONbits.PTCKPS=0b00;//PWM Time Base Input Clock Prescale Select
PTCONbits.PTMOD=0b00;//PWM Time Base Mode Select
//10 = PWM time base operates in a continuous up/down counting mode
//00 = PWM time base operates in a free running mode
OVDCON=0xFF00;
PTCONbits.PTEN = 1; //PWM Time Base Timer Enable
}
void override_PWM12(void)
{
//OVDCON&=0xFCFC;//override PWM1, all to inactive state
OVDCON&=0xF0F0;//override PWM1 and PWM2, all to inactive state
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -