/**************************************************************************************
*名称:PWM调试程序
*说明:要求PWM6H输出40%占空比方波,PWM7H输出60%占空比方波,PWM6L和PWM7L始终为高(CS240决定)
*其他:如有能力,要求PWM6H与PWM7H输出180°移相
***************************************************************************************/
//"文件包含“处理
#include
#define BIT7 0x0080
#define IGBTEN_ON PORTD=LATD&(~BIT7)
//配置文件
_FOSCSEL(FNOSC_FRC) //初始振荡器源选择:000=FRC振荡器
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON) //时钟切换模式:01=使能时钟切换,禁止故障保护时钟监视器
//OSC2引脚功能位(XT和HS模式除外):0=OSC2为通用数字I/O引脚
_FWDT(FWDTEN_OFF) //看门狗定时器使能:0=通过用户软件使能/ 禁止看门狗定时器
_FPOR(FPWRT_PWR128) //上电复位定时器值选择位:111=PWRT=128ms
_FICD(ICS_PGD1 & JTAGEN_OFF) //ICD通信通道选择使能位:11=在PGEC1和PGED1上进行通信
//JTAG使能位:0=禁止JTAG
void init_PWM(void);
//main主函数
int main()
{
/******** Configure Oscillator to operate the device at 39.6288Mhz(MIPS)
Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
Fosc= 7.3728*(43)/(2*2)=79.2576Mhz for Fosc, Fcy = 40Mhz *********/
/* Configure PLL prescaler, PLL postscaler, PLL divisor */
PLLFBD=41; /* M=PLLFBD+2=43 */
CLKDIVbits.PLLPOST=0; /* N2=PLLPOST+2=2 */
CLKDIVbits.PLLPRE=0; /* N1=PLLPRE+2=2 */
__builtin_write_OSCCONH(0x01); /* 快速RC振荡器(FRC)→带PLL的快速RC振荡器(FRC) */
__builtin_write_OSCCONL(0x01); /* Enable Switch */
// Wait for Clock switch to occur
while(OSCCONbits.COSC != 0b001); /* Wait for new Oscillator to become FRC with PLL */
while(OSCCONbits.LOCK != 1); /* Wait for Pll to Lock */
/**************** Setup the ADC and PWM clock for 117.9648MHz
N=APSTSCLR=1
((Posc * 16) / APSTSCLR ) = (7.3728 * 16) / 1 = 117.9648MHz
PWM resolution is 1.0596ns. ****************/
// ACLKCONbits.FRCSEL = 1; /* 为附属PLL 选择FRC时钟 */
// ACLKCONbits.SELACLK = 1; /* 附属振荡器为附属时钟分频器提供源时钟,Auxiliary Oscillator provides clock source for PWM & ADC */
// ACLKCONbits.APSTSCLR = 7; /* 附属时钟输出1分频,Divide Auxiliary clock by 1 */
// ACLKCONbits.ENAPLL = 1; /* 使能APLL */
//
// while(ACLKCONbits.APLLCK != 1); /* Wait for Auxiliary PLL to Lock */
/************************ IO端口设定 *********************************/
// ADPCFG |= 0xFFFF;
// ADPCFG2 |= 0x00FF; //所有模拟口 都设为数字IO口
//PWM模块IO初始
PORTD = LATD | 0x2038;
PORTD = LATD & 0xFF7F;
TRISD &= 0xdf47; //IGBTEN初始为低,配置为输出引脚pwm6H、6L、7H、7L配置为输出引脚,初始高电平
init_PWM();
while(1); /* Infinite Loop */
}
void init_PWM()
{
PTPER=47188; /* PTPER = ((1 / 20kHz) / 1.0596ns) = 47188, where 20kHz
is the desired switching frequency and 1.0596ns is PWM resolution. */
/*~~~~~~~~~~~~~~~~~~~~~~~ PWM6 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// PWMCON1:PWM 控制寄存器 1
PWM1CON1bits.PEN1H = 1; /* 1 = PWM模块控制PWM1H引脚 */
PWM1CON1bits.PEN1L = 0; /* 0 = GPIO模块控制PWM1L引脚 */
PWM1CON1bits.PMOD1 = 1; /* PWM I/O引脚对处于真正独立输出模式 */
PDC1 = 18874; /* Initial Duty cycle 40%(最终输出60%)*/
// DTCON1 = 64; /* Deadtime setting */
// ALTDTR6 = 64; /* Deadtime setting */
// PHASE6 = 0; /* No phase shift */
/*~~~~~~~~~~~~~~~~~~~~~~~ PWM7 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
PWM1CON1bits.PEN2H = 1; /* PWM2H is controlled by PWM module */
PWM1CON1bits.PEN2L = 0; /* PWM2L is controlled by GPIO module */
PWM1CON1bits.PMOD2 = 1; /* Select Independent Output PWM mode */
PDC2 = 28313; /* Initial Duty cycle 60(最终输出40%)*/
// DTCON2 = 64; /* Deadtime setting */
// ALTDTR7 = 64; /* Deadtime setting */
// PHASE7 = 18874; /* approximately 20us phase shift(最终移相216°) */
PTCONbits.PTEN = 1; /* Enable the PWM Module */
}