代码pwm模式初始化:
IEC4bits.CCP2IE=0; //禁止CCP2中断
CCP2CON1Lbits.CCSEL = 0; // PWM
CCP2CON1Lbits.MOD = 0b0101; //PWM模式
//Configure MCCP Timebase
CCP2CON1Lbits.TMR32 = 0; // Set timebase width (16-bit)
CCP2CON1Lbits.TMRSYNC = 0;//
CCP2CON1Lbits.CLKSEL = 0b000; // 系统时钟
CCP2CON1Lbits.TMRPS = 0b00; // 预分频比(1:1)
CCP2CON1Hbits.TRIGEN = 0; // 禁止时基的触发操作
CCP2CON1Hbits.SYNC = 0b11111; //与周期寄存器匹配完无中断事件发生
//Configure MCCP output for PWM signal
CCP2CON2Hbits.OCAEN = 1; // PWM输出引脚OCM2A
CCP2CON3Hbits.OUTM = 0b000; // PWM输出模式 可转向单输出模式
CCP2CON3Hbits.POLACE = 0; //输出引脚极性为高电平有效
CCP2TMRL = 0x0000;//定时器寄存器
CCP2PRL = 0xffff; //定时器周期寄存器
CCP2RA = 0x1000; // 匹配为高电平
CCP2RB = 0x8000; // 匹配为低电平 CCP2RB-CCP1RA=脉冲周期
TRISDbits.TRISD1=0;
ANSELD=0x0000;
CCP2CON1Lbits.CCPON = 1; // Turn on MCCP module
__builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS
RPOR12bits.RP24R = 0x0010; //RD1->CCPM2A输出比较
__builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
以上设置,无脉冲产生 |