-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultra.c
72 lines (59 loc) · 2.54 KB
/
ultra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "ultra.h"
/// initialization HRS04 variables
void init_hrsd04_variable(HRS04_VAR* sen){
sen->cap_rising_edge = 0;
sen->cap_falling_edge = 0;
sen->pulse_width = 0;
sen->capture = false;
sen->distance = false;
}
/// generate the input signal
void Triger_InputSig(HRS04_VAR* sen){
GPIO_SetBits(sen->trig_port, sen->trig_pin);
delay_us(10);
GPIO_ResetBits(sen->trig_port, sen->trig_pin);
}
void ultra_setup(HRS04_VAR* sen)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
// RCC Configuration
RCC_APB2PeriphClockCmd(sen->trig_rcc, ENABLE);
// GPIO Output for Trigger
GPIO_InitStructure.GPIO_Pin = sen->trig_pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(sen->trig_port, &GPIO_InitStructure);
// TIMx Ch1 (sen->timer_pin)
GPIO_InitStructure.GPIO_Pin = sen->timer_pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(sen->timer_port, &GPIO_InitStructure);
// RCC Configuration
RCC_APB1PeriphClockCmd(sen->timer_rcc, ENABLE );
// Enable the TIMx global Interrupt
NVIC_InitStructure.NVIC_IRQChannel = sen->timer_irq;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
// Generate 1usec timer using 72 MHz clock
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000000) - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(sen->timer, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channelx */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_Channel = sen->timer_inp_channel;
TIM_ICInit(sen->timer, &TIM_ICInitStructure);
TIM_Cmd(sen->timer, ENABLE);
TIM_ITConfig(sen->timer, sen->timer_cc_channel, ENABLE);
init_hrsd04_variable(sen);
}