1.主函数(main.c)代码部分:
#include "stm32f10x.h" // Device header #include "Delay.h" #include "OLED.h" #include "Encoder.h" int16_t Num; int main(void) { OLED_Init(); OLED_ShowString(1,1,"Num:"); Encoder_Init(); while(1) { Num+=Encoder_Get(); OLED_ShowNum(2,1,Num,4); } }
2.旋转编码器(Encoder.c)代码部分:
#include "stm32f10x.h" // Device header int16_t Encoder_Count; void Encoder_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStruct); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1); EXTI_InitTypeDef EXTI_InitStruct; EXTI_InitStruct.EXTI_Line=EXTI_Line0|EXTI_Line1; EXTI_InitStruct.EXTI_LineCmd=ENABLE; EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling; EXTI_Init(&EXTI_InitStruct); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStruct.NVIC_IRQChannelSubPriority=1; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_Init(&NVIC_InitStruct); NVIC_InitStruct.NVIC_IRQChannel=EXTI1_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStruct.NVIC_IRQChannelSubPriority=2; NVIC_Init(&NVIC_InitStruct); } void EXTI0_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line0)==SET) { if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0) { if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) { Encoder_Count-- ; } } EXTI_ClearITPendingBit(EXTI_Line0); } } void EXTI1_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line1)==SET) { if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) { if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0) { Encoder_Count++ ; } } EXTI_ClearITPendingBit(EXTI_Line0); } } int16_t Encoder_Get(void) { int16_t Temp; Temp = Encoder_Count; Encoder_Count = 0; return Temp; }
3.Encoder.h代码部分:
#ifndef __COUNT_SENSOR_H #define __COUNT_SENSOR_H int16_t Encoder_Get(void); //void EXTI1_IRQHandler(void); //void EXTI0_IRQHandler(void); void Encoder_Init(void); #endif