Facebook
From david, 11 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 96
  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : 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) 2018 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 "stm32f3xx_hal.h"
  41. #include "i2c.h"
  42. #include "gpio.h"
  43.  
  44. /* USER CODE BEGIN Includes */
  45. #include "LSM303DLHC.h"
  46. #include "math.h"
  47. /* USER CODE END Includes */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50.  
  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53.  unsigned char measurementData[6];
  54.  signed short rawData[3];
  55.  float accData[3],theta,alpha;
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60.  
  61. /* USER CODE BEGIN PFP */
  62. /* Private function prototypes -----------------------------------------------*/
  63.  
  64. /* USER CODE END PFP */
  65.  
  66. /* USER CODE BEGIN 0 */
  67.  
  68. /* USER CODE END 0 */
  69.  
  70. /**
  71.   * @brief  The application entry point.
  72.   *
  73.   * @retval None
  74.   */
  75. int main(void)
  76. {
  77.   /* USER CODE BEGIN 1 */
  78. LSM303DLHCAcc_InitTypeDef acc_init;
  79.   /* USER CODE END 1 */
  80.  
  81.   /* MCU Configuration----------------------------------------------------------*/
  82.  
  83.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  84.   HAL_Init();
  85.  
  86.   /* USER CODE BEGIN Init */
  87.  
  88.   /* USER CODE END Init */
  89.  
  90.   /* Configure the system clock */
  91.   SystemClock_Config();
  92.  
  93.   /* USER CODE BEGIN SysInit */
  94.  
  95.   /* USER CODE END SysInit */
  96.  
  97.   /* Initialize all configured peripherals */
  98.   MX_GPIO_Init();
  99.   MX_I2C1_Init();
  100.   /* USER CODE BEGIN 2 */
  101.   acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_4G;
  102.   acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_50_HZ;
  103.         acc_init.Axes_Enable=LSM303DLHC_AXES_ENABLE;
  104.         acc_init.BlockData_Update=LSM303DLHC_BlockUpdate_Continous;
  105.         acc_init.Endianness=LSM303DLHC_BLE_LSB;
  106.         acc_init.High_Resolution=LSM303DLHC_HR_ENABLE;
  107.         acc_init.Power_Mode=LSM303DLHC_NORMAL_MODE;
  108.        
  109.         LSM303DLHC_AccInit(&acc_init);
  110.   /* USER CODE END 2 */
  111.  
  112.   /* Infinite loop */
  113.   /* USER CODE BEGIN WHILE */
  114.   while (1)
  115.   {
  116. //read acc data measurement, device address – 0x32, reg address 0x28  
  117.                 LSM303DLHC_Read(0x32,0x28,&measurementData[0],6);
  118.                 //join two bytes to create short type variable
  119.                 rawData[0]=(signed short)measurementData[0]+(signed short)measurementData[1]*256;
  120.                 rawData[1]=(signed short)measurementData[2]+(signed short)measurementData[3]*256;
  121.                 rawData[2]=(signed short)measurementData[4]+(signed short)measurementData[5]*256;
  122.                 //scale measurements and express them in g units
  123.                 accData[0]=(float)rawData[0]*(float)0.001/16;
  124.                 accData[1]=(float)rawData[1]*(float)0.001/16;
  125.                 accData[2]=(float)rawData[2]*(float)0.001/16;
  126.                 HAL_Delay(10);
  127.                 theta= asin(accData[0]);
  128.                 alpha = acos((-accData[1])/cos(theta)) ;
  129.                
  130.                
  131.                 if (theta>1){
  132.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
  133.                 HAL_Delay(10);
  134.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
  135.                 }
  136.                 else if (theta<-1){
  137.           HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,GPIO_PIN_RESET);
  138.                 HAL_Delay(10);
  139.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,GPIO_PIN_SET);
  140.                 }
  141.                
  142.                
  143.                 if (alpha>1){
  144.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,GPIO_PIN_RESET);
  145.                 HAL_Delay(10);
  146.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,GPIO_PIN_SET);
  147.                 }
  148.                 else if (alpha<-1){
  149.           HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,GPIO_PIN_RESET);
  150.                 HAL_Delay(10);
  151.                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,GPIO_PIN_SET);
  152.                 }
  153.   /* USER CODE END WHILE */
  154.  
  155.   /* USER CODE BEGIN 3 */
  156.  
  157.   }
  158.   /* USER CODE END 3 */
  159.  
  160. }
  161.  
  162. /**
  163.   * @brief System Clock Configuration
  164.   * @retval None
  165.   */
  166. void SystemClock_Config(void)
  167. {
  168.  
  169.   RCC_OscInitTypeDef RCC_OscInitStruct;
  170.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  171.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  172.  
  173.     /**Initializes the CPU, AHB and APB busses clocks
  174.     */
  175.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  176.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  177.   RCC_OscInitStruct.HSICalibrationValue = 16;
  178.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  179.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  180.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  181.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  182.   {
  183.     _Error_Handler(__FILE__, __LINE__);
  184.   }
  185.  
  186.     /**Initializes the CPU, AHB and APB busses clocks
  187.     */
  188.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  189.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  190.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  191.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  192.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  193.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  194.  
  195.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  196.   {
  197.     _Error_Handler(__FILE__, __LINE__);
  198.   }
  199.  
  200.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  201.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  202.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  203.   {
  204.     _Error_Handler(__FILE__, __LINE__);
  205.   }
  206.  
  207.     /**Configure the Systick interrupt time
  208.     */
  209.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  210.  
  211.     /**Configure the Systick
  212.     */
  213.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  214.  
  215.   /* SysTick_IRQn interrupt configuration */
  216.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  217. }
  218.  
  219. /* USER CODE BEGIN 4 */
  220.  
  221. /* USER CODE END 4 */
  222.  
  223. /**
  224.   * @brief  This function is executed in case of error occurrence.
  225.   * @param  file: The file name as string.
  226.   * @param  line: The line in file as a number.
  227.   * @retval None
  228.   */
  229. void _Error_Handler(char *file, int line)
  230. {
  231.   /* USER CODE BEGIN Error_Handler_Debug */
  232.   /* User can add his own implementation to report the HAL error return state */
  233.   while(1)
  234.   {
  235.   }
  236.   /* USER CODE END Error_Handler_Debug */
  237. }
  238.  
  239. #ifdef  USE_FULL_ASSERT
  240. /**
  241.   * @brief  Reports the name of the source file and the source line number
  242.   *         where the assert_param error has occurred.
  243.   * @param  file: pointer to the source file name
  244.   * @param  line: assert_param error line source number
  245.   * @retval None
  246.   */
  247. void assert_failed(uint8_t* file, uint32_t line)
  248. {
  249.   /* USER CODE BEGIN 6 */
  250.   /* User can add his own implementation to report the file name and line number,
  251.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  252.   /* USER CODE END 6 */
  253. }
  254. #endif /* USE_FULL_ASSERT */
  255.  
  256. /**
  257.   * @}
  258.   */
  259.  
  260. /**
  261.   * @}
  262.   */
  263.  
  264. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  265.