初级会员
- 积分
- 50
- 金钱
- 50
- 注册时间
- 2011-4-8
- 在线时间
- 3 小时
|
发表于 2015-11-3 11:51:24
|
显示全部楼层
#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;
}
} |
|