用的芯片是stm32f103c8t6
#include "encoder.h"
#include "usart.h"
#include "delay.h"
uint8_t TIM3CH3_CAPTURE_STA=0; //输入捕获状态
uint16_t TIM3CH3_CAPTURE_VAL; //输入捕获值
int distance = 0;
void Encoder_Init_TIM3(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM3_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能定时器3的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //端口配置 PA6 PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOA
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //PB8 下拉
//初始化定时器4 TIM3
TIM_TimeBaseStructure.TIM_Period = 0XFFFF; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =72-1; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM2输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM3,ENABLE ); //使能定时器4
}
void TIM3_IRQHandler(void)
{
if((TIM3CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM3CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
TIM3CH3_CAPTURE_VAL=0XFFFF;
}else TIM3CH3_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//捕获1发生捕获事件
{
if(TIM3CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM3CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
TIM3CH3_CAPTURE_VAL=TIM_GetCapture3(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM3CH3_CAPTURE_STA=0; //清空
TIM3CH3_CAPTURE_VAL=0;
TIM_SetCounter(TIM3,0);
TIM3CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3|TIM_IT_Update); //清除中断标志位
}
void Count_Dist(void)
{
delay_init(); //在 delay.h 中
TRIG_Init();
GPIO_SetBits(GPIOB, GPIO_Pin_12);
delay_us(15);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
if(TIM3CH3_CAPTURE_STA&0X80)//成功捕获到了一次高电平
{
distance=TIM3CH3_CAPTURE_STA&0X3F;
distance*=65536; //溢出时间总和
distance+=TIM3CH3_CAPTURE_VAL; //得到总的高电平时间
distance=distance*170*0.0001;
printf("distance:%d cm\r\n",distance); //打印总的高点平时间
TIM3CH3_CAPTURE_STA=0; //开启下一次捕获
}
}
|