此程序202也通用,PWM输出频率计算是MCU跑80MHz时候的,输出脚是PWM1L。详细的注释
//--------------------------PWM 初始化-------------------------------------------------------------------
void InitPWM(void)
{//PWM 周期 = [(PRy) + 1] TCY/(P1TMR定时器预分频值)
// P1TCON = 0x0000;
P1TCONbits.PTEN = 0; //PWM 时基关闭
P1TCONbits.PTSIDL = 0; //PWM 时基在CPU 空闲模式下运行
P1TCONbits.PTOPS = 0; //1:1 后分频比
P1TCONbits.PTCKPS = 0; //PWM 时基输入时钟周期为Tcy (1:1 预分频比)
P1TCONbits.PTMOD = 0; //PWM 时基在自由运行模式下工作
// PWM1CON1 = 0x0101; //PWM I/O 引脚对处于独立PWM 输出模式,1L有效
PWM1CON1bits.PMOD1 = 1; //PWM I/O 引脚对处于独立PWM 输出模式
PWM1CON1bits.PMOD2 = 0;
// PWM1CON1bits.PMOD3 = 0;
PWM1CON1bits.PEN1L = 1; //使能PWM1L 引脚,用于PWM 输出
PWM1CON1bits.PEN1H = 0;
PWM1CON1bits.PEN2L = 0;
PWM1CON1bits.PEN2H = 0;
// PWM1CON1bits.PEN3L = 0;
// PWM1CON1bits.PEN3H = 0;
// PWM1CON2 = 0x0000;
PWM1CON2bits.SEVOPS = 0; //1:1 后分频比
PWM1CON2bits.IUE = 0; //更新到有效PxDC 寄存器与PWM 时基同步
PWM1CON2bits.OSYNC = 0; //通过PxOVDCON 寄存器进行的输出改写在下一个TCY 边界发生
PWM1CON2bits.UDIS = 0; //使能来自占空比和周期缓冲寄存器的更新
P1DTCON1 = 0x0000;
P1DTCON2 = 0x0000;
P1FLTACON = 0x0000;
P1OVDCON = 0x0100; //PWM1L有效
// P1TPER = 999; //40KHz,最大P1DC1=2000
// P1TPER = 1599; //25KHz,最大P1DC1=3200
P1TPER = 1999; //20KHz,最大P1DC1=4000
P1DC1 = 0; //占空比
P1TCONbits.PTEN=1; //允许PWM1输出
}
更多dsPIC技术资料,可到http://www.714e.com/论坛的Microchip栏目查找。
文章评论(0条评论)
登录后参与讨论