RCWL0801激光测距传感器模块驱动(基于传感器管理组件)

RCWL0801是一款可3.3V-5V供电,采用优质激光测距传感器VL53L0X的测距模块。模块采用高性能MCU,内部集成VL53L0X的运算滤波算法,可直接串口输出测量距离。

RCWL_0801.c

  1 /**
  2  * @file RCWL_0801.c
  3  * @brief
  4  * @version 0.1
  5  * @date 2019-06-28
  6  *
  7  * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
  8  *
  9  */
 10 /*-----------------------------------------------------------------------------
 11                             include
 12 -----------------------------------------------------------------------------*/
 13 #include "RCWL_0801.h"
 14 #include "ci_log.h"
 15 #include "ci110x_uart.h"
 16 
 17 /*-----------------------------------------------------------------------------
 18                             define
 19 -----------------------------------------------------------------------------*/
 20 #define RCWL_0801_RECEIVE_DATA_UART (UART1)/*!< 接收数据串口 */
 21 
 22 #define RCWL_0801_INIT_CMD                      (0xA1)/*!< 初始化整个模块 */
 23 #define RCWL_0801_OPEN_CMD                      (0xD0)/*!< 打开VL53L0X模块 */
 24 #define RCWL_0801_CLOSE_CMD                     (0xD1)/*!< 关闭VL53L0X模块 */
 25 
 26 #define RCWL_0801_READ_DATA_CMD                 (0xA0)/*!< 读取测量数据 */
 27 #define RCWL_0801_READ_STATE_CMD                (0xF0)/*!< 读取当前状态 */
 28 
 29 #define RCWL_0801_SET_RATE_9600_CMD             (0xB0)/*!< 波特率9600 */
 30 #define RCWL_0801_SET_RATE_19200_CMD            (0xB1)/*!< 波特率19200 */
 31 #define RCWL_0801_SET_RATE_115200_CMD           (0x02)/*!< 波特率115200 */
 32 
 33 #define RCWL_0801_SET_MODE_DISTANCE_MODE_CMD    (0xC0)/*!< 长距离模式 */
 34 #define RCWL_0801_SET_MODE_SPEED_MODE_CMD       (0xC1)/*!< 高速模式 */
 35 #define RCWL_0801_SET_MODE_PRECISION_MODE_CMD   (0xC2)/*!< 高精度模式 */
 36 
 37 /*-----------------------------------------------------------------------------
 38                             extern
 39 -----------------------------------------------------------------------------*/
 40 
 41 /*-----------------------------------------------------------------------------
 42                         struct / enum / union
 43 -----------------------------------------------------------------------------*/
 44 
 45 /*-----------------------------------------------------------------------------
 46                             global
 47 -----------------------------------------------------------------------------*/
 48 static uint8_t data_h,data_l;
 49 static uint16_t rcwl_data = 0;
 50 
 51 /*-----------------------------------------------------------------------------
 52                             declare
 53 -----------------------------------------------------------------------------*/
 54 
 55 /*-----------------------------------------------------------------------------
 56                             function
 57 -----------------------------------------------------------------------------*/
 58 
 59 /**
 60  * @brief rcwl_0801测试数据更新
 61  *
 62  */
 63 void rcwl_0801_update(void)
 64 {
 65     static uint8_t data_flag = 1;
 66     if(data_flag)
 67     {
 68         data_h = UART_RXDATA(RCWL_0801_RECEIVE_DATA_UART);
 69         data_flag = 0;
 70     }
 71     else
 72     {
 73         data_l = UART_RXDATA(RCWL_0801_RECEIVE_DATA_UART);
 74         rcwl_data = ((data_h << 8) + data_l);
 75         data_flag = 1;
 76     }
 77 }
 78 
 79 /**
 80  * @brief RCWL_0801传感器初始化
 81  *
 82  * @param irq_callback 中断回调
 83  * @retval RETURN_OK
 84  * @retval RETURN_ERR
 85  */
 86 int32_t rcwl_0801_open(void)
 87 {
 88     UARTPollingConfig(RCWL_0801_RECEIVE_DATA_UART);
 89     /* init */
 90     UartPollingSenddata(RCWL_0801_RECEIVE_DATA_UART,RCWL_0801_INIT_CMD);
 91     UartPollingSenddone(RCWL_0801_RECEIVE_DATA_UART);
 92     UARTInterruptConfig(RCWL_0801_RECEIVE_DATA_UART,UART_BaudRate9600);
 93     return RETURN_OK;
 94 }
 95 
 96 /**
 97  * @brief RCWL_0801传感器读数据
 98  *
 99  * @param data 读到的数据
100  * @retval RETURN_OK
101  * @retval RETURN_ERR
102  */
103 int32_t rcwl_0801_read(sensor_data_t *data)
104 {
105     sensor_data_inform(SENSOR_TYPE_LASER_FINDER);
106     data->laser_finder = rcwl_data;
107     mprintf("RCWL-0801 测量结果 %d\n",rcwl_data);
108     return RETURN_OK;
109 }
110 
111 /**
112  * @brief RCWL_0801传感器ops
113  *
114  */
115 sensor_ops_t rcwl_0801_ops =
116 {
117     rcwl_0801_open,
118     rcwl_0801_read,
119 };
120 
121 /*-----------------------------------------------------------------------------
122                             end of the file
123 -----------------------------------------------------------------------------*/

RCWL_0801.h

 1 /**
 2  * @file RCWL_0801.h
 3  * @brief RCWL_0801传感器的头文件
 4  * @version 0.1
 5  * @date 2019-07-02
 6  *
 7  * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
 8  *
 9  */
10 
11 #ifndef __RCWL_0801_H__
12 #define __RCWL_0801_H__
13 
14 /**
15  * @ingroup third_device_driver
16  * @defgroup RCWL0801
17  * @brief RCWL0801传感器驱动
18  * @{
19  */
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*-----------------------------------------------------------------------------
26                             include
27 -----------------------------------------------------------------------------*/
28 #include "ci_sensor.h"
29 
30 /*-----------------------------------------------------------------------------
31                             define
32 -----------------------------------------------------------------------------*/
33 
34 /*-----------------------------------------------------------------------------
35                             extern
36 -----------------------------------------------------------------------------*/
37 extern sensor_ops_t rcwl_0801_ops;
38 
39 /*-----------------------------------------------------------------------------
40                         struct / enum / union
41 -----------------------------------------------------------------------------*/
42 
43 /*-----------------------------------------------------------------------------
44                             global
45 -----------------------------------------------------------------------------*/
46 
47 /*-----------------------------------------------------------------------------
48                         function declare
49 -----------------------------------------------------------------------------*/
50 void rcwl_0801_update(void);
51 
52 #ifdef __cplusplus
53 }
54 #endif
55 
56 /**
57  * @}
58  */
59 
60 #endif
61 
62 /*-----------------------------------------------------------------------------
63                             end of the file
64 -----------------------------------------------------------------------------*/

 

你可能感兴趣的:(RCWL0801激光测距传感器模块驱动(基于传感器管理组件))