新手入门
- 积分
- 4
- 金钱
- 4
- 注册时间
- 2021-8-4
- 在线时间
- 0 小时
|
1金钱
在用stm32c8t6 做智能车的时候 在做测速模块发现 当只有一个轮子转动时 另一个轮子的还是有速度,且两轮测出来的速度一样
去查寄存器发现竟然清除不掉两个中断标志位,导致中断一直在执行 !!!弄了好久还是不知道怎么回事? 求大佬告知

测试如图

运行完两行代码后依旧还是1????

附上代码部分
speedtest.c
#include "stm32f10x.h"
#include "speedtest.h"
#include "sys.h"
#include "beep.h"
u16 speedcount_L=0;
u16 speedcount_R=0;
u16 speed_Right=0;
u16 speed_Left=0;
void Speed_Test_Init(u16 arr,u16 psc)
{ //外部中断配置
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
BEEP_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2|GPIO_PinSource1); //ÖD¶Ïó3éä
EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line1;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//TIM2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//¿aÆôTIM2ê±Öó
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_Cmd(TIM2, DISABLE);
EXTI->IMR&=~(6);
speed_Left=speedcount_L*60/20; // r/min
speed_Right=speedcount_R*60/20; // r/min
speedcount_L=0;
speedcount_R=0;
if(EXTI_GetITStatus(EXTI_Line2)==SET) EXTI_ClearITPendingBit(EXTI_Line2); //清除不掉
if(EXTI_GetITStatus(EXTI_Line1)==SET) EXTI_ClearITPendingBit(EXTI_Line1);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_Cmd(TIM2, ENABLE);
EXTI->IMR|=6;
TIM_SetCounter(TIM2,0x00);
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)==SET)
{
speedcount_L++;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1)==SET)
{
speedcount_R++;
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
main.c
#include "stm32f10x.h"
#include "carwheel.h"
#include "redfollow.h"
#include "speedtest.h"
#include "beep.h"
#include "usart_speed.h"
#include "sys.h"
#define speed_L 400
#define speed_R 400
int main(void)
{
SystemInit();
WHEEL_Init();
Red_Follow_Init(); //红外循迹模块 使用if函数没用中断
uart_init(115200);
Speed_Test_Init(9999,7199); //1s 检测一次速度
while(1)
{
Red_Follow(speed_L,speed_R);
Usart1_Speed();
// if(speed_R >=4) BEEP=!BEEP;
}
}
|
|