写了一个通过定时器3测速测距的东西。但是总是无法调试成功,哪位大哥帮忙看一下
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "stm32f10x_tim.h"
#include "motor.h"
#include "PWM.h"
#include "timer.h"
int main(void)
{
u16 t;
u16 len;
u16 times=0;
u8 a[200];
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为115200
TIM4_PWM_Init(899,0);
Motor_12_Config(); //298电机驱动初始化
TIM3_Cap_Init(0XFFFF,72-1) ; //1秒计数1000000次
IN1(Low);
IN2(High);
IN3(Low);
IN4(High);//保持控制正反转电平恒定默认直行
while(1)
{
Motor_1_STOP();
Motor_2_STOP();
if(USART_RX_STA&0x8000)
{
a[200]=0;
len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
printf("
您发送的消息为:
");
for(t=0;t<len;t++)
{ a[t]= USART_RX_BUF[t];
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
printf("
");//插入换行
USART_RX_STA=0;
if(a[0]=='0')
{
Motor_1_STOP();
Motor_2_STOP();
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
if(a[0]=='1')
{
Motor_1_PRun();
Motor_2_PRun();
delay_ms(1000);
}
if(a[0]=='2')
{
Motor_left();
delay_ms(500);
}
if(a[0]=='3')
{
Motor_right();
delay_ms(500);
}
if(a[0]=='4')
{
Motor_1_NRun();
Motor_2_NRun();
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
}
else
{
times++;
if(times%5000==0)
{
printf("杨某真帅
");
}
delay_ms(10);
}
#include "timer.h"
#include "led.h"
#include "usart.h"
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM5时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;; //PA0 输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //PA0 下拉
//初始化定时器5 TIM5
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM5输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI3上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
printf("1");
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
printf("2");
TIM_Cmd(TIM3,ENABLE); //使能定时器5
}
double speed,length,temp;
extern u16 TimeValue=0;
extern u16 TimeCount=0;
extern int count=0;
void TIM3_IRQn_Handler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TimeCount++;
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//捕获1发生捕获事件
{
if(count<12)
{
count++;
}
else
{
temp=TIM_GetCapture3(TIM3);
temp=temp+TimeCount*65536;
temp/=1000000;
speed=0.06*3.14/temp;
length+=0.06*3.14;
printf("length: %lf
",length);
printf("speed: %lf
",speed);
TimeCount=0;
count=0;
temp=0;
TIM_SetCounter(TIM3,0);
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3|TIM_IT_Update); //清除中断标志位
}
具体表现为一旦打开TIM3程序就跑飞了,怀疑是中断函数有问题,困扰我一个月了,搭嘎帮忙看看。
一周热门 更多>