找回密码
 立即注册

QQ登录

只需一步,快速开始

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

用stm32的iic读取三轴指南针源码

[复制链接]
跳转到指定楼层
楼主
ID:376516 发表于 2018-7-21 12:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用stm32的iic读取三轴指南针

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : 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) 2017 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 "stm32f4xx_hal.h"
  41. #include "math.h"
  42. #ifdef __GNUC__
  43.   /* With GCC, small printf (option LD Linker->Libraries->Small printf
  44.      set to 'Yes') calls __io_putchar() */
  45.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  46. #else
  47.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  48. #endif



  49. I2C_HandleTypeDef hi2c1;

  50. UART_HandleTypeDef huart2;

  51. PUTCHAR_PROTOTYPE
  52. {
  53.   /* Place your implementation of fputc here */
  54.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  55.   HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

  56.   return ch;
  57. }
  58. /* USER CODE BEGIN PV */
  59. /* Private variables ---------------------------------------------------------*/

  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_USART2_UART_Init(void);

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

  68. /* USER CODE END PFP */

  69. /* USER CODE BEGIN 0 */




  70. int main(void)
  71. {

  72.   /* USER CODE BEGIN 1 */

  73.   /* USER CODE END 1 */

  74.   /* MCU Configuration----------------------------------------------------------*/

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

  77.   /* USER CODE BEGIN Init */

  78.   /* USER CODE END Init */

  79.   /* Configure the system clock */
  80.   SystemClock_Config();

  81.   /* USER CODE BEGIN SysInit */

  82.   /* USER CODE END SysInit */

  83.   /* Initialize all configured peripherals */
  84.   MX_GPIO_Init();
  85.   MX_I2C1_Init();
  86.   MX_USART2_UART_Init();

  87.   /* USER CODE BEGIN 2 */

  88.   /* USER CODE END 2 */

  89.   /* Infinite loop */
  90.   /* USER CODE BEGIN WHILE */

  91.    uint16_t  x,y,z;
  92.    double angle;

  93.    uint8_t  r_data,data[7],s=0x00;
  94.     HAL_I2C_Mem_Write(&hi2c1,0x3C,0x02,I2C_MEMADD_SIZE_8BIT,&s,1,10);
  95.    
  96.   while (1)
  97.   {  uint16_t  address=0x03;
  98.    HAL_Delay(50);
  99.    for(int i=0;i<6;i++)
  100.       {HAL_I2C_Mem_Read(&hi2c1,0x3D,address,I2C_MEMADD_SIZE_8BIT,&r_data,1,10);
  101.        data[i] = r_data;      
  102.        address=address +1;
  103.        HAL_Delay(50);
  104.       }
  105.     x = data[0] << 8 | data[1];
  106.     y = data[4] << 8 | data[5];
  107.     z = data[2] << 8 | data[3];
  108.     angle = atan2((double)y,(double)x) * (180 / 3.14159265) + 180;
  109.     HAL_Delay(10);
  110.     printf("角度为:%.3lf  \n",angle);
  111.     HAL_Delay(50);

  112.   }
  113.   /* USER CODE END 3 */

  114. }

  115. /** System Clock Configuration
  116. */
  117. void SystemClock_Config(void)
  118. {

  119.   RCC_OscInitTypeDef RCC_OscInitStruct;
  120.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  121.     /**Configure the main internal regulator output voltage
  122.     */
  123.   __HAL_RCC_PWR_CLK_ENABLE();

  124.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  125.     /**Initializes the CPU, AHB and APB busses clocks
  126.     */
  127.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  128.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  129.   RCC_OscInitStruct.HSICalibrationValue = 16;
  130.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  131.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  132.   RCC_OscInitStruct.PLL.PLLM = 8;
  133.   RCC_OscInitStruct.PLL.PLLN = 84;
  134.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  135.   RCC_OscInitStruct.PLL.PLLQ = 4;
  136.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  137.   {
  138.     _Error_Handler(__FILE__, __LINE__);
  139.   }

  140.     /**Initializes the CPU, AHB and APB busses clocks
  141.     */
  142.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  143.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  144.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  146.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  147.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  148.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  149.   {
  150.     _Error_Handler(__FILE__, __LINE__);
  151.   }

  152.     /**Configure the Systick interrupt time
  153.     */
  154.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  155.     /**Configure the Systick
  156.     */
  157.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  158.   /* SysTick_IRQn interrupt configuration */
  159.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  160. }

  161. /* I2C1 init function */
  162. static void MX_I2C1_Init(void)
  163. {

  164.   hi2c1.Instance = I2C1;
  165.   hi2c1.Init.ClockSpeed = 100000;
  166.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  167.   hi2c1.Init.OwnAddress1 = 4;
  168.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT;
  169.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  170.   hi2c1.Init.OwnAddress2 = 0;
  171.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  172.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  173.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  174.   {
  175.     _Error_Handler(__FILE__, __LINE__);
  176.   }

  177. }

  178. /* USART2 init function */
  179. static void MX_USART2_UART_Init(void)
  180. {

  181.   huart2.Instance = USART2;
  182.   huart2.Init.BaudRate = 115200;
  183.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  184.   huart2.Init.StopBits = UART_STOPBITS_1;
  185.   huart2.Init.Parity = UART_PARITY_NONE;
  186.   huart2.Init.Mode = UART_MODE_TX_RX;
  187.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  188.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  189.   if (HAL_UART_Init(&huart2) != HAL_OK)
  190.   {
  191.     _Error_Handler(__FILE__, __LINE__);
  192.   }

  193. }

  194. /** Configure pins as
  195.         * Analog
  196.         * Input
  197.         * Output
  198.         * EVENT_OUT
  199.         * EXTI
  200. */
  201. static void MX_GPIO_Init(void)
  202. {

  203.   /* GPIO Ports Clock Enable */
  204.   __HAL_RCC_GPIOH_CLK_ENABLE();
  205.   __HAL_RCC_GPIOA_CLK_ENABLE();
  206.   __HAL_RCC_GPIOB_CLK_ENABLE();

  207. }

  208. /* USER CODE BEGIN 4 */

  209. /* USER CODE END 4 */

  210. /**
  211.   * @brief  This function is executed in case of error occurrence.
  212.   * @param  None
  213.   * @retval None
  214.   */
  215. void _Error_Handler(char * file, int line)
  216. {
  217.   /* USER CODE BEGIN Error_Handler_Debug */
  218.   /* User can add his own implementation to report the HAL error return state */
  219.   while(1)
  220.   {
  221. ……………………

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

所有资料51hei提供下载:
指南针轮询.rar (11.71 MB, 下载次数: 27)


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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