12058|22

9713

帖子

24

TA的资源

版主

楼主
 

vl53l0x测距程序,使用STM32L4R5 [复制链接]

 
 STM32CubeMX工程
D15->SCL->PB8
D14->SDA->PB9







  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * [url=home.php?mod=space&uid=159083]@brief[/url]          : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32l4xx_hal.h"

  41. /* USER CODE BEGIN Includes */
  42. #include "component.h"  
  43. #include "stdio.h"  
  44. #include "nucleo144_prox.h"  

  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/
  47. I2C_HandleTypeDef hi2c1;

  48. UART_HandleTypeDef hlpuart1;

  49. /* USER CODE BEGIN PV */
  50. /* Private variables ---------------------------------------------------------*/
  51. #ifdef __GNUC__  
  52. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)  
  53. #else  
  54. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)  
  55. #endif /* __GNUC__ */  
  56. void *VL53L0X_0_handler;
  57. UART_HandleTypeDef UARTHandle;
  58. volatile uint8_t no_VL53L0X;
  59. uint16_t range;
  60. /* USER CODE END PV */

  61. /* Private function prototypes -----------------------------------------------*/
  62. void SystemClock_Config(void);
  63. static void MX_GPIO_Init(void);
  64. static void MX_I2C1_Init(void);
  65. static void MX_LPUART1_UART_Init(void);

  66. /* USER CODE BEGIN PFP */
  67. /* Private function prototypes -----------------------------------------------*/

  68. /* USER CODE END PFP */

  69. /* USER CODE BEGIN 0 */
  70. void PROX_Measurement(void)
  71. {
  72.   uint8_t proxStatus, pDataready;
  73.   BSP_PROX_IsInitialized( VL53L0X_0_handler, &proxStatus );
  74.   if(proxStatus == 1)
  75.   {
  76.     BSP_PROX_Start_Measurement(VL53L0X_0_handler);
  77.     do
  78.     {
  79.       BSP_PROX_Get_Measurement_DataReady( VL53L0X_0_handler, &pDataready);
  80.     }
  81.     while(!pDataready);
  82.     BSP_PROX_Get_Range(VL53L0X_0_handler, &range);
  83.   }
  84. }
  85. /* USER CODE END 0 */

  86. /**
  87.   * @brief  The application entry point.
  88.   *
  89.   * @retval None
  90.   */
  91. int main(void)
  92. {
  93.   /* USER CODE BEGIN 1 */

  94.   /* USER CODE END 1 */

  95.   /* MCU Configuration----------------------------------------------------------*/

  96.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  97.   HAL_Init();

  98.   /* USER CODE BEGIN Init */
  99.   /* USER CODE END Init */

  100.   /* Configure the system clock */
  101.   SystemClock_Config();

  102.   /* USER CODE BEGIN SysInit */
  103.   /* USER CODE END SysInit */

  104.   /* Initialize all configured peripherals */
  105.   MX_GPIO_Init();
  106.   MX_I2C1_Init();
  107.   MX_LPUART1_UART_Init();
  108.   /* USER CODE BEGIN 2 */
  109.   
  110.   if (BSP_PROX_Init( VL53L0X_0, &VL53L0X_0_handler) == COMPONENT_ERROR)
  111.   {
  112.     no_VL53L0X=1;
  113.   }
  114.   printf("BSP_PROX_Init.\r");
  115.   /* USER CODE END 2 */

  116.   /* Infinite loop */
  117.   /* USER CODE BEGIN WHILE */
  118.   while (1)
  119.   {
  120.   /* USER CODE END WHILE */

  121.   /* USER CODE BEGIN 3 */
  122.     PROX_Measurement();
  123.     printf("%d\r",range);
  124.   }
  125.   /* USER CODE END 3 */

  126. }

  127. /**
  128.   * @brief System Clock Configuration
  129.   * @retval None
  130.   */
  131. void SystemClock_Config(void)
  132. {

  133.   RCC_OscInitTypeDef RCC_OscInitStruct;
  134.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  135.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  136.     /**Configure the main internal regulator output voltage
  137.     */
  138.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST) != HAL_OK)
  139.   {
  140.     _Error_Handler(__FILE__, __LINE__);
  141.   }

  142.     /**Initializes the CPU, AHB and APB busses clocks
  143.     */
  144.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  145.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  146.   RCC_OscInitStruct.MSICalibrationValue = 0;
  147.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  148.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  149.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  150.   RCC_OscInitStruct.PLL.PLLM = 1;
  151.   RCC_OscInitStruct.PLL.PLLN = 60;
  152.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  153.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  154.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  155.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  156.   {
  157.     _Error_Handler(__FILE__, __LINE__);
  158.   }

  159.     /**Initializes the CPU, AHB and APB busses clocks
  160.     */
  161.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  162.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  163.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  164.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  165.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  166.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  167.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  168.   {
  169.     _Error_Handler(__FILE__, __LINE__);
  170.   }

  171.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_I2C1;
  172.   PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  173.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  174.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  175.   {
  176.     _Error_Handler(__FILE__, __LINE__);
  177.   }

  178.     /**Configure the Systick interrupt time
  179.     */
  180.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  181.     /**Configure the Systick
  182.     */
  183.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  184.   /* SysTick_IRQn interrupt configuration */
  185.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  186. }

  187. /* I2C1 init function */
  188. static void MX_I2C1_Init(void)
  189. {

  190.   hi2c1.Instance = I2C1;
  191.   hi2c1.Init.Timing = 0x107075B0;
  192.   hi2c1.Init.OwnAddress1 = 0;
  193.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  194.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  195.   hi2c1.Init.OwnAddress2 = 0;
  196.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  197.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  198.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  199.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  200.   {
  201.     _Error_Handler(__FILE__, __LINE__);
  202.   }

  203.     /**Configure Analogue filter
  204.     */
  205.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  206.   {
  207.     _Error_Handler(__FILE__, __LINE__);
  208.   }

  209.     /**Configure Digital filter
  210.     */
  211.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  212.   {
  213.     _Error_Handler(__FILE__, __LINE__);
  214.   }

  215. }

  216. /* LPUART1 init function */
  217. static void MX_LPUART1_UART_Init(void)
  218. {

  219.   hlpuart1.Instance = LPUART1;
  220.   hlpuart1.Init.BaudRate = 115200;
  221.   hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  222.   hlpuart1.Init.StopBits = UART_STOPBITS_1;
  223.   hlpuart1.Init.Parity = UART_PARITY_NONE;
  224.   hlpuart1.Init.Mode = UART_MODE_TX_RX;
  225.   hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  226.   hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  227.   hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  228.   hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  229.   hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
  230.   if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  231.   {
  232.     _Error_Handler(__FILE__, __LINE__);
  233.   }

  234.   if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  235.   {
  236.     _Error_Handler(__FILE__, __LINE__);
  237.   }

  238.   if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  239.   {
  240.     _Error_Handler(__FILE__, __LINE__);
  241.   }

  242. }

  243. /** Configure pins as
  244.         * Analog
  245.         * Input
  246.         * Output
  247.         * EVENT_OUT
  248.         * EXTI
  249.      PD8   ------> USART3_TX
  250.      PD9   ------> USART3_RX
  251.      PA8   ------> USB_OTG_FS_SOF
  252.      PA9   ------> USB_OTG_FS_VBUS
  253.      PA10   ------> USB_OTG_FS_ID
  254.      PA11   ------> USB_OTG_FS_DM
  255.      PA12   ------> USB_OTG_FS_DP
  256. */
  257. static void MX_GPIO_Init(void)
  258. {

  259.   GPIO_InitTypeDef GPIO_InitStruct;

  260.   /* GPIO Ports Clock Enable */
  261.   __HAL_RCC_GPIOC_CLK_ENABLE();
  262.   __HAL_RCC_GPIOH_CLK_ENABLE();
  263.   __HAL_RCC_GPIOB_CLK_ENABLE();
  264.   __HAL_RCC_GPIOD_CLK_ENABLE();
  265.   __HAL_RCC_GPIOG_CLK_ENABLE();
  266.   HAL_PWREx_EnableVddIO2();
  267.   __HAL_RCC_GPIOA_CLK_ENABLE();

  268.   /*Configure GPIO pin Output Level */
  269.   HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);

  270.   /*Configure GPIO pin Output Level */
  271.   HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);

  272.   /*Configure GPIO pin : B1_Pin */
  273.   GPIO_InitStruct.Pin = B1_Pin;
  274.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  275.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  276.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  277.   /*Configure GPIO pins : LD3_Pin LD2_Pin */
  278.   GPIO_InitStruct.Pin = LD3_Pin|LD2_Pin;
  279.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  280.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  281.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  282.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  283.   /*Configure GPIO pins : STLK_RX_Pin STLK_TX_Pin */
  284.   GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;
  285.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  286.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  287.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  288.   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  289.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  290.   /*Configure GPIO pin : USB_OverCurrent_Pin */
  291.   GPIO_InitStruct.Pin = USB_OverCurrent_Pin;
  292.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  293.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  294.   HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);

  295.   /*Configure GPIO pin : USB_PowerSwitchOn_Pin */
  296.   GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;
  297.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  298.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  299.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  300.   HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);

  301.   /*Configure GPIO pins : USB_SOF_Pin USB_ID_Pin USB_DM_Pin USB_DP_Pin */
  302.   GPIO_InitStruct.Pin = USB_SOF_Pin|USB_ID_Pin|USB_DM_Pin|USB_DP_Pin;
  303.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  304.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  305.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  306.   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
  307.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  308.   /*Configure GPIO pin : USB_VBUS_Pin */
  309.   GPIO_InitStruct.Pin = USB_VBUS_Pin;
  310.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  311.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  312.   HAL_GPIO_Init(USB_VBUS_GPIO_Port, &GPIO_InitStruct);

  313. }

  314. /* USER CODE BEGIN 4 */

  315. PUTCHAR_PROTOTYPE  
  316. {  
  317.     HAL_UART_Transmit(&hlpuart1 , (uint8_t *)&ch, 1, 0xFFFF);  
  318.     return ch;  
  319. }  

  320. /* USER CODE END 4 */

  321. /**
  322.   * @brief  This function is executed in case of error occurrence.
  323.   * @param  file: The file name as string.
  324.   * @param  line: The line in file as a number.
  325.   * @retval None
  326.   */
  327. void _Error_Handler(char *file, int line)
  328. {
  329.   /* USER CODE BEGIN Error_Handler_Debug */
  330.   /* User can add his own implementation to report the HAL error return state */
  331.   while(1)
  332.   {
  333.   }
  334.   /* USER CODE END Error_Handler_Debug */
  335. }

  336. #ifdef  USE_FULL_ASSERT
  337. /**
  338.   * @brief  Reports the name of the source file and the source line number
  339.   *         where the assert_param error has occurred.
  340.   * @param  file: pointer to the source file name
  341.   * @param  line: assert_param error line source number
  342.   * @retval None
  343.   */
  344. void assert_failed(uint8_t* file, uint32_t line)
  345. {
  346.   /* USER CODE BEGIN 6 */
  347.   /* User can add his own implementation to report the file name and line number,
  348.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  349.   /* USER CODE END 6 */
  350. }
  351. #endif /* USE_FULL_ASSERT */

  352. /**
  353.   * @}
  354.   */

  355. /**
  356.   * @}
  357.   */

  358. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

游客,如果您要查看本帖隐藏内容请回复

最新回复

这个代码感觉十分复杂啊,怎么样看懂,注释也没有啊   STM32好难啊,我应该放弃吗   详情 回复 发表于 2024-6-2 21:58
点赞 关注
个人签名虾扯蛋,蛋扯虾,虾扯蛋扯虾
 

回复
举报

152

帖子

0

TA的资源

一粒金砂(中级)

沙发
 
学习学习,后面要做这个模块,
个人签名

欢迎大家来到电机驱动控制技术天地,一起在EEworld交流、学习。

 
 

回复

2

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
vl53l0x测距程序,使用STM32L4R5
 
 
 

回复

2

帖子

0

TA的资源

一粒金砂(初级)

4
 
正好需要用到,感謝
 
 
 

回复

4

帖子

0

TA的资源

一粒金砂(初级)

5
 
 
 

回复

2

帖子

0

TA的资源

一粒金砂(初级)

6
 
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

7
 
正在调试,看看先。
 
 
 

回复

18

帖子

0

TA的资源

一粒金砂(中级)

8
 
我是来看寄存器的...........
 
 
 

回复

2

帖子

0

TA的资源

一粒金砂(初级)

9
 
目前正在寻找着方面的资料
 
 
 

回复

4

帖子

0

TA的资源

一粒金砂(中级)

10
 
不错,谢谢分享
 
 
 

回复

111

帖子

0

TA的资源

一粒金砂(中级)

11
 
 
 

回复

16

帖子

0

TA的资源

一粒金砂(中级)

12
 
买了VL53L1X 版本了,代码应该可以兼容
 
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

13
 
init(I2C.MASTER, baudrate=20000)
 
 
 

回复

2

帖子

0

TA的资源

一粒金砂(初级)

14
 
 
 

回复

22

帖子

0

TA的资源

一粒金砂(初级)

15
 
 
 
 

回复

5

帖子

0

TA的资源

一粒金砂(初级)

16
 
如果您要查看本帖隐藏内容请回复
 
 
 

回复

31

帖子

0

TA的资源

一粒金砂(中级)

17
 
 
 

回复

55

帖子

0

TA的资源

禁止发言

18
个人签名

深圳市全球芯科技 有限公司(加密芯片/SRAM/pSRAM/图像传感器 13924645577 )

 
 
 

回复

34

帖子

0

TA的资源

一粒金砂(中级)

19
 

不错,谢谢分享

 
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

20
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表