21ic问答首页 - 为什么STM32在使用定时器中断的时候ARR的参数不能给0?
为什么STM32在使用定时器中断的时候ARR的参数不能给0?
Ericknfk2023-09-04
我在使用ETR外部时钟信号时,想实现类似于外部中断的,信号来一次进入一次中断的操作。但是发现当TIM_Period参数给1-1,也就是0时,无法正常进入中断。但是将参数调整为2-1时,来两次信号又可以进入中断,请问这是为什么?uint16_t Count;
void TIM_QuicklyInit(void);
int main(void)
{
TIM_QuicklyInit();
OLED_Init();
OLED_ShowString(1,1,"Counts:");
while(1)
{
OLED_ShowNum(2,1,Count,4);
OLED_ShowNum(3,1,TIM_GetCounter(TIM2),1);
}
}
void TIM_QuicklyInit(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIOInitstructure;
GPIOInitstructure.GPIO_Mode = GPIO_Mode_IPD;
GPIOInitstructure.GPIO_Pin = GPIO_Pin_0;
GPIOInitstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOInitstructure);
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0x0F);
TIM_TimeBaseInitTypeDef TimeInitstructure;
TimeInitstructure.TIM_CounterMode = TIM_CounterMode_Up;
TimeInitstructure.TIM_ClockDivision = TIM_CKD_DIV4;
TimeInitstructure.TIM_Period = 1-1;
TimeInitstructure.TIM_Prescaler = 1-1;
TimeInitstructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TimeInitstructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_Cmd(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVICInitstructure;
NVICInitstructure.NVIC_IRQChannel = TIM2_IRQn ;
NVICInitstructure.NVIC_IRQChannelCmd = ENABLE;
NVICInitstructure.NVIC_IRQChannelPreemptionPriority = 2;
NVICInitstructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVICInitstructure);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM2,TIM_IT_Update)==SET)
Count++;
TIM_ClearFlag(TIM2,TIM_IT_Update);
}
void TIM_QuicklyInit(void);
int main(void)
{
TIM_QuicklyInit();
OLED_Init();
OLED_ShowString(1,1,"Counts:");
while(1)
{
OLED_ShowNum(2,1,Count,4);
OLED_ShowNum(3,1,TIM_GetCounter(TIM2),1);
}
}
void TIM_QuicklyInit(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIOInitstructure;
GPIOInitstructure.GPIO_Mode = GPIO_Mode_IPD;
GPIOInitstructure.GPIO_Pin = GPIO_Pin_0;
GPIOInitstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOInitstructure);
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0x0F);
TIM_TimeBaseInitTypeDef TimeInitstructure;
TimeInitstructure.TIM_CounterMode = TIM_CounterMode_Up;
TimeInitstructure.TIM_ClockDivision = TIM_CKD_DIV4;
TimeInitstructure.TIM_Period = 1-1;
TimeInitstructure.TIM_Prescaler = 1-1;
TimeInitstructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TimeInitstructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_Cmd(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVICInitstructure;
NVICInitstructure.NVIC_IRQChannel = TIM2_IRQn ;
NVICInitstructure.NVIC_IRQChannelCmd = ENABLE;
NVICInitstructure.NVIC_IRQChannelPreemptionPriority = 2;
NVICInitstructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVICInitstructure);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM2,TIM_IT_Update)==SET)
Count++;
TIM_ClearFlag(TIM2,TIM_IT_Update);
}
赞0
一般要进行内存优化,尽量提高内存的使用效率
评论
2024-01-10
赞0
超过变量128后必须使用compact模式编译
评论
2024-01-10
赞0
让尽可能多的变量使用直接寻址,提高速度
评论
2024-01-10
赞0
靠近功率开关管或整流器的电容分到的纹波电流远多于相距较远的电容分到的纹波电流
评论
2024-01-09
赞0
我们也可以稍微理解下,如果ARR=0,意味着计数周期为1个计数时钟,实质上计数器根本就没有被用上。
因为计1个时钟不也就是时钟源本身吗?那还有计数器干什么呢?
评论
2023-10-23
赞0
评论
2023-10-23
赞0
评论
2023-10-19
赞0
评论
2023-10-19
赞0
这种情况,一般属于芯片的bug,别深究了。
评论
2023-09-05
您需要登录后才可以回复 登录 | 注册