原子哥,我根据你的输入捕获程序改编一个光电编码器的测速程序,结果不理想,望帮忙。
根据定时2秒钟捕获所产生高电平次数来判断单位时间内通过角度的大小。
错误结果是车轮没有转动 都有速度输出
<timer.c>
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 TIM5CH1_CAPTURE_STA=0; //输入捕获状态
int count=0;
int ccount=0;
//定时器5中断服务函数
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)==SET)//更新中断
{ccount=count;} //捕获单位时间内通过的高电平个数,单位高电平对应的车轮转动角度固定
else //捕获中断
{ if(TIM5CH1_CAPTURE_STA&0X40) //捕获下降沿
{
count++; //单位时间高电平个数
TIM5CH1_CAPTURE_STA=0; //清空
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //设置上升沿触发
}else //捕获到上升沿
{
TIM5CH1_CAPTURE_STA=0;
TIM5CH1_CAPTURE_STA|=0X40; //比较捕捉到上升沿
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //设置为下降沿出发
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清空中断标志位
}
<main.c>
extern u8 TIM5CH1_CAPTURE_STA;
extern int count;
extern int ccount;
int main(void)
{
float Vr;
float Vw;
float time=2.0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
TIM5_CH1_Cap_Init(2000000-1,84-1); //定时器定时2s 更新中断
while(1)
{
delay_ms(10);
if(ccount!=0)//当有高电平时既有车轮有转动才打印速度
{
Vr=(6*ccount)/time; //测速盘的角速度 单位高电平走过6°
Vw=Vr*0.28; //车轮的线速度
printf("COUNT:%d SPEEDw:%f rad/s Vw:%f m/s
",ccount,Vr,Vw); //打印单位时间通过的高电平个数,测速盘的角速度,车轮的线速度
ccount=0; //测速后 清零
count=0;
TIM5CH1_CAPTURE_STA=0;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>