久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3217|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲每日更新 | 国产精品视频久久 | 岛国av免费在线观看 | 九九热在线免费视频 | 久久午夜视频 | 一区2区| 欧美视频xxx| 最近中文字幕在线视频1 | 欧美精品一区二区三区在线播放 | 一区二区在线免费观看视频 | 欧美黑人一级爽快片淫片高清 | 久久久国产一区 | 91麻豆产精品久久久久久夏晴子 | 成年视频在线观看福利资源 | 免费黄色网址视频 | 欧美a区| 一区二区三区精品视频 | 日本三级网址 | 日韩av成人 | 久久精品国产久精国产 | 91久久久久久久久久久 | 国产一区二区三区色淫影院 | 欧美精品久久久久 | 射欧美| 九九热这里只有精品在线观看 | 久久久久久综合 | 国产视频线观看永久免费 | 亚洲国产精品成人综合久久久 | 久久中文字幕视频 | 精品国产一区二区国模嫣然 | 国产美女自拍视频 | 一级一级一级毛片 | 久久黄色 | 亚洲第一av网站 | 国产成人精品一区二区三区 | 一区二区三区四区电影 | 国产天天操 | 欧美一区二区成人 | 欧美四虎| 日日爱视频 | 色久影院 |