霍尔传感器测速,代码在测试的时候发现有问题
1,使用Timer3 channel 1, PB4接入霍尔传感器,上升沿触发计数
2,使用timer9产生100ms的定时中断取数,放入一个10位的数组中
3,测试用一个timer产生1Khz的脉冲,按理说1000ms应该计数是1000个,但不知为什么只收到500个,少了一半?
u32 hall1_value[10]={0,0,0,0,0,0,0,0,0,0}; //该数组用于存储左轮霍尔传感器数据
u8 CMP_Hall_R=0;
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_Initure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能TIM3时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOC0的时钟
//GPIO_DeInit(GPIOC); //恢复GPIOC配置到缺省值
GPIO_Initure.GPIO_Pin=GPIO_Pin_4;
GPIO_Initure.GPIO_Mode=GPIO_Mode_AF;
GPIO_Initure.GPIO_Speed=GPIO_High_Speed;
GPIO_Initure.GPIO_OType=GPIO_OType_PP;
GPIO_Initure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_Initure);//初始化PC6
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3); //PB6复用位定时器3
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=0; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3
TIM3->CCMR1|=0X0001;
TIM3->CCER&=0XFFF5;
TIM3->SMCR|=0X0057;
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE); //使能定时器3
}
//定时器3中断服务函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
{
//printf("Timer3 timeout
");
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
//产生100ms中断取数
void TIM9_Int_Init(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE); //使能TIM9时钟
TIM_DeInit(TIM9);
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseInitStructure);//初始化TIM3
TIM_ITConfig(TIM9,TIM_IT_Update,ENABLE); //允许定时器3更新中断
TIM_Cmd(TIM9,ENABLE); //使能定时器3
NVIC_InitStructure.NVIC_IRQChannel=TIM1_BRK_TIM9_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//定时器9中断服务函数
void TIM1_BRK_TIM9_IRQHandler(void)
{
u8 cmp_message_length;
if(TIM_GetITStatus(TIM9,TIM_IT_Update)==SET) //溢出中断
{
TIM_Cmd(TIM3, DISABLE);
TIM_Cmd(TIM4, DISABLE);
//每一百毫秒取一次霍尔传感器值
if(CMP_Hall_R<9)
{
hall1_value[CMP_Hall_R]=TIM3->CNT;
CMP_Hall_R++;
}
else
{
hall1_value[CMP_Hall_R]=TIM3->CNT;
hall2_value[CMP_Hall_R]=TIM4->CNT;
CMP_Hall_R=0;
}
TIM3->CNT&=0X0000; //清除计数器hall sensor1计数寄存器的值
TIM_Cmd(TIM3, ENABLE);
}
TIM_ClearITPendingBit(TIM9,TIM_IT_Update); //清除中断标志位
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
TImer9 初始化时这样的:
TIM9_Int_Init(1000-1, 8400-1);
我在timer9的中断程序中加了一个GPIO 管脚,toggle这个bit, 用示波器看了, Timer9 的时长是50ms而不是100ms,计数是对的,但我的timer9初始化是用你的例程,应该是100ms才对?
自问自答。。。你好寂寞
一周热门 更多>