当前位置:网站首页>STM32 encountered problems using encoder module (library function version)

STM32 encountered problems using encoder module (library function version)

2022-06-26 08:16:00 Wu He


```c
void Interrupt_Init(void)
{	GPIO_InitTypeDef GPIO_InitStructure;// Initial structure of the pin 
	EXTI_InitTypeDef EXTI_InitStructure;//EXTI Structure 
	NVIC_InitTypeDef NVIC_InitStructure;//NVIC Structure 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// Turn on GPIOA The clock of 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);// Turn on AFIO The clock 
	// Be careful RCC_APB2PeriphClockCmd() The inside needs to correspond to the inside 
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);// Select fracture selection GPIOA0
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource4);// Select fracture selection GPIOA4
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line4|EXTI_Line0;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;// External interrupt 
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;// Operation mode 
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;//EXTI0 Interrupt channel 
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ Channel enable 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;// preemption 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;// Sub priority 
	NVIC_Init(&NVIC_InitStructure);// Initialize... According to the specified parameters VIC register 
	
	NVIC_InitStructure.NVIC_IRQChannel =EXTI4_IRQn;//EXTI4 Interrupt channel 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;// preemption 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;		// Sub priority 
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ Channel enable 
	NVIC_Init(&NVIC_InitStructure);	// Initialize... According to the specified parameters VIC register 
}
``uint16_t CountSensor_Get(void)
{
	return CountSensor_Count0;
}

// Note the name of the interrupt function EXTIx_IRQHandler
void EXTI0_IRQHandler()
{
	
	if(EXTI_GetITStatus(EXTI_Line0)==1)
	{
		
		
			
			//if(EXTI_GetITStatus(EXTI_Line0)==1)
			**if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))**// When the two levels are equal , It corresponds to clockwise rotation . Unequal vice versa 
			{
				CountSensor_Count0++;
			}
			else
			{
				CountSensor_Count0--;
			}
			
	}
	while(!EXTI_GetITStatus(EXTI_Line0));
	EXTI_ClearITPendingBit(EXTI_Line0);
}

void EXTI4_IRQHandler()
{
	
	if(EXTI_GetITStatus(EXTI_Line4)==1)
	{
		
		
			
			if(EXTI_GetITStatus(EXTI_Line4)==1)
			
			{
				//sw=~sw;
			}
			
			
	}
	EXTI_ClearITPendingBit(EXTI_Line4);
}
/*
 Encoder A Phase and B Phase output low level : Clockwise 
 Encoder A Phase output low level and B Phase output high level : Anti-clockwise 
 Be careful : When you enable the interrupt service, you must write code in the interrupt service function , Otherwise, the code will get stuck ( You can be free , Also write )
 Be careful : It is better to use double edge trigger mode for encoder module interrupt trigger 
*/
 We can choose the interrupt method or the query method , We choose according to our own needs `
 Insert a code chip here `void encodeing_scan(void)
{
	if((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))==0)
	{	
		if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0)// Positive rotation 
		{
			CountSensor_Count0++;
		}
		else 
		{
			CountSensor_Count0--;
		}
		while((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))==0 );
	}
}
原网站

版权声明
本文为[Wu He]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170600536050.html