旋转编码器计次
1,创建 .C .H 文件
Count_Sonser.c
Count_Sonser.h
2,包含头文件
#include "ch32v30x.h"
//#include "debug.h"
#include "Count_Sonser.h"
注意:如果不包含第一第二个头文件uint、int这些定义就会报错。
3,配置CountSonser_Init()相关
/*********************************************************************
1,开启GPIO时钟、AFIO时钟
2,初始化GPIO
3,AFIO复用GPIO到EXTI
-->GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
4,初始化EXTI
5,给NVIC分组,一个工程只能分一次组。
-->NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
6,初始化NVIC
7,声明中断通道是一个中断 -->void EXTI0_IRQHandler(void)__attribute__((interrupt("WCH-Interrupt-fast")));
*/
#include "ch32v30x.h"
#include "Count_Sonser.h"
#include "debug.h"/*********************************************************************
1,开启GPIO时钟、AFIO时钟
2,初始化GPIO
3,AFIO复用GPIO到EXTI -->GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
4,初始化EXTI
5,给NVIC分组 -->NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
6,初始化NVIC
7,声明中断通道是一个中断 -->void EXTI0_IRQHandler(void)__attribute__((interrupt("WCH-Interrupt-fast")));
*/int16_t Get_Sonser ;
void CountSonser_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//开启AFIO、GPIO时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);GPIO_InitTypeDef GPIO_InitStruct = {0};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); //AFIO复用EXTI-->EXTI_Lin0;GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1); //AFIO复用EXTI-->EXTI_Lin1;EXTI_InitTypeDef EXTI_InitStruct = {0};EXTI_InitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line1; //通道0和通道1EXTI_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); //中断分组2,先占优先级和响应优先级都配置为2NVIC_InitTypeDef NVIC_InitStruct = {0};NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; //EXTI中断0NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级1NVIC_Init(&NVIC_InitStruct);NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn; //EXTI中断1NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级2NVIC_Init(&NVIC_InitStruct);
}
//返回Get_Sonser的值
int16_t GetSonSer(void)
{int16_t temp ;temp = Get_Sonser;Get_Sonser = 0;return temp;
}
/** 声明这是一个中断,("WCH-Interrupt-fast")说明是个高速中断* 这句不能少* */
void EXTI0_IRQHandler(void)__attribute__((interrupt("WCH-Interrupt-fast")));
void EXTI0_IRQHandler(void)
{if(EXTI_GetITStatus(EXTI_Line0) == SET) //判断中断挂起标志位{if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == Bit_RESET){if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == Bit_RESET){Get_Sonser--;}}}EXTI_ClearITPendingBit(EXTI_Line0); //清除中断挂起标志位
}void EXTI1_IRQHandler(void)__attribute__((interrupt("WCH-Interrupt-fast")));//声明这是一个中断,("WCH-Interrupt-fast")说明是个高速中断
void EXTI1_IRQHandler(void)
{if(EXTI_GetITStatus(EXTI_Line1) == SET){if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == Bit_RESET){if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == Bit_RESET){Get_Sonser++;}}}EXTI_ClearITPendingBit(EXTI_Line1);
}
4,实验现象