当前位置:网站首页>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 );
}
}
边栏推荐
- buuresevewp
- Click the button to call the system browser to open Baidu home page
- Chapter VIII (classes and objects)
- See which processes occupy specific ports and shut down
- (vs2019 MFC connects to MySQL) make a simple login interface (detailed)
- Win11 open folder Caton solution summary
- Wifi-802.11 2.4G band 5g band channel frequency allocation table
- 我想要股票账户优惠开户,如何操作?手机开户安全么?
- Use intent to shuttle between activities -- use explicit intent
- optee中支持的时间函数
猜你喜欢

Double linked list -- tail interpolation construction (C language)

(vs2019 MFC connects to MySQL) make a simple login interface (detailed)

MySQL practice: 3 Table operation
GHUnit: Unit Testing Objective-C for the iPhone

Chapter VIII (classes and objects)

JS precompile - Variable - scope - closure

Chapter VI (pointer)

MySQL practice: 1 Common database commands

Deeply analyze storage costs and find cost reduction solutions

Multi interface switching in one UI of QT
随机推荐
Calculation of decoupling capacitance
xxl-job配置告警邮件通知
4 best practices for wireless (OTA) updates
Idea auto Guide
Vs2019-mfc setting edit control and static text font size
Understanding of closures
教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning
ReW_ p
ReW_p
Golang collaboration and channel usage
How to Use Instruments in Xcode
[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc
Real machine debugging of uniapp custom base
Idea automatically sets author information and date
Data governance: from top project to data culture!
我想造SQL数据(存储结构)
Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor
Power apps application practice | easily develop employee leave attendance management applet with power apps
The difference between push-pull circuit drive and totem pole drive