void init_TPWM(void)
{
P2_MFP &= ~(P20_AD8_PWM0 |
P21_AD9_PWM1 |
P22_AD10_PWM2|
P23_AD11_PWM3|
P24_AD12_PWM4|
P25_AD13_PWM5
);
P2_MFP |= (PWM0|PWM1|PWM2|PWM3|PWM4|PWM5); //设置P2.0~5为PWM0~5功能
P2_PMD &= ~(Px0_PMD | Px1_PMD | Px2_PMD | Px3_PMD| Px4_PMD | Px5_PMD);
P2_PMD |= (Px0_OUT | Px1_OUT | Px2_OUT | Px3_OUT| Px4_OUT | Px5_OUT); //设置P2.0~5为推挽输出模式
APBCLK |= (PWM01_CLKEN | PWM23_CLKEN|PWM45_CLKEN | PWM67_CLKEN); //使能PWM0,1和PWM2,3时钟PWM4,5和PWM6,7时钟
CLKSEL1 = CLKSEL1 & (~(PWM01_CLK | PWM23_CLK));
CLKSEL1 |= ( PWM01_HCLK | PWM23_HCLK);
CLKSEL2 = CLKSEL2&(~(PWM45_CLK | PWM67_CLK));
CLKSEL2 |=(PWM45_HCLK | PWM67_HCLK);
PPRA = PWM_PRESCALAE | DEAD_ZONE_INTERVAL; //设置PWM0,1和pWM2,3时钟预分频值和PWM1,3死区长度
PPRB = PWM_PRESCALAE | DEAD_ZONE_INTERVAL; //设置PWM4,5和pWM6,7时钟预分频值和PWM5,6死区长度
CSRA = CSRA & (~(CSR0 | CSR1 | CSR2 | CSR3));
CSRA |= (CSR0_CLK_1|CSR1_CLK_1|CSR2_CLK_1|CSR3_CLK_1); //设置PWM0~3时钟分频值
CSRB = CSRB & (~(CSR4 | CSR5 | CSR6 | CSR7));
CSRB |= (CSR4_CLK_1|CSR5_CLK_1|CSR6_CLK_1|CSR7_CLK_1); //设置PWM4~7时钟分频值
PCRA |= ( PWM01TYPE_CEN|PWM23TYPE_CEN|CH1INV_ON|CH3INV_ON|DZEN01| DZEN23); //设定PWM2,3为互补输出模式,并且PWM2反向
PCRA |= (CH0_AU_RL | CH1_AU_RL | CH2_AU_RL | CH3_AU_RL); //设定PWM0,1,2,3为自动重载模式
PCRB |= ( PWM45TYPE_CEN|PWM67TYPE_CEN|CH5INV_ON|DZEN45| DZEN67); //设定PWM4,5为互补输出模式
PCRB |= (CH4_AU_RL | CH5_AU_RL | CH6_AU_RL | CH7_AU_RL); //设定PWM4,5,6,7为自动重载模式
CNR0A = CNR2A = CNR0B = CNR2B = T_S; //设定PWM0,1,2,3,4,5,6,7周期
CMR0A = CMR2A = T_S_2; //设定PWM0,1,3,4高占空比50%
CMR0B = T_S_2; //设定PWM4,5高占空比50%
CMR2B = 45; //设定PWM6,7高占空比
PIERB |= ( PWMIE6|INT67TYPE); //使能PWM6中断 //采样时间为中点
NVIC_ISER |= PWMB_INT; //使能NVIC PWMB中断向量
} |