定时器配置:
#include "
timer.h"
#include "led.h"
#include "
STM32f10x.h"
//通用定时器中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器3
void Timerx_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
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的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update |TIM_IT_Trigger,ENABLE);
TIM_ARRPreloadConfig(TIM3,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
// TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
}
void TIM3_IRQHandler(void) //TIM2中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
}
}
超声波模块配置:
#include "UltrasonicWave.h"
#include "usart.h"
#include "delay.h"
#define TRIG_PORT GPIOC //TRIG
#define ECHO_PORT GPIOA //ECHO
#define TRIG_PIN GPIO_Pin_8 //TRIG
#define ECHO_PIN GPIO_Pin_7 //ECHO
float UltrasonicWave_Distance; //计算出的距离
/*
* 函数名:UltrasonicWave_Configuration
* 描述 :超声波模块的初始化
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //关闭jtag
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = TRIG_PIN; //PC8接TRIG
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设为推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //初始化外设GPIO
GPIO_InitStructure.GPIO_Pin = ECHO_PIN; //PC7接ECH0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设为输入
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //初始化GPIOA
//GPIOC.7 中断线以及中断初始化配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource7);
EXTI_InitStructure.EXTI_Line=EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}
void EXTI9_5_IRQHandler(void)
{
delay_us(60); //延时10us
if(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3, ENABLE); //开启时钟
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待低电平
TIM_Cmd(TIM3, DISABLE); //定时器2失能
delay_ms(60);
UltrasonicWave_Distance=TIM_GetCounter(TIM3)*5*34/200.0; //计算距离&&UltrasonicWave_Distance<150
// TIM_Cmd(TIM3, DISABLE);
if(UltrasonicWave_Distance>0)
{
printf("distance:%f cm
",UltrasonicWave_Distance);
}
EXTI_ClearITPendingBit(EXTI_Line7); //清除EXTI7线路挂起位
}
else while(EXTI_GetITStatus(EXTI_Line7) == RESET);
}
/*
* 函数名:UltrasonicWave_StartMeasure
* 描述 :开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN); //送>10US的高电平RIG_PORT,TRIG_PIN这两个在define中有?
delay_us(20); //延时20US
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
}
主函数:
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "exti.h"
#include "timer.h"
#include "UltrasonicWave.h"
int main(void)
{
// SystemInit();
delay_init(); //延时初始化
// NVIC_Configuration();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
uart_init(115200); //串口初始化
LED_Init(); //LED端口初始化
Timerx_Init(4999,7199); //10Khz的计数频率,计数到5000为500ms
UltrasonicWave_Configuration(); //对超声波模块初始化
while(1)
{
UltrasonicWave_StartMeasure();
//开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
// LED0=!LED0;
// delay_ms(1000);
}
Distance是个公式,用了340m/s换成cm算了的,其实主要问题是打印出来数据不变,至于时间的话我用TIM计时了,公式中用了个函数就是读取TIM时间的函数,我在TIM输出为高时开启定时器,为低时关闭定时器,就是把这段时间给读取出来了,这就是算距离的公式吧!
一周热门 更多>