贴一段代码给你, 你对着研究寄存器吧。。
// ============= Motor PWM ======================
// Center aligned PWM.
// Note: The PWM period is set to dLoopInTcy/2 but since it counts up and
// and then down => the interrupt flag is set to 1 at zero => actual
// interrupt period is dLoopInTcy
PHASE1 = LOOPINTCY;
PHASE2 = LOOPINTCY;
PHASE3 = LOOPINTCY;
PTPER = 2*LOOPINTCY+1; //one trigger per PWM period
PWMCON1 = 0x0204; // Enable PWM output pins and configure them as
PWMCON2 = 0x0204; // complementary mode
PWMCON3 = 0x0204;
//I/O pins controlled by PWM
IOCON1 = 0xC000;
IOCON2 = 0xC000;
IOCON3 = 0xC000;
DTR1 = 0x0000;
DTR2 = 0x0000;
DTR3 = 0x0000;
ALTDTR1 = DDEADTIME; // 700ns of dead time
ALTDTR2 = DDEADTIME; // 700ns of dead time
ALTDTR3 = DDEADTIME; // 700ns of dead time
//fault disabled
FCLCON1 = 0x0003;
FCLCON2 = 0x0003;
FCLCON3 = 0x0003;
// FCLCON1 = 0x00FD;
// FCLCON2 = 0x00FD;
// FCLCON3 = 0x00FD;
PTCON2 = 0x0000; // Divide by 1 to generate PWM
/* low side turn on errate workaraund */
PDC1 = MIN_DUTY; // PDC cannot be init with 0, please check errata
PDC2 = MIN_DUTY;
PDC3 = MIN_DUTY;
IPC23bits.PWM1IP = 4; // PWM Interrupt Priority 4
IPC23bits.PWM2IP = 4; // PWM Interrupt Priority 4
IPC24bits.PWM3IP = 4; // PWM Interrupt Priority 4
IFS5bits.PWM1IF = 0; //clear PWM interrupt flag
IEC5bits.PWM1IE = 0; // Disable PWM interrupts
PTCON = 0x8000; // Enable PWM for center aligned operation
|