旋转计数器,b0口有一个坏口,测了半天才发现
This commit is contained in:
66
BSP/Encoder.c
Normal file
66
BSP/Encoder.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "stm32f10x.h"
|
||||
|
||||
int16_t Encoder_Counter = 0;
|
||||
|
||||
void Encoder_Init(void) {
|
||||
// 初始化Gpio口
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
// 初始化exit
|
||||
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
|
||||
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
|
||||
EXTI_InitTypeDef EXTI_InitStructure;
|
||||
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;
|
||||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||||
EXTI_Init(&EXTI_InitStructure);
|
||||
|
||||
// 初始化nvic
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
|
||||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
int16_t GetEncoder_Counter(void) {
|
||||
int16_t temp = 0;
|
||||
temp = Encoder_Counter;
|
||||
Encoder_Counter = 0;
|
||||
return temp;
|
||||
}
|
||||
|
||||
void EXTI0_IRQHandler(void) {
|
||||
if (EXTI_GetITStatus(EXTI_Line0) == SET) {
|
||||
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) {
|
||||
Encoder_Counter--;
|
||||
}
|
||||
EXTI_ClearITPendingBit(EXTI_Line0);
|
||||
}
|
||||
}
|
||||
|
||||
void EXTI1_IRQHandler(void) {
|
||||
if (EXTI_GetITStatus(EXTI_Line1) == SET) {
|
||||
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0) {
|
||||
Encoder_Counter++;
|
||||
}
|
||||
EXTI_ClearITPendingBit(EXTI_Line1);
|
||||
}
|
||||
}
|
||||
8
BSP/Encoder.h
Normal file
8
BSP/Encoder.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef __ENCODER_H
|
||||
#define __ENCODER_H
|
||||
|
||||
void Encoder_Init(void) ;
|
||||
int16_t GetEncoder_Counter(void) ;
|
||||
void EXTI0_IRQHandler(void) ;
|
||||
void EXTI1_IRQHandler(void);
|
||||
#endif
|
||||
@@ -41,6 +41,8 @@ add_executable(${CMAKE_PROJECT_NAME}
|
||||
BSP/OLED_Font.h
|
||||
BSP/CountSensor.c
|
||||
BSP/CountSensor.h
|
||||
BSP/Encoder.c
|
||||
BSP/Encoder.h
|
||||
)
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
||||
|
||||
16
User/main.c
16
User/main.c
@@ -1,13 +1,21 @@
|
||||
#include "stm32f10x.h"
|
||||
#include "Delay.h"
|
||||
#include "OLED.h"
|
||||
#include "CountSensor.h"
|
||||
#include "Encoder.h"
|
||||
|
||||
int16_t Num = 0;
|
||||
|
||||
int main(void) {
|
||||
OLED_Init();
|
||||
CountSensor_Init();
|
||||
OLED_ShowString(1,1, "Count:");
|
||||
Encoder_Init();
|
||||
OLED_ShowString(1, 1, "Num:");
|
||||
|
||||
|
||||
while (1) {
|
||||
OLED_ShowNum(1,7, CountSensor_GetCount(), 5);
|
||||
OLED_ShowNum(2, 1, GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0), 1); // 显示 PB0 电平
|
||||
OLED_ShowNum(3, 1, GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1), 1); // 显示 PB1 电平
|
||||
|
||||
Num += GetEncoder_Counter();
|
||||
OLED_ShowSignedNum(1, 5, Num, 5);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user