hc-sr04.c
#ifndef __HCSR04_H__
#define __HCSR04_H__
#include "tim.h"
#ifdef __cplusplus
class HCSR
{
public:
void HC04_id_init(char id_);
void HC_Start();
void HC_measure();
float Distance;
private:
char id;
int Edge;
unsigned int t1,t2,T;
};
extern HCSR hcsr_04[4];
#endif
void HC_init();
void HC_rise(char id);
void HC_falling(char id);
uint32_t HC_time(char id);
void HC_start(char id);
void TIM1_Delay_us(unsigned short int n_us);
#endif
hc-sr04.c
#include "tim.h"
#include "hc-sr04.h"
HCSR hcsr_04[4];
void HC_init(){
HAL_TIM_Base_Start(&htim3);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_3);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_4);
hcsr_04[0].HC04_id_init(0);
hcsr_04[1].HC04_id_init(1);
hcsr_04[2].HC04_id_init(2);
hcsr_04[3].HC04_id_init(3);
}
void TIM1_Delay_us(uint16_t n_us)
{
__HAL_TIM_ENABLE(&htim3);
__HAL_TIM_SetCounter(&htim3, 0);
while(__HAL_TIM_GetCounter(&htim3) < ((1 * n_us)-1) );
__HAL_TIM_DISABLE(&htim3);
}
void HCSR::HC04_id_init(char id_)
{
id=id_;
}
void HCSR::HC_Start()
{
HC_start(id);
}
void HCSR::HC_measure()
{
if(Edge == 0)
{
t1 = HC_time(id);
HC_falling(id);
Edge = 1;
}
else if(Edge == 1)
{
t2 = HC_time(id);
if(t2>t1)
{
T = t2 - t1;
}
else
{
T = t2 - t1 + 65535;
}
HC_rise(id);
Distance = T * 0.00001 * 170 * 100 ;
Edge = 0;
}
}
void HC_falling(char id)
{
if(id==0){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_1,TIM_INPUTCHANNELPOLARITY_FALLING);}
if(id==1){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_FALLING);}
if(id==2){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_3,TIM_INPUTCHANNELPOLARITY_FALLING);}
if(id==3){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_4,TIM_INPUTCHANNELPOLARITY_FALLING);}
}
void HC_rise(char id)
{
if(id==0){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_1,TIM_INPUTCHANNELPOLARITY_RISING);}
if(id==1){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_RISING);}
if(id==2){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_3,TIM_INPUTCHANNELPOLARITY_RISING);}
if(id==3){__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_4,TIM_INPUTCHANNELPOLARITY_RISING);}
}
uint32_t HC_time(char id)
{
if(id==0){return HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1);}
if(id==1){return HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2);}
if(id==2){return HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_3);}
if(id==3){return HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_4);}
}
void HC_start(char id)
{
if(id==0)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
TIM1_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
}
if(id==1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
TIM1_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
}
if(id==2)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
TIM1_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
}
if(id==3)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
TIM1_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if((htim->Instance == htim3.Instance) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)){hcsr_04[0].HC_measure();}
if((htim->Instance == htim3.Instance) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)){hcsr_04[1].HC_measure();}
if((htim->Instance == htim3.Instance) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)){hcsr_04[2].HC_measure();}
if((htim->Instance == htim3.Instance) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)){hcsr_04[3].HC_measure();}
}