当前位置:网站首页>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 );
}
}
边栏推荐
- Orderly arrangement
- Test method - decision table learning
- Baoyan postgraduate entrance examination interview - Network
- Automatic backup of MySQL database in the early morning with Linux
- Double linked list -- tail interpolation construction (C language)
- Uni app is similar to Taobao in selecting multiple specifications of commodities (inventory judgment)
- buuresevewp
- Chapter 9 (using classes and objects)
- Bluebridge cup 1 introduction training Fibonacci series
- Win11 open folder Caton solution summary
猜你喜欢

Chapter VI (pointer)

Go language shallow copy and deep copy

Data governance: from top project to data culture!

Uniapp uses uviewui

Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor

Idea uses regular expressions for global substitution

Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI

Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers

Chapter 5 (array)

Can the encrypted JS code and variable name be cracked and restored?
随机推荐
MFC writes a suggested text editor
Handwritten instanceof underlying principle
CodeBlocks集成Objective-C开发
Pychart connects to Damon database
Wifi-802.11 2.4G band 5g band channel frequency allocation table
Pic 10B parsing
Power apps application practice | easily develop employee leave attendance management applet with power apps
Blue Bridge Cup 3 sequence summation
Uniapp wechat withdrawal (packaged as app)
Google Earth engine (GEE) 02 basic knowledge and learning resources
Automatic backup of MySQL database in the early morning with Linux
Undefined symbols for architecture i386 is related to third-party compiled static libraries
How to Use Instruments in Xcode
Oracle 19C local listener configuration error - no listener
GHUnit: Unit Testing Objective-C for the iPhone
Deeply analyze storage costs and find cost reduction solutions
Idea update
How to Use Instruments in Xcode
教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
The solution of installing opencv with setting in pycharm