按照数据手册的PWM设置步骤,PWM初始化函数如下:
void PWM_Init(void)
{
CCPTMRS = 0; //此行可注释,默认TMR2作为PWM时钟源
RC3PPS = 0x05; //设置CCP1/PWM1映射到引脚RC3
TRISCbits.TRISC3 = 1; //禁止RC3输出
T2PR = 31; //周期寄存器
CCP1CON = 0x8C; //使能CCP1,PWM模式
//PIR6bits.CCP1IF = 1;
CCPR1H = 0x04;
CCPR1L = 0x00; //占空比
CCP1CON |= 0x10;
PIR4bits.TMR2IF = 0; //清除中断标志
T2CLKCON = 1; //时钟源选择:1-Fosc/4
T2CON = 0x20; //预分频:1/4
T2CONbits.ON = 1; //启动TMR2
while( !(PIR4bits.TMR2IF) );//等待TMR2溢出
TRISCbits.TRISC3 = 0; //使能RC3输出
}
RC3脚,始终无法输出PWM波形! |