Facebook
From Rude Stork, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 157
  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) 2020 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.         unsigned char measurementData[6];
  52. signed short rawData[3];
  53. float accData[3];
  54. float teta;
  55. float fi;
  56. float xfi;
  57. float xteta;
  58.  
  59. signed short magData[3];
  60.        
  61. /* USER CODE BEGIN PV */
  62. /* Private variables ---------------------------------------------------------*/
  63.  
  64. /* USER CODE END PV */
  65.  
  66. /* Private function prototypes -----------------------------------------------*/
  67. void SystemClock_Config(void);
  68.  
  69. /* USER CODE BEGIN PFP */
  70. /* Private function prototypes -----------------------------------------------*/
  71.  
  72. /* USER CODE END PFP */
  73.  
  74. /* USER CODE BEGIN 0 */
  75.  
  76. /* USER CODE END 0 */
  77.  
  78. /**
  79.   * @brief  The application entry point.
  80.   *
  81.   * @retval None
  82.   */
  83. int main(void)
  84. {
  85.   /* USER CODE BEGIN 1 */
  86.         LSM303DLHCAcc_InitTypeDef acc_init;
  87.         LSM303DLHCMag_InitTypeDef mag_init;
  88.   /* USER CODE END 1 */
  89.  
  90.   /* MCU Configuration----------------------------------------------------------*/
  91.  
  92.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  93.   HAL_Init();
  94.  
  95.   /* USER CODE BEGIN Init */
  96.  
  97.   /* USER CODE END Init */
  98.  
  99.   /* Configure the system clock */
  100.   SystemClock_Config();
  101.  
  102.   /* USER CODE BEGIN SysInit */
  103.  
  104.   /* USER CODE END SysInit */
  105.  
  106.   /* Initialize all configured peripherals */
  107.   MX_GPIO_Init();
  108.   MX_I2C1_Init();
  109.   /* USER CODE BEGIN 2 */
  110.         unsigned char recData[10];
  111.        
  112.   HAL_I2C_Master_Receive(&hi2c1,0x31,recData,10,100);
  113.        
  114.         HAL_I2C_Mem_Read(&hi2c1,0x31,0x10,1,recData,10,100);
  115.        
  116.         acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_2G;
  117.         acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_10_HZ;
  118.         acc_init.Axes_Enable=LSM303DLHC_AXES_ENABLE;
  119.         acc_init.BlockData_Update=LSM303DLHC_BlockUpdate_Continous;
  120.         acc_init.Endianness=LSM303DLHC_BLE_LSB;
  121.         acc_init.High_Resolution=LSM303DLHC_HR_ENABLE;
  122.         acc_init.Power_Mode=LSM303DLHC_NORMAL_MODE;
  123.        
  124.        
  125.         LSM303DLHC_AccInit(&acc_init);
  126.        
  127.         mag_init.MagFull_Scale=LSM303DLHC_FS_1_3_GA;
  128.         mag_init.MagOutput_DataRate=LSM303DLHC_MAG_ODR_7_5_HZ;
  129.         mag_init.Temperature_Sensor=LSM303DLHC_TEMPSENSOR_ENABLE;
  130.         mag_init.Working_Mode=LSM303DLHC_CONTINUOS_CONVERSION;
  131.        
  132.         LSM303DLHC_MagInit(&mag_init);
  133.   /* USER CODE END 2 */
  134.  
  135.   /* Infinite loop */
  136.   /* USER CODE BEGIN WHILE */
  137.   while (1)
  138.   {
  139.                 HAL_Delay(100);
  140.                
  141.                 LSM303DLHC_Read(0x32,0x28 | 0x80,&measurementData[0],6);
  142.                
  143.                 rawData[0]=(signed short)measurementData[0]+(signed short)measurementData[1]*256;
  144.                 rawData[1]=(signed short)measurementData[2]+(signed short)measurementData[3]*256;
  145.                 rawData[2]=(signed short)measurementData[4]+(signed short)measurementData[5]*256;
  146.                 accData[0]=(float)rawData[0]*(float)0.001/16;
  147.                 accData[1]=(float)rawData[1]*(float)0.001/16;
  148.                 accData[2]=(float)rawData[2]*(float)0.001/16;
  149.                
  150.                 teta=asin(accData[0]);
  151.                 fi=asin(-accData[1]/(1.0*cos(teta)));
  152.                
  153.                 teta=teta*(180/3.14);
  154.                 fi=fi*(180/3.14);
  155.                
  156.                 if (fi>1)
  157.                 {
  158.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_SET);
  159.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  160.                 }
  161.                 else if (fi<-1)
  162.                         {
  163.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET);
  164.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  165.                 }
  166.                                 else if (-1<fi && fi<1)
  167.                         {
  168.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  169.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  170.                 }
  171.                 if (teta>1)
  172.                 {
  173.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9, GPIO_PIN_SET);
  174.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  175.                 }
  176.                                 else if (teta<-1)
  177.                         {
  178.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_SET);
  179.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9, GPIO_PIN_RESET);
  180.                 }
  181.                                 else if (-1<teta && teta<1)
  182.                         {
  183.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  184.                         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9, GPIO_PIN_RESET);
  185.                 }
  186.  
  187.   /* USER CODE END WHILE */
  188.  
  189.   /* USER CODE BEGIN 3 */
  190.        
  191.                        
  192.                  LSM303DLHC_Read(0x3C,0x03 | 0x80,&measurementData[0],6);
  193.                
  194.                 magData[0]=(float)rawData[0]/LSM303DLHC_M_SENSITIVITY_XY_1_3Ga;
  195.                 magData[1]=(float)rawData[1]/LSM303DLHC_M_SENSITIVITY_XY_1_3Ga;
  196.                 magData[2]=(float)rawData[2]/LSM303DLHC_M_SENSITIVITY_Z_1_3Ga;
  197.   }
  198.   /* USER CODE END 3 */
  199.  
  200. }
  201.  
  202. /**
  203.   * @brief System Clock Configuration
  204.   * @retval None
  205.   */
  206. void SystemClock_Config(void)
  207. {
  208.  
  209.   RCC_OscInitTypeDef RCC_OscInitStruct;
  210.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  211.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  212.  
  213.     /**Initializes the CPU, AHB and APB busses clocks
  214.     */
  215.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  216.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  217.   RCC_OscInitStruct.HSICalibrationValue = 16;
  218.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  219.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  220.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  221.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  222.   {
  223.     _Error_Handler(__FILE__, __LINE__);
  224.   }
  225.  
  226.     /**Initializes the CPU, AHB and APB busses clocks
  227.     */
  228.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  229.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  230.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  231.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  232.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  233.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  234.  
  235.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  236.   {
  237.     _Error_Handler(__FILE__, __LINE__);
  238.   }
  239.  
  240.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  241.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  242.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  243.   {
  244.     _Error_Handler(__FILE__, __LINE__);
  245.   }
  246.  
  247.     /**Configure the Systick interrupt time
  248.     */
  249.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  250.  
  251.     /**Configure the Systick
  252.     */
  253.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  254.  
  255.   /* SysTick_IRQn interrupt configuration */
  256.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  257. }
  258.  
  259. /* USER CODE BEGIN 4 */
  260.  
  261. /* USER CODE END 4 */
  262.  
  263. /**
  264.   * @brief  This function is executed in case of error occurrence.
  265.   * @param  file: The file name as string.
  266.   * @param  line: The line in file as a number.
  267.   * @retval None
  268.   */
  269. void _Error_Handler(char *file, int line)
  270. {
  271.   /* USER CODE BEGIN Error_Handler_Debug */
  272.   /* User can add his own implementation to report the HAL error return state */
  273.   while(1)
  274.   {
  275.   }
  276.   /* USER CODE END Error_Handler_Debug */
  277. }
  278.  
  279. #ifdef  USE_FULL_ASSERT
  280. /**
  281.   * @brief  Reports the name of the source file and the source line number
  282.   *         where the assert_param error has occurred.
  283.   * @param  file: pointer to the source file name
  284.   * @param  line: assert_param error line source number
  285.   * @retval None
  286.   */
  287. void assert_failed(uint8_t* file, uint32_t line)
  288. {
  289.   /* USER CODE BEGIN 6 */
  290.   /* User can add his own implementation to report the file name and line number,
  291.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  292.   /* USER CODE END 6 */
  293. }
  294. #endif /* USE_FULL_ASSERT */
  295.  
  296. /**
  297.   * @}
  298.   */
  299.  
  300. /**
  301.   * @}
  302.   */
  303.  
  304. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  305.