forked from MaJerle/stm32f429
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm_stm32f4_hcsr04.h
177 lines (155 loc) · 4.56 KB
/
tm_stm32f4_hcsr04.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @author Tilen MAJERLE
* @email [email protected]
* @website http://stm32f4-discovery.net
* @link http://stm32f4-discovery.net/2014/08/library-30-measure-distance-with-hc-sr04-and-stm32f4xx
* @version v2.0
* @ide Keil uVision
* @license GNU GPL v3
* @brief Measure distance with HC-SR04 Ultrasonic distance sensor on STM32F4xx devices
*
@verbatim
----------------------------------------------------------------------
Copyright (C) Tilen MAJERLE, 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------
@endverbatim
*/
#ifndef TM_HCSR04_H
#define TM_HCSR04_H 200
/**
* @addtogroup TM_STM32F4xx_Libraries
* @{
*/
/**
* @defgroup TM_HCSR04
* @brief Measure distance with HC-SR04 Ultrasonic distance sensor on STM32F4xx devices - http://stm32f4-discovery.net/2014/08/library-30-measure-distance-with-hc-sr04-and-stm32f4xx
* @{
*
* \par Default pinout
*
* There is not default pinout anymore.
* As of version 2.0, you can use unlimited HC-SR sensors connected to STM32F4xx device.
*
* You just make sure that you use different pins for different sensors.
*
* Pins are passed on @ref TM_HCSR04_Init function.
*
@verbatim
I didn't tested that, but you might use the same ECHO pin for all sensors if you read sensor by sensor at a time. Do that by own risk!
@endverbatim
*
*
* \par Changelog
*
@verbatim
Version 2.0
- March 29, 2015
- Library has been rewritten. Now supports unlimited number of HCSR04 devices
- Only limit is number of GPIO pins available in your MCU package
Version 1.2
- March 11, 2015
- Added support for my new GPIO library
Version 1.1
- November 29, 2014
- Update to fit new delay system
Version 1.0
- First release
@endverbatim
*
* \par Dependencies
*
@verbatim
- STM32F4xx
- STM32F4xx RCC
- defines.h
- TM DELAY
- TM GPIO
@endverbatim
*/
#include "stm32f4xx.h"
#include "defines.h"
#include "tm_stm32f4_delay.h"
#include "tm_stm32f4_gpio.h"
/**
* @defgroup TM_HCSR04_Macros
* @brief Library macros
* @{
*/
/* Default timeout pulses */
#ifndef HCSR04_TIMEOUT
#define HCSR04_TIMEOUT 1000000
#endif
/**
* @brief Time in microseconds to centimeters conversion
*/
#define HCSR04_NUMBER ((float)0.0171821)
/**
* @}
*/
/**
* @defgroup TM_HCSR04_Typedefs
* @brief Library Typedefs
* @{
*/
/**
* @brief HC-SR04 working structure
*/
typedef struct {
float Distance; /*!< Distance measured from sensor in centimeters */
GPIO_TypeDef* ECHO_GPIOx; /*!< Pointer to GPIOx PORT for ECHO pin. Meant for private use only */
uint16_t ECHO_GPIO_Pin; /*!< GPIO Pin for ECHO pin. Meant for private use only */
GPIO_TypeDef* TRIGGER_GPIOx; /*!< Pointer to GPIOx PORT for TRIGGER pin. Meant for private use only */
uint16_t TRIGGER_GPIO_Pin; /*!< GPIO Pin for ECHO pin. Meant for private use only */
} TM_HCSR04_t;
/**
* @}
*/
/**
* @defgroup TM_HCSR04_Functions
* @brief Library Functions
* @{
*/
/**
* @brief Initializes HC-SR04 sensor
* @param *HCSR04: Pointer to empty @ref TM_HCSR04_t structure to save initialization data
* @param *ECHO_GPIOx: Pointer to GPIOx PORT for ECHO pin
* @param ECHO_GPIO_Pin: GPIO Pin for ECHO pin
* @param *TRIGGER_GPIOx: Pointer to GPIOx PORT for TRIGGER pin
* @param TRIGGER_GPIO_Pin: GPIO Pin for ECHO pin
* @retval HC-SR04 status:
* - 0: Device not detected
* - > 0: Device is ready to use
*/
uint8_t TM_HCSR04_Init(TM_HCSR04_t* HCSR04, GPIO_TypeDef* ECHO_GPIOx, uint16_t ECHO_GPIO_Pin, GPIO_TypeDef* TRIGGER_GPIOx, uint16_t TRIGGER_GPIO_Pin);
/**
* @brief Starts sensor measurement and read it's data
* @param *HCSR04: Pointer to @ref TM_HCSR04_t structure to save initialization data
* @retval Distance in float:
* - > 0: Valid distance in cm (centimeters)
* - -1: Error
*/
float TM_HCSR04_Read(TM_HCSR04_t* HCSR04);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/* C++ detection */
#ifdef __cplusplus
}
#endif
#endif