找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2621|回复: 1
打印 上一主题 下一主题
收起左侧

STM32F103硬件IIC驱动ADXL345(HAL库版)

[复制链接]
跳转到指定楼层
楼主
ID:466381 发表于 2021-3-14 17:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ADXL345,简称MPU6050弱化版的三轴加速度计,运用IIC接口驱动,再加上STM32CUBEMX以及给力的硬件IIC,绝对实用。

单片机源程序如下:
  1. /* USER CODE END Header */
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "i2c.h"
  5. #include "spi.h"
  6. #include "gpio.h"

  7. /* Private includes ----------------------------------------------------------*/
  8. /* USER CODE BEGIN Includes */
  9. #include "lcd144.h"
  10. #include "gui.h"
  11. #include "adxl345.h"
  12. /* USER CODE END Includes */

  13. /* Private typedef -----------------------------------------------------------*/
  14. /* USER CODE BEGIN PTD */
  15. uint8_t SHENJIAJIA[6];
  16. int a_x;
  17. int a_y;
  18. int a_z;
  19. /* USER CODE END PTD */

  20. /* Private define ------------------------------------------------------------*/
  21. /* USER CODE BEGIN PD */
  22. /* USER CODE END PD */

  23. /* Private macro -------------------------------------------------------------*/
  24. /* USER CODE BEGIN PM */

  25. /* USER CODE END PM */

  26. /* Private variables ---------------------------------------------------------*/

  27. /* USER CODE BEGIN PV */

  28. /* USER CODE END PV */

  29. /* Private function prototypes -----------------------------------------------*/
  30. void SystemClock_Config(void);
  31. /* USER CODE BEGIN PFP */

  32. /* USER CODE END PFP */

  33. /* Private user code ---------------------------------------------------------*/
  34. /* USER CODE BEGIN 0 */

  35. /* USER CODE END 0 */

  36. /**
  37.   * @brief  The application entry point.
  38.   * @retval int
  39.   */
  40. int main(void)
  41. {
  42.   /* USER CODE BEGIN 1 */

  43.   /* USER CODE END 1 */

  44.   /* MCU Configuration--------------------------------------------------------*/

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

  47.   /* USER CODE BEGIN Init */

  48.   /* USER CODE END Init */

  49.   /* Configure the system clock */
  50.   SystemClock_Config();

  51.   /* USER CODE BEGIN SysInit */

  52.   /* USER CODE END SysInit */

  53.   /* Initialize all configured peripherals */
  54.   MX_GPIO_Init();
  55.   MX_SPI2_Init();
  56.   MX_I2C1_Init();
  57.   /* USER CODE BEGIN 2 */
  58.         Lcd_Init();
  59.   /* USER CODE END 2 */

  60.   /* Infinite loop */
  61.   /* USER CODE BEGIN WHILE */
  62.         Init_ADXL345();
  63.   while (1)
  64.   {
  65.     /* USER CODE END WHILE */

  66.     /* USER CODE BEGIN 3 */
  67.                 Multiple_Read_ADXL345(SHENJIAJIA);   
  68.                 a_x=Cauculate_X(SHENJIAJIA);
  69.                 a_y=Cauculate_Y(SHENJIAJIA);
  70.                 a_z=Cauculate_Z(SHENJIAJIA);
  71.                 if(a_x<0)
  72.                 {
  73.                         a_x=-a_x;
  74.                         Gui_DrawFont_GBK16(0,0,BLACK,WHITE,"-");                       
  75.                 }
  76.                 if(a_y<0)
  77.                 {
  78.                         a_y=-a_y;
  79.                         Gui_DrawFont_GBK16(0,16,BLACK,WHITE,"-");                       
  80.                 }
  81.                 if(a_z<0)
  82.                 {
  83.                         a_z=-a_z;
  84.                         Gui_DrawFont_GBK16(0,32,BLACK,WHITE,"-");                       
  85.                 }
  86.                 Gui_DrawFont_Num32(32,0,BLACK,WHITE,a_x);
  87.                 Gui_DrawFont_Num32(32,16,BLACK,WHITE,a_y);
  88.                 Gui_DrawFont_Num32(32,32,BLACK,WHITE,a_z);
  89.                 //Lcd_Clear(WHITE);
  90.                 HAL_Delay(500);
  91.   }
  92.   /* USER CODE END 3 */
  93. }

  94. /**
  95.   * @brief System Clock Configuration
  96.   * @retval None
  97.   */
  98. void SystemClock_Config(void)
  99. {
  100.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  101.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  102.   /** Initializes the RCC Oscillators according to the specified parameters
  103.   * in the RCC_OscInitTypeDef structure.
  104.   */
  105.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  106.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  107.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  108.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  109.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  110.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  111.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  112.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  113.   {
  114.     Error_Handler();
  115.   }
  116.   /** Initializes the CPU, AHB and APB buses clocks
  117.   */
  118.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  119.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  120.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  121.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  122.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  123.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  124.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  125.   {
  126.     Error_Handler();
  127.   }
  128.   /** Enables the Clock Security System
  129.   */
  130.   HAL_RCC_EnableCSS();
  131. }

  132. /* USER CODE BEGIN 4 */

  133. /* USER CODE END 4 */

  134. /**
  135.   * @brief  This function is executed in case of error occurrence.
  136.   * @retval None
  137.   */
  138. void Error_Handler(void)
  139. {
  140.   /* USER CODE BEGIN Error_Handler_Debug */
  141.   /* User can add his own implementation to report the HAL error return state */
  142.   __disable_irq();
  143.   while (1)
  144.   {
  145.   }
  146.   /* USER CODE END Error_Handler_Debug */
  147. }

  148. #ifdef  USE_FULL_ASSERT
  149. /**
  150.   * @brief  Reports the name of the source file and the source line number
  151.   *         where the assert_param error has occurred.
  152.   * @param  file: pointer to the source file name
  153.   * @param  line: assert_param error line source number
  154.   * @retval None
  155.   */
  156. void assert_failed(uint8_t *file, uint32_t line)
  157. {
  158.   /* USER CODE BEGIN 6 */
  159.   /* User can add his own implementation to report the file name and line number,
  160.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  161.   /* USER CODE END 6 */
  162. }
  163. #endif /* USE_FULL_ASSERT */
复制代码

所有资料51hei提供下载:
ADXL345 TFT1.44(ST7735).7z (5.23 MB, 下载次数: 106)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:365823 发表于 2022-1-5 17:41 | 只看该作者
明明是IIC的
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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