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初始化的串口不能正常工作,是为什么
时间: 2023-06-24 16:06:31 浏览: 182
drm_encoder_slave.rar_I2C_Slave_Init_encoder_linux i2c slave
这段代码中没有涉及到PA2和PA3,因此不太可能直接影响串口的正常工作。可能原因如下:
1. 可能是其他地方对PA2和PA3进行了配置,导致串口无法正常工作;
2. 可能是串口初始化代码存在问题,需要检查一下是否正确地配置了波特率、数据位、停止位等参数;
3. 可能是硬件问题,需要检查一下PA2和PA3的连接是否正确,是否存在短路等问题。
阅读全文