int16_t Encoder_Num; void Encoder_Init (void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_InitTypeDef GPIO_Initstructure; GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1; GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_Initstructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0|GPIO_PinSource1); EXTI_InitTypeDef EXTI_Initstructure; EXTI_Initstructure.EXTI_Line=EXTI_Line0|EXTI_Line1; EXTI_Initstructure.EXTI_LineCmd=ENABLE; EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling; EXTI_Init(&EXTI_Initstructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_Initstructure; NVIC_Initstructure.NVIC_IRQChannel=EXTI0_IRQn; NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=1; NVIC_Initstructure.NVIC_IRQChannelSubPriority=1; NVIC_Init(&NVIC_Initstructure); NVIC_Initstructure.NVIC_IRQChannel=EXTI1_IRQn; NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_Initstructure.NVIC_IRQChannelSubPriority=2; NVIC_Init(&NVIC_Initstructure); } int Encoder_get() { int16_t temp; temp=Encoder_Num; Encoder_Num=0; return temp; }void EXTI0_IRQHandler () { if (EXTI_GetITStatus(EXTI_Line0)==SET) { if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0) { if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==1) { Encoder_Num--; } else { Encoder_Num++; } } EXTI_ClearITPendingBit(EXTI_Line0); } }
时间: 2023-09-06 18:08:57 浏览: 66
根据您提供的代码,有几个可能导致中断不触发的问题:
1. 外部中断线未正确配置:根据您的代码,外部中断线0已经正确配置为中断模式并连接到GPIOB的引脚0。但是,您需要确保外部中断线1也正确连接到GPIOB的引脚1,并进行相应的配置。
2. 中断优先级设置不正确:根据代码,中断优先级已经设置为EXTI0_IRQn的优先级为1,EXTI1_IRQn的优先级为2。但是,您需要确保这些优先级高于其他可能存在的中断。
3. 中断触发方式设置不正确:根据代码,中断触发方式被配置为下降沿触发。如果您期望使用上升沿触发或其他触发方式,请相应地调整`EXTI_Initstructure.EXTI_Trigger`参数。
请确保以上问题得到正确解决,并检查硬件连接是否正确。如果问题仍然存在,请提供更多关于硬件环境和其他相关代码的信息,以便更好地帮助您解决问题。
相关问题
void Encoder_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 1000 - 1; TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); TIM_Cmd(TIM1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure1; GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure1); GPIO_InitTypeDef GPIO_InitStructure2; GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStructure2.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13; GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure2); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource10); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource12); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource13); EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line = EXTI_Line0|EXTI_Line1|EXTI_Line4|EXTI_Line5|EXTI_Line10|EXTI_Line11|EXTI_Line12|EXTI_Line13; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_Init(&EXTI_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStruct1; NVIC_InitStruct1.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStruct1.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct1.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStruct1.NVIC_IRQChannelSubPriority = 0x00; NVIC_Init(&NVIC_InitStruct1); NVIC_InitTypeDef NVIC_InitStruct2; NVIC_InitStruct2.NVIC_IRQChannel = EXTI1_IRQn; NVIC_InitStruct2.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct2.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStruct2.NVIC_IRQChannelSubPriority = 0x00; NVIC_Init(&NVIC_InitStruct2); NVIC_InitTypeDef NVIC_InitStruct3; NVIC_InitStruct3.NVIC_IRQChannel = EXTI4_IRQn; NVIC_InitStruct3.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct3.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStruct3.NVIC_IRQChannelSubPriority = 0x00; NVIC_Init(&NVIC_InitStruct3); NVIC_InitTypeDef NVIC_InitStruct4; NVIC_InitStruct4.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStruct4.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct4.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStruct4.NVIC_IRQChannelSubPriority = 0x00; NVIC_Init(&NVIC_InitStruct4); NVIC_InitTypeDef NVIC_InitStruct5; NVIC_InitStruct5.NVIC_IRQChannel = EXTI15_10_IRQn; NVIC_InitStruct5.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct5.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStruct5.NVIC_IRQChannelSubPriority = 0x00; NVIC_Init(&NVIC_InitStruct5); }stm32f103系列芯片,当这段代码初始化时,以PA2PA3初始化的串口不能正常工作,是为什么
这段代码中没有涉及到PA2和PA3,因此不太可能直接影响串口的正常工作。可能原因如下:
1. 可能是其他地方对PA2和PA3进行了配置,导致串口无法正常工作;
2. 可能是串口初始化代码存在问题,需要检查一下是否正确地配置了波特率、数据位、停止位等参数;
3. 可能是硬件问题,需要检查一下PA2和PA3的连接是否正确,是否存在短路等问题。
用stm32写一段控制520编码器电机转动以及测量转速与位置的代码
你好,这是一个技术问题,我可以回答。以下是一个简单的示例代码:
#include "stm32f4xx.h"
#define ENCODER_TIM TIM2
#define ENCODER_TIM_CLK RCC_APB1Periph_TIM2
#define ENCODER_TIM_IRQn TIM2_IRQn
#define ENCODER_A_GPIO GPIOA
#define ENCODER_A_PIN GPIO_Pin_0
#define ENCODER_A_CLK RCC_AHB1Periph_GPIOA
#define ENCODER_A_SOURCE GPIO_PinSource0
#define ENCODER_A_AF GPIO_AF_TIM2
#define ENCODER_B_GPIO GPIOA
#define ENCODER_B_PIN GPIO_Pin_1
#define ENCODER_B_CLK RCC_AHB1Periph_GPIOA
#define ENCODER_B_SOURCE GPIO_PinSource1
#define ENCODER_B_AF GPIO_AF_TIM2
#define MOTOR_GPIO GPIOB
#define MOTOR_PIN GPIO_Pin_0
#define MOTOR_TIM TIM3
#define MOTOR_TIM_CLK RCC_APB1Periph_TIM3
#define MOTOR_PWM_CHANNEL TIM_OCMode_PWM1
#define MOTOR_PWM_GPIO GPIOB
#define MOTOR_PWM_PIN GPIO_Pin_1
#define MOTOR_PWM_CLK RCC_AHB1Periph_GPIOB
#define MOTOR_PWM_SOURCE GPIO_PinSource1
#define MOTOR_PWM_AF GPIO_AF_TIM3
#define MOTOR_PWM_PERIOD 1000
volatile uint32_t encoder_count = 0;
void ENCODER_TIM_IRQHandler(void)
{
if (TIM_GetITStatus(ENCODER_TIM, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(ENCODER_TIM, TIM_IT_Update);
encoder_count++;
}
}
void encoder_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(ENCODER_A_CLK | ENCODER_B_CLK, ENABLE);
RCC_APB1PeriphClockCmd(ENCODER_TIM_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ENCODER_A_PIN | ENCODER_B_PIN;
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_UP;
GPIO_Init(ENCODER_A_GPIO, &GPIO_InitStructure);
GPIO_PinAFConfig(ENCODER_A_GPIO, ENCODER_A_SOURCE, ENCODER_A_AF);
GPIO_PinAFConfig(ENCODER_B_GPIO, ENCODER_B_SOURCE, ENCODER_B_AF);
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)