Facebook
From Unreliable Hog, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 263
  1.  
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   ** This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * COPYRIGHT(c) 2018 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f3xx_hal.h"
  42. #include "i2c.h"
  43. #include "gpio.h"
  44.  
  45. /* USER CODE BEGIN Includes */
  46. #include "LSM303DLHC.h"
  47. #include "math.h"
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51.  
  52. /* USER CODE BEGIN PV */
  53. /* Private variables ---------------------------------------------------------*/
  54. LSM303DLHCAcc_InitTypeDef acc_init;
  55. unsigned char measurementData[6];
  56. signed short rawData[3];
  57. float accData[3];
  58.  
  59. /* USER CODE END PV */
  60.  
  61. /* Private function prototypes -----------------------------------------------*/
  62. void SystemClock_Config(void);
  63.  
  64. /* USER CODE BEGIN PFP */
  65. /* Private function prototypes -----------------------------------------------*/
  66.  
  67. /* USER CODE END PFP */
  68.  
  69. /* USER CODE BEGIN 0 */
  70.  
  71. /* USER CODE END 0 */
  72.  
  73. /**
  74.   * @brief  The application entry point.
  75.   *
  76.   * @retval None
  77.   */
  78. int main(void)
  79. {
  80.   /* USER CODE BEGIN 1 */
  81. LSM303DLHCAcc_InitTypeDef acc_init;
  82.   /* USER CODE END 1 */
  83.  
  84.   /* MCU Configuration----------------------------------------------------------*/
  85.  
  86.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87.   HAL_Init();
  88.  
  89.   /* USER CODE BEGIN Init */
  90.  
  91.   /* USER CODE END Init */
  92.  
  93.   /* Configure the system clock */
  94.   SystemClock_Config();
  95.  
  96.   /* USER CODE BEGIN SysInit */
  97.  
  98.   /* USER CODE END SysInit */
  99.  
  100.   /* Initialize all configured peripherals */
  101.   MX_GPIO_Init();
  102.   MX_I2C1_Init();
  103.   /* USER CODE BEGIN 2 */
  104.   acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_4G;
  105.   acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_50_HZ;
  106.         acc_init.Axes_Enable=LSM303DLHC_AXES_ENABLE;
  107.         acc_init.BlockData_Update=LSM303DLHC_BlockUpdate_Continous;
  108.         acc_init.Endianness=LSM303DLHC_BLE_LSB;
  109.         acc_init.High_Resolution=LSM303DLHC_HR_ENABLE;
  110.         acc_init.Power_Mode=LSM303DLHC_NORMAL_MODE;
  111.        
  112.         LSM303DLHC_AccInit(&acc_init);
  113.   /* USER CODE END 2 */
  114.  
  115.   /* Infinite loop */
  116.   /* USER CODE BEGIN WHILE */
  117.   while (1)
  118.   {
  119.  
  120. //read acc data measurement, device address – 0x32, reg address 0x28
  121. LSM303DLHC_Read(0x32,0x28,&measurementData[0],6);
  122. //join two bytes to create short type variable
  123. rawData[0]=(signed short)measurementData[0]+(signed short)measurementData[1]*256;
  124. rawData[1]=(signed short)measurementData[2]+(signed short)measurementData[3]*256;
  125. rawData[2]=(signed short)measurementData[4]+(signed short)measurementData[5]*256;
  126. //scale measurements and express them in g units
  127. accData[0]=(float)rawData[0]*(float)0.001/16;
  128. accData[1]=(float)rawData[1]*(float)0.001/16;
  129. accData[2]=(float)rawData[2]*(float)0.001/16;
  130.  
  131.  
  132.                 }
  133.   /* USER CODE END WHILE */
  134.  
  135.   /* USER CODE BEGIN 3 */
  136.  
  137.   }
  138.   /* USER CODE END 3 */
  139.  
  140. }
  141.  
  142. /**
  143.   * @brief System Clock Configuration
  144.   * @retval None
  145.   */
  146. void SystemClock_Config(void)
  147. {
  148.  
  149.   RCC_OscInitTypeDef RCC_OscInitStruct;
  150.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  151.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  152.  
  153.     /**Initializes the CPU, AHB and APB busses clocks
  154.     */
  155.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  156.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  157.   RCC_OscInitStruct.HSICalibrationValue = 16;
  158.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  159.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  160.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  161.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  162.   {
  163.     _Error_Handler(__FILE__, __LINE__);
  164.   }
  165.  
  166.     /**Initializes the CPU, AHB and APB busses clocks
  167.     */
  168.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  169.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  170.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  171.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  172.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  173.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  174.  
  175.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  176.   {
  177.     _Error_Handler(__FILE__, __LINE__);
  178.   }
  179.  
  180.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  181.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  182.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  183.   {
  184.     _Error_Handler(__FILE__, __LINE__);
  185.   }
  186.  
  187.     /**Configure the Systick interrupt time
  188.     */
  189.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  190.  
  191.     /**Configure the Systick
  192.     */
  193.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  194.  
  195.   /* SysTick_IRQn interrupt configuration */
  196.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  197. }
  198.  
  199. /* USER CODE BEGIN 4 */
  200.  
  201. /* USER CODE END 4 */
  202.  
  203. /**
  204.   * @brief  This function is executed in case of error occurrence.
  205.   * @param  file: The file name as string.
  206.   * @param  line: The line in file as a number.
  207.   * @retval None
  208.   */
  209. void _Error_Handler(char *file, int line)
  210. {
  211.   /* USER CODE BEGIN Error_Handler_Debug */
  212.   /* User can add his own implementation to report the HAL error return state */
  213.   while(1)
  214.   {
  215.   }
  216.   /* USER CODE END Error_Handler_Debug */
  217. }
  218.  
  219. #ifdef  USE_FULL_ASSERT
  220. /**
  221.   * @brief  Reports the name of the source file and the source line number
  222.   *         where the assert_param error has occurred.
  223.   * @param  file: pointer to the source file name
  224.   * @param  line: assert_param error line source number
  225.   * @retval None
  226.   */
  227. void assert_failed(uint8_t* file, uint32_t line)
  228. {
  229.   /* USER CODE BEGIN 6 */
  230.   /* User can add his own implementation to report the file name and line number,
  231.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  232.   /* USER CODE END 6 */
  233. }
  234. #endif /* USE_FULL_ASSERT */
  235.  
  236. /**
  237.   * @}
  238.   */
  239.  
  240. /**
  241.   * @}
  242.   */
  243.  
  244. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  245.  
  246.