找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4387|回复: 1
收起左侧

BMP180驱动程序,基于STM32f103ZET6 库版本:HAL

[复制链接]
ID:329372 发表于 2018-5-14 11:32 | 显示全部楼层 |阅读模式
BMP180 驱动程序,基于 STM32f103ZET6 ,开发平台:Keil 5 ,库版本:HAL
(BMP180 driver, based on STM32f103ZET6, development platform: Keil 5, library version: HAL.)

使用硬件IIC的方式,读取 BMP180 中的寄存器值(0xAA)

利用LPME1函数修改,硬件IIC可完成目标,但不规范

开发平台:Keil 5
0.jpg

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2018 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35. #include "i2c.h"
  36. #include "spi.h"
  37. #include "usart.h"
  38. #include "gpio.h"

  39. /* USER CODE BEGIN Includes */

  40. #include "bmp180.h"

  41. /* USER CODE END Includes */

  42. /* Private variables ---------------------------------------------------------*/

  43. /* USER CODE BEGIN PV */
  44. /* Private variables ---------------------------------------------------------*/

  45. /* USER CODE END PV */

  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. void Error_Handler(void);

  49. /* USER CODE BEGIN PFP */
  50. /* Private function prototypes -----------------------------------------------*/

  51. /* USER CODE END PFP */

  52. /* USER CODE BEGIN 0 */

  53. /* USER CODE END 0 */

  54. int main(void)
  55. {

  56.     /* USER CODE BEGIN 1 */

  57.     /* USER CODE END 1 */

  58.     /* MCU Configuration----------------------------------------------------------*/

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

  61.     /* Configure the system clock */
  62.     SystemClock_Config();

  63.     /* Initialize all configured peripherals */
  64.     MX_GPIO_Init();
  65.     MX_I2C1_Init();
  66.     //MX_SPI1_Init();
  67.     MX_USART1_UART_Init();

  68.     /* USER CODE BEGIN 2 */
  69.     float Temp = 10.0;
  70.     float data[2] = {0};
  71.     /* USER CODE END 2 */

  72.     /* Infinite loop */
  73.     /* USER CODE BEGIN WHILE */
  74.     while (1)
  75.     {
  76.     /* USER CODE END WHILE */

  77.     /* USER CODE BEGIN 3 */
  78.         //for(int i = 1000; i > 0; i--);
  79.         //lpme1_example();
  80.         lpme1_get_acc(data);

  81.         //printf("Temperature: %.1f \r\n", Temp);


  82.     }
  83.     /* USER CODE END 3 */

  84. }

  85. /** System Clock Configuration
  86. */
  87. void SystemClock_Config(void)
  88. {

  89.     RCC_OscInitTypeDef RCC_OscInitStruct;
  90.     RCC_ClkInitTypeDef RCC_ClkInitStruct;

  91.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  92.     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  93.     RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  94.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  95.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  96.     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  97.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98.     {
  99.         Error_Handler();
  100.     }

  101.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  102.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  103.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  104.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  105.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  106.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  107.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  108.     {
  109.         Error_Handler();
  110.     }

  111.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  112.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  113.     /* SysTick_IRQn interrupt configuration */
  114.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  115. }

  116. /* USER CODE BEGIN 4 */

  117. /* USER CODE END 4 */

  118. /**
  119.   * @brief  This function is executed in case of error occurrence.
  120.   * @param  None
  121.   * @retval None
  122.   */
  123. void Error_Handler(void)
  124. {
  125.   /* USER CODE BEGIN Error_Handler */
  126.   /* User can add his own implementation to report the HAL error return state */
  127.   while(1)
  128.   {
  129.   }
  130.   /* USER CODE END Error_Handler */
  131. }

  132. #ifdef USE_FULL_ASSERT

  133. /**
  134.    * @brief Reports the name of the source file and the source line number
  135.    * where the assert_param error has occurred.
  136.    * @param file: pointer to the source file name
  137.    * @param line: assert_param error line source number
  138. ……………………

  139. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
Cube_Demo_2_BMP180.zip (3.71 MB, 下载次数: 67)
回复

使用道具 举报

ID:73118 发表于 2018-7-31 14:18 | 显示全部楼层
下载下来了,意义不大,只有几句代码,没注释,没按数据手册校准,读出的数据也不知道为啥是float类型,算了还是对照数据手册重新做吧。
YJXVPQ77U$34CZFHFN`DZNO.png
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表