STM32F030K6T6 开启两个定时器后,对OLED显示有干扰,请大神解惑
void TIM16_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 12 - 1; //预分频系数为48-1,这样计数器计数频率为48MHz/48 = 1MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数器模式为向上计数模式
TIM_TimeBaseStructure.TIM_Period = 33399; //设置计数溢出大小,每计1000个数就产生一个更新事件,1ms一次中断
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复n次计数才触发中断
TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure); //将配置应用到TIM16中
TIM_ITConfig(TIM16, TIM_IT_Update, ENABLE); //使能TIM16的更新中断
TIM_Cmd(TIM16, ENABLE); //开启定时器16
//Enable the TIM16 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM16_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM16_IRQHandler(void)
{
if(TIM_GetITStatus(TIM16, TIM_IT_Update) != RESET)
{
TIM3_TimeInt();
}
TIM_ClearITPendingBit(TIM16, 0x00ff);
}
/******************************************** 输入捕获 ********************************************/
/************************************************
函数名称 : TIMER_IC_GPIO_Configuration
功 能 : 定时器输入捕获引脚配置
参 数 : 无
返 回 值 : 无
作 者 : strongerHuang
*************************************************/
void TIMER_IC_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //LED引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //高速输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推完输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //无上下拉(浮空)
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2); //复用配置
}
/************************************************
函数名称 : TIMER_IC_Configuration
功 能 : 定时器输入捕获配置
参 数 : 无
返 回 值 : 无
作 者 : strongerHuang
*************************************************/
void TIMER_IC_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 12-1; //8分频(与捕获分频相同)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //定时周期(自动从装载寄存器ARR的值)
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频因子
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; //通道2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //捕获极性
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //捕获选择
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; //捕获分频
TIM_ICInitStructure.TIM_ICFilter = 0; //捕获滤波
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_Cmd(TIM2, ENABLE); //使能TIM2
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); //使能中断
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 外设中断 */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //IRQ通道:定时器2
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)
{
//捕获中断
TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
TIM2_CaptureInt();
}
} |