代码如下,知道原因的麻烦告知下,谢谢。
- #include "QuadratureEncoder.h"
- u16 left_count=0; //×ó±àÂëÆ÷¼Æêy
- u16 right_count=0; //óò±àÂëÆ÷¼Æêy
- //×óÕy½»±àÂëÆ÷£¬AB½öóÃò»Ïî2aËù
- void Encoder_L_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //éèÖÃÎa¸¡¿Õêäèë
- GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉ
-
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); //PB.0éèÖÃÎaía2¿ÖD¶Ï
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line0; //ÖD¶ÏÏß0
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //ÖD¶ÏÏìó|
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //éÏéyÑØ′¥·¢
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //ê1ÄüÖD¶ÏÏß
- EXTI_Init(&EXTI_InitStructure); //3õê¼»ˉ
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃÖD¶Ï·Ö×é2£o2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ía2¿ÖD¶Ï0
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇàÕ¼óÅÏ輶3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Ïìó|óÅÏ輶2
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ía2¿ÖD¶Ï0ê1Äü
- NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉ
- }
- //óòÕy½»±àÂëÆ÷£¬AB½öóÃò»Ïî2aËù
- void Encoder_R_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //éèÖÃÎa¸¡¿Õêäèë
- GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉ
-
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); //PB.1éèÖÃÎaía2¿ÖD¶Ï
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line1; //ÖD¶ÏÏß1
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //ÖD¶ÏÏìó|
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //éÏéyÑØ′¥·¢
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //ê1ÄüÖD¶ÏÏß
- EXTI_Init(&EXTI_InitStructure); //3õê¼»ˉ
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃÖD¶Ï·Ö×é2£o2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //ía2¿ÖD¶Ï1
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇàÕ¼óÅÏ輶3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Ïìó|óÅÏ輶2
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ía2¿ÖD¶Ï0ê1Äü
- NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉ
- }
- //Õy½»±àÂëÆ÷íaéè3õê¼»ˉ
- void Encoder_Init(void)
- {
- Encoder_L_Init();
- Encoder_R_Init();
- }
- //ÖD¶Ï·tÎñ3ìDò0
- void EXTI0_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //ÅD¶ÏLINE0éÏμÄÖD¶ÏêÇ·ñ·¢éú
- {
- left_count++;
- EXTI_ClearITPendingBit(EXTI_Line0); //Çå3yLINE0éÏμÄÖD¶Ï±ê־λ
- }
- }
- //ÖD¶Ï·tÎñ3ìDò1
- void EXTI1_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_Line1)!=RESET) //ÅD¶ÏLINE1éÏμÄÖD¶ÏêÇ·ñ·¢éú
- {
- right_count++;
- EXTI_ClearITPendingBit(EXTI_Line1); //Çå3yLINE1éÏμÄÖD¶Ï±ê־λ
- }
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>