完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
2个回答
|
|
#include "UatrasonicHcsr04.H"
#include "delay.h" //宏定义使用UCOS #if SYSTEM_SUPPORT_UCOS #include "includes.h"//ucos 使用 #endif //extern OS_EVENT *sem_Hcsr04; #include "COMVARIABLE.H" /******************************************************************************* * Function Name: Hc_sr04Init * Description : pB6->trig out ;pB7->echo in; * Input : None * Output : None * Return : *******************************************************************************/ void Hc_sr04Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//TIM4 时钟使能 GPIO_InitStructure.GPIO_Pin = Out_Trig_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(HC_SR04_PORT, &GPIO_InitStructure); GPIO_ResetBits(HC_SR04_PORT,Out_Trig_pin); GPIO_InitStructure.GPIO_Pin = In_Echo_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入 GPIO_Init(HC_SR04_PORT, &GPIO_InitStructure); // TIM_TimeBaseStructure.TIM_Period = 9999; //设定计数器自动重装值 最大10ms溢出 TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //预分频器,1M的计数频率,1us加1. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 //根据指定的参数初始化TIMx TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // 选择输入端 IC2映射到TI2上? TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //上升下降捕获 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM_ICInitStructure.TIM_ICFilter = 0x03;//IC4F=0011 配置输入滤波器 8个定时器时钟周期滤波 TIM_ICInit(TIM4, &TIM_ICInitStructure);//初始化定时器输入捕获通道 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级2级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 //allow interrupt source capture TIM_ITConfig(TIM4,TIM_IT_CC2,ENABLE); } /******************************************************************************* * Function Name: TIM2_IRQHandler * Description : * Input : None * Output : None * Return : TIM4_CH2->B7 *******************************************************************************/ u16 distance_tim4; u8 Succeed_EchoFlag = 0; void TIM4_IRQHandler(void) { OSIntEnter(); //中断进入 //capture cc2 channal if(TIM_GetITStatus(TIM4,TIM_IT_CC2) != RESET) { //rising capture enable? if(Echo) //if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)) { TIM_SetCounter(TIM4,0); //clear cnt counter Succeed_EchoFlag = 0; TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling);//CC2P=1 设置为下降沿捕获 } //falling capture enable? else { distance_tim4 = TIM_GetCapture2(TIM4); Succeed_EchoFlag = 1; TIM_Cmd(TIM4,DISABLE);//turn off tim4 counter TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC2P=0设置为上升沿捕获 } } TIM_ClearFlag(TIM4,TIM_IT_CC2); OSIntExit(); //中断退出 } /******************************************************************************* * Function Name: Start_HCSR04 * Description : 开启启动 * Input : None * Output : None * Return : *******************************************************************************/ void Start_HCSR04(void) { Trig = 1; delay_us(25); Trig = 0; //start measure } /******************************************************************************* * Function Name: Hc_sr04Manage * Description : 接收数据处理 * Input : None * Output : None * Return : *******************************************************************************/ u8 buffhcsr[6]; void Hc_sr04Manage(void) { u8 i,err; static u8 flag_unit = 0,flag_send = 0; static u16 data_temp; u16 Data_unit; Start_HCSR04(); TIM_Cmd(TIM4,ENABLE); //start tim4 counter while(Echo == 0); //waite Echo feelback delay_ms(62); //等待测量结果 //4*62 = 248ms if(Succeed_EchoFlag) { flag_unit++; data_temp += (distance_tim4*10)/58; Succeed_EchoFlag = 0; if(flag_unit > 3) { Data_unit = data_temp/4; data_temp = 0; flag_unit = 0; flag_send = 1; buffhcsr[0] = 1; buffhcsr[1] = (u8)(Data_unit); buffhcsr[2] = Data_unit>>8; } } //add signal os_sem to indicate usart1 send masssage to pc if(flag_send) { //等待PC通信发送命令 OSSemPend(sem_Hcsr04,0,&err); for(i = 0;i < 3; i++) { USART_SendData(USART1,buffhcsr); while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); } flag_send = 0; } } |
|
|
|
多谢多谢,还有个问题需要咨询一下有两个变量是不是这么定义的:
#define Echo GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) #define Trig Bout(6)// B6 能不能把你的工程分享一下,刚开始学不咋会,谢谢啊 |
|
|
|
只有小组成员才能发言,加入小组>>
810 浏览 0 评论
1161 浏览 1 评论
2535 浏览 5 评论
2871 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2719 浏览 6 评论
keil5中manage run-time environment怎么是灰色,不可以操作吗?
1109浏览 3评论
198浏览 2评论
465浏览 2评论
378浏览 2评论
M0518 PWM的电压输出只有2V左右,没有3.3V是怎么回事?
460浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-27 03:33 , Processed in 1.122353 second(s), Total 81, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号