一.对射式红外传感器计次
1.原理
2.硬件连接
3.程序
CountSensor.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"uint16_t CountSensor_Count;void CountSensor_Init(void)
{//配置RCC时钟:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//EXTI和NVIC时钟一直是打开的//配置GPIO,端口为输入模式:GPIO_InitTypeDef GPIO_InitStruture;GPIO_InitStruture.GPIO_Mode = GPIO_Mode_IPU;//上拉输入,可参考手册建议选择GPIO_InitStruture.GPIO_Pin = GPIO_Pin_14;GPIO_InitStruture.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStruture);//配置AFIO,选择用上一步的GPIO,连接到后面的EXTI:GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);//配置AFIO的数据选择器//将PB14号引脚的电平信号通过AFIO进入到EXTI//配置EXTI(选择边沿触发方式,选择触发响应方式):EXTI_InitTypeDef EXTI_InitStructure;EXTI_InitStructure.EXTI_Line = EXTI_Line14;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发EXTI_Init(&EXTI_InitStructure);//配置NVIC(选择优先级):NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2 bits for pre-emption priority,2 bits for subpriority//pre-emption抢占优先级 subpriority响应优先级NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStructure);
}uint16_t CountSensor_Get(void)
{return CountSensor_Count;
}void EXTI15_10_IRQHandler(void)//中断函数
{if(EXTI_GetITStatus(EXTI_Line14)==SET)//进行中断标志位的判断{Delay_ms(300);//消抖//开始中断函数:CountSensor_Count ++;EXTI_ClearITPendingBit(EXTI_Line14);//将中断标志位清除,以防无限中断}
}
CountSensor.h:
#ifndef __COUNT_SENSOR_H
#define __COUNT_SENSOR_Hvoid CountSensor_Init(void);
uint16_t CountSensor_Get(void);#endif
main.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"int main(void)
{OLED_Init();CountSensor_Init();OLED_ShowString(1,1,"Count:");//显示字符串while(1){OLED_ShowNum(1,7,CountSensor_Get(),5);}
}
二.旋转编码计次
1.硬件连接
2.程序
Encoder.c:
#include "stm32f10x.h" // Device headerint16_t Encoder_Count;void Encoder_Init(void)
{//配置RCC时钟:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//EXTI和NVIC时钟一直是打开的//配置GPIO,端口为输入模式:GPIO_InitTypeDef GPIO_InitStruture;GPIO_InitStruture.GPIO_Mode = GPIO_Mode_IPU;//上拉输入,可参考手册建议选择GPIO_InitStruture.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;GPIO_InitStruture.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStruture);//配置AFIO,选择用上一步的GPIO,连接到后面的EXTI:GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);//配置AFIO的数据选择器//将PB0号引脚的电平信号通过AFIO进入到EXTIGPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);//配置AFIO的数据选择器//将PB1号引脚的电平信号通过AFIO进入到EXTI//配置EXTI(选择边沿触发方式,选择触发响应方式):EXTI_InitTypeDef EXTI_InitStructure;EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发EXTI_Init(&EXTI_InitStructure);//配置NVIC(选择优先级):NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2 bits for pre-emption priority,2 bits for subpriority//pre-emption抢占优先级 subpriority响应优先级NVIC_InitTypeDef NVIC_InitStructure;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 Encoder_Get(void)
{int16_t Temp;Temp = Encoder_Count;Encoder_Count = 0;return Temp;
}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_Line1);}
}
Encoder.h:
#ifndef __ENCODER_H
#define __ENCODER_H
void Encoder_Init(void);
int16_t Encoder_Get(void);
#endif
main.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Encoder.h"int16_t Num;int main(void)
{OLED_Init();Encoder_Init();OLED_ShowString(1,1,"Num:");//显示字符串while(1){Num += Encoder_Get();OLED_ShowSignedNum(1,5,Num,5);}
}
四.调试
出现了两个问题:
1.旋转无反应,调试发现因为有的面包板中间不相连,所以要用跳线相连。
2.旋转太慢时记不上数,最后在中断代码那里加了一个if语句防抖