当前位置:网站首页>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 );
}
}
边栏推荐
- Chapter VIII (classes and objects)
- Go language shallow copy and deep copy
- JS precompile - Variable - scope - closure
- Chapter VI (pointer)
- Which securities company has the lowest Commission for opening a mobile account? Is it safe to open an account online?
- Blue Bridge Cup 3 sequence summation
- The 9th zero code training camp is officially open for registration! (Beijing, Shanghai, Guangzhou and Shenzhen)
- Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning
- Comparison version number [leetcode]
- What is the five levels of cultivation of MES management system
猜你喜欢
Livevideostackcon | evolution of streaming media distribution for online education business
GHUnit: Unit Testing Objective-C for the iPhone
Multi interface switching in one UI of QT
Uniapp uses uviewui
Chapter VI (pointer)
JMeter performance testing - Basic Concepts
Uni app installation and project directory (hbuilder configuration)
Idea auto Guide
(vs2019 MFC connects to MySQL) make a simple login interface (detailed)
Go language shallow copy and deep copy
随机推荐
Junit
Bluebridge cup 1 introduction training Fibonacci series
MySQL insert Chinese error
你为什么会浮躁
Interview JS and browser
Pychart connects to Damon database
MySQL query time period
Listview control
Flume learning notes
Idea update
Diode voltage doubling circuit
Microcontroller from entry to advanced
Chapter VII (structure)
X-VLM多模态模型解读
Chapter 9 (using classes and objects)
How to debug plug-ins using vs Code
xxl-job配置告警邮件通知
buuresevewp
ASP. Net and Net framework and C #
h5 localStorage