在PWM溢出或是比较中断时,进中断将一个IO口翻转,然后执行中断程序(用延时3us测试),屏蔽中断程序,可以看IO翻转边沿与PWM翻转边沿对齐,当执行中断程序时,IO翻转边沿滞后PWM翻转边沿。以下是初始化配置与中断程序。void PWM1_INIT(void)
{
CLK_PCKENR1 |= 0x80;//开启定时器1外设时钟
tiM1_CR1 = (1<<7); //自动预装载允许
//设置TIM1 CH1 与 TIM1 CH1N通道
TIM1_CCMR1 = (6<<4)|(1<<3)|(1<<2); //TIM1 CH1 PWM1 输出比较1预装载使能 输出比较1 快速使能
//TIM1_CCER1 |= 0x01; //CC1为输出 高电平有效
TIM1_CCER1 |= (1<<0)|(1<<2); //CC1为输出 OC1高电平有效 开启OC1N OC1N高电平有效
//设置TIM1 CH2 与 TIM1 CH2N通道
TIM1_CCMR2 =(6<<4)|(1<<3)|(1<<2); //TIM1 CH2 输出模式 PWM1 输出比较2预装载使能 输出比较2 快速使能
TIM1_CCER1 |= (1<<4)|(1<<6); //CC2为输出 互补输出使能
TIM1_PSCRH = 0x00; //预分频 0
TIM1_PSCRL = 0x00; //16M
TIM1_ARRH = 0x01; //设定自动重装载值高8位
TIM1_ARRL = 0x40; //设置自动重装载值低8位
TIM1_CCR1H = 0x00;
TIM1_CCR1L = 0x64; // 占空比值
TIM1_CCR2H = 0x00;
TIM1_CCR2L = 0x64; // 占空比值
//TIM1_BKR |= (1<<7)|(1<<4); //刹车寄存器 使能OC1输出 定时器不工作时输出无效电平 开启刹车输入 刹车输入低电平有效
TIM1_BKR |= (1<<7); //主输出使能 关闭刹车输入
TIM1_DTR = 0x18; //死区时间 24*125ns=3000ns
TIM1_EGR = (1<<0); //产生更新事件
TIM1_CR1 |= 0x01; //使能计数器
TIM1_IER |= 0x01; //允许更新中断
}
#pragma vector=0xD
__interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)
{
TIM1_SR1_UIF = 0; //更新中断标志
PC_ODR_ODR5 ^= 1;
//PC_ODR_ODR5 = 1;
//Delay_Just(5);
//PC_ODR_ODR5 = 0;
// TIM1_SR1_UIF = 0; //更新中断标志
}