这是自己写的一个加速度传感器的代码,但是使用stm32的捕获功能就是无法成功,求大神!
#include "Memsic2125.h"
#include <math.h>
#include "usart.h"
#define PI 3.1415926
#define TILT(a) a*180/PI
float Xmg,AngleX;
float Ymg,AngleY;
u16 Ta=0; //Ta:X轴上升开始计数Ta=0
u16 Tb,Tc,Td; //Tb:X轴下降沿结束计数;Tc:Y轴的上升沿,Td:Y轴下降沿
u8 CaptureNumber = 0;
u16 Capture = 0;
u8 flag = 0;
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//TIM3捕获通道3(X轴),4(Y轴)引脚设置为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 49999; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 溢出一次25ms
TIM_TimeBaseStructure.TIM_Prescaler =36-1; //设置用来作为TIMx时钟频率除数的预分频值 72分频 记一次数0.5us
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/* Enables the Update event for TIM3 */
// TIM_UpdateDisableConfig(TIM4,ENABLE); //使能 TIM4 更新事件
/*配置输入捕获通道3-PB0(X)*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; //通道选择
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //下降沿触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //管脚与寄存器对应关系
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入预分频。意思是控制在多少个输入周期做一次捕获,如果
//输入的信号频率没有变,测得的周期也不会变。比如选择4分频,则每四个输入周期才做一次捕获,这样在输入信号变化不频繁的情况下,
//可以减少软件被不断中断的次数。
TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
TIM_ICInit(TIM3, &TIM_ICInitStructure); //根据参数配置TIM外设信息
/*配置输入捕获通道4-PB1(Y)*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //通道选择
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //管脚与寄存器对应关系
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入预分频。意思是控制在多少个输入周期做一次捕获,如果
//输入的信号频率没有变,测得的周期也不会变。比如选择4分频,则每四个输入周期才做一次捕获,这样在输入信号变化不频繁的情况下,
//可以减少软件被不断中断的次数。
TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
TIM_ICInit(TIM3, &TIM_ICInitStructure); //根据参数配置TIM外设信息
/* 使能TIM3 */
TIM_ClearITPendingBit(TIM3,TIM_IT_CC3|TIM_IT_CC4|TIM_IT_Update); //清溢出中断
TIM_ITConfig(TIM3,TIM_IT_CC3|TIM_IT_CC4,DISABLE);
TIM_Cmd(TIM3, DISABLE);
}
void Read_XYmg(void)
{
// u16 Ta,Tb,Tc,Td; //Ta:X轴上升开始计数,Tb:X轴下降沿结束计数;Tc:Y轴的上升沿,Td:Y轴下降沿
float T1x,T1y,T2;
float RadianX,RadianY;
TIM3->CNT = 0; //计数器清零
TIM3->CCR3 = 0;
TIM3->CCR4 = 0;
flag = 0;
TIM3->DIER |=3<<3;
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 1);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0);
TIM3->CR1 |= 1<<0; //enable TIM
while(!flag); //等待Tb,Tc,Td采样完成
T1x = (float)Tb;
T1y = (float)(Td-Tc);
T2 = (Td-((Td-Tc)/2))-(Tb/2); //T2 = [Td – ((Td – Tc)/2)] – [Tb/2]
Xmg = ((T1x/T2)-500)*8.0; //数据手册中公式推导
Ymg = ((T1y/T2)-500)*8.0;
RadianX = asin(Xmg*0.001); //XY弧度制
RadianY = asin(Ymg*0.001);
AngleX = TILT(RadianX); //XY角度值
AngleY = TILT(RadianY);
printf("%f\n",T1x);
}
void TIM3_IRQHandler(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
if(TIM_GetITStatus(TIM3,TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);
Tb = TIM3->CCR3;
TIM3->CNT =0;
TIM3->DIER &= ~(1<<3);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);
if(CaptureNumber == 0)
{
Tc = TIM3->CCR4;
CaptureNumber = 1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //下降沿触发
TIM_ICInit(TIM3, &TIM_ICInitStructure);
}
else if(CaptureNumber == 1)
{
Td = TIM3->CCR4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
TIM_ICInit(TIM3, &TIM_ICInitStructure);
if (Td > Tc)
{
Capture = (Td - Tc);
}
else
{
Capture = ((49999 - Tc) + Td);
}
CaptureNumber = 0;
TIM3->CNT =0;
flag = 1;
TIM3->DIER &= ~(1<<4);
TIM3->CR1 &= ~(1<<0);
}
}
}
|