This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to change LIS3DSH resolution

Hello everyone, 

I am new to STM32 and to Keil. I would like to change the accelerometer resolution from 2G to 16G, but due to my inexperience, I don't know how. My teacher didn't help my and said that I need to figure it out by myself, but I really don't know what to do. Can somebody help me what to do a what to change? 

Here is code that teacher provided: 

I will be very thankful is someone can help me. 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>© Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdbool.h>

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */

//PROMENNE
uint8_t adresaRegistru, data;
uint8_t osaXH, osaXL, osaYH, osaYL, osaZH, osaZL; //Pro spodní a horni byty os
int16_t osaX, osaY, osaZ; //Pro spojeni hornich a dolnich bytu
bool bouchnuti;
volatile float zrychleniX, zrychleniY, zrychleniZ; //Pro prepocet do g
volatile uint32_t ADCValue=0;
char kTiskuAD[10]= {0};

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

	
	//Preruseni od AD prevodniku
	void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1)
	{
		ADCValue = HAL_ADC_GetValue(hadc1);
 		HAL_ADC_Start_IT(hadc1);
	}
	

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
	
	//-----------------------------NASTAVENI AKCELEROMETRU-------------------------
  //Defaultne je rozlišení +-2g
  //SPI Komunikace
  HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET); //Chip select
  adresaRegistru = 0x20; //CTRL_REG4
  HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50); 
  data = 0x37; //povol osy, 12,5Hz na vystupu
  HAL_SPI_Transmit(&hspi1, &data, 1, 50);
  HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET); //Chip select
  HAL_Delay(20);
	
	//spustime prvni AD prevod
	HAL_ADC_Start_IT(&hadc1);
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		//CTENI VYSSIHO BYTU X
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET); //Chip select
		adresaRegistru = 0x29+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaXH, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET); //Chip select
		//CTENI NIZSIHO BYTU X
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET); //Chip select
		adresaRegistru = 0x28+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaXL, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET); //Chip select
		//CTENI VYSSIHO BYTU Y
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET);
		adresaRegistru = 0x2B+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaYH, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET);
		//CTENI NIZSIHO BYTU Y
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET); //Chip select
		adresaRegistru = 0x2A+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaYL, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET); //Chip select
		//CTENI VYSSIHO BYTU Z
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET);
		adresaRegistru = 0x2D+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaZH, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET);
		//CTENI NIZSIHO BYTU Z
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_RESET);
		adresaRegistru = 0x2C+0x80;
		HAL_SPI_Transmit(&hspi1, &adresaRegistru, 1, 50);
		HAL_SPI_Receive(&hspi1, &osaZL, 1, 50);
		HAL_GPIO_WritePin(GPIOE, ACC_CS_Pin, GPIO_PIN_SET);
		
		//Do 16bitovyho cisla
		osaX = ((int16_t)osaXH <<8) | osaXL;
		osaY = ((int16_t)osaYH <<8) | osaYL;
		osaZ = ((int16_t)osaZH <<8) | osaZL;
		
		//PRepocitej to g
		zrychleniX = ((2.0/65535.0) * osaX)*2;
		zrychleniY = ((2.0/65535.0) * osaY)*2;
		zrychleniZ = ((2.0/65535.0) * osaZ)*2;
		
		if(zrychleniY <= -0.015 && zrychleniX >= 0.01){
			bouchnuti = !bouchnuti;
		}
		
		
		if(bouchnuti == true){
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_SET);
		}
		else{
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
		}
		
		

		
		//vycteni hodnot z AD
		//while(HAL_ADC_PollForConversion(&hadc1, 1000000) != HAL_OK) ;
		//ADCValue = HAL_ADC_GetValue(&hadc1);
		//HAL_ADC_Start(&hadc1);

		
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

/**
  * @brief SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(ACC_CS_GPIO_Port, ACC_CS_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

  /*Configure GPIO pin : ACC_CS_Pin */
  GPIO_InitStruct.Pin = ACC_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(ACC_CS_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/