本帖最后由 ipman 于 2020-8-3 20:26 编辑
我把要发送的10组PWM周期和占空比放到数组里面,然后再计数器更新中断里读取下一组赋值,很奇怪,第一组脉冲丢失了!!!
uint16_t pwmOutWave[10][2] = {{100,10}, {100,30},{100,30},{100,40},{100,40},{100,50},{100,50},{100,60},{100,60},{100,80}};
uint16_t SendIndex = 0;
void Tim16_Config(uint16_t period, uint16_t pulse)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM16);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16,ENABLE);
TIM_ARRPreloadConfig(TIM16, DISABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 48-1; //1us per count
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = period;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_Pulse = pulse;
TIM_OC1Init(TIM16, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM16, TIM_OCPreload_Disable);
//TIM_ARRPreloadConfig(TIM16, ENABLE);// DISABLE立即更新 ENABLE 影子
TIM_CtrlPWMOutputs(TIM16, ENABLE);
TIM_Cmd(TIM16, ENABLE);
TIM_ITConfig(TIM16,TIM_IT_Update|TIM_IT_CC1, ENABLE);
}
void TIM16_IRQHandler(void)
{
//匹配时
if(TIM_GetITStatus(TIM16, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit (TIM16, TIM_IT_CC1); //必须要清除中断标志位
}
//溢出时更新计数和匹配
if (TIM_GetITStatus(TIM16, TIM_IT_Update) != RESET) //TIM_IT_Update
{
if(SendIndex<9)
{
SendIndex++; // 数组0在初始化时候已经执行
//重新赋值影子寄存器
TIM_SetAutoreload(TIM16, pwmOutWave[SendIndex][0]);
TIM_SetCompare1(TIM16, pwmOutWave[SendIndex][1]);
}else{
//发送完成10组,关闭
TIM_Cmd(TIM16, DISABLE);
//确保输出电平为空闲设置
TIM_ForcedOC1Config(TIM16, TIM_ForcedAction_InActive);
}
TIM_ClearITPendingBit (TIM16, TIM_IT_Update); //必须要清除中断标志位
}
void testSend(void)
{
SendIndex = 0;
Tim16_Config(pwmOutWave[SendIndex][0],pwmOutWave[SendIndex][1]);
} |