AnsweredAssumed Answered

Getting adc value greter than maximum possible value of adc.

Question asked by zaidi.uzair on Jan 6, 2015
I am using STM32F3-discovery board. I want to calculate value of all three axis of accelerometer and send it to UART. For this i edit demo code of adc (BSP) and include UART code in it. Now problem is my x-axis value and y-axis values are coming around 80 and -640 respectively when out in no motion condition . But my z-axis values is around 1560. and according to datasheet of my board , adc registers is of 12 bit. So, it should not be greater than 4096. But if plot graph of the receiving values, it looks fine according to motion. So, can any one tell me why I am getting such big value of z-axis?....Here is my code:-



/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f3_discovery.h"
#include "stm32f3xx_hal.h"
#include "stm32f3_discovery_gyroscope.h"
#include "stm32f3_discovery_accelerometer.h"
#include "mems.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>




UART_HandleTypeDef UartHandle;


/* Buffer used for transmission */
uint8_t aTxBuffer[] = "A";


/* Buffer used for reception */
uint8_t aRxBuffer[RXBUFFERSIZE];


/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
/////static void Error_Handler(void);
static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);




uint8_t DemoIndex = 0;




__IO uint8_t UserPressButton = 0;


/* Counter for User button presses*/
__IO uint32_t PressCount = 0;


/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);




extern __IO uint8_t UserPressButton;
/* Init af threahold to detect acceleration on MEMS */
int16_t ThresholdHigh = 1000;
int16_t ThresholdLow = -1000;


/* Private function prototypes -----------------------------------------------*/
static void ACCELERO_ReadAcc(void);


/* Private functions ---------------------------------------------------------*/
void ACCELERO_MEMS_Test(void)
  {
  /* Init Accelerometer Mems */
  if(BSP_ACCELERO_Init() != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  UserPressButton = 0;
  while(!UserPressButton)
  {
    ACCELERO_ReadAcc();
  }
}  


static void ACCELERO_ReadAcc(void)
{
  int16_t buffer[3] = {0};
  int16_t xval;
    int16_t yval;
    int16_t zval;// = 0x00;
    int16_t  fxval[10],fyval[10],fzval[10];    
  /* Read Acceleration*/
  BSP_ACCELERO_GetXYZ(buffer);
 
  /* Update autoreload and capture compare registers value*/
    for(int i =0;i<10;i++)
    {
  fxval[i] = buffer[0]-48;
  fyval[i] = buffer[1]-48;
  fzval[i] = buffer[2]-48;
    }
    xval = (fxval[0]+fxval[1]+fxval[2]+fxval[3]+fxval[4]+fxval[5]+fxval[6]+fxval[7]+fxval[8]+fxval[9])/10;
  yval = (fyval[0]+fyval[1]+fyval[2]+fyval[3]+fyval[4]+fyval[5]+fyval[6]+fyval[7]+fyval[8]+fyval[9])/10;
    zval = (fzval[0]+fzval[1]+fzval[2]+fzval[3]+fzval[4]+fzval[5]+fzval[6]+fzval[7]+fzval[8]+fzval[9])/10;
    char a[]= "x";    
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)a, strlen(a), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
    char y[100];
    int t =xval;
    sprintf(y,"%d",t);
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)y, strlen(y), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
    int q;


////filter(xval);
     char b[]= "y";    
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)b, strlen(b), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
    
    t =yval;
    sprintf(y,"%d",t);
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)y, strlen(y), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
    char c[]= "z";    
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)c, strlen(c), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
      t =zval;
    sprintf(y,"%d",t);
    if(HAL_UART_Transmit(&UartHandle, (uint8_t*)y, strlen(y), 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
  if((ABS(xval))>(ABS(yval)))
  {
    if(xval > ThresholdHigh)
    { 
      /* LED10 On */
      BSP_LED_On(LED10);
      HAL_Delay(10);
    }
    else if(xval < ThresholdLow)
    { 
      /* LED3 On */
      BSP_LED_On(LED3);
      HAL_Delay(10);
    }
    else
    { 
      HAL_Delay(10);
    }
        
  }
  else
  {
    if(yval < ThresholdLow)
    {
      /* LED7 On */
      BSP_LED_On(LED7);
      HAL_Delay(10);
    }
    else if(yval > ThresholdHigh)
    {
      /* LED6 On */
      BSP_LED_On(LED6);
      HAL_Delay(10);
    } 
    else
  {        
      HAL_Delay(10);
    }
  } 
  
     BSP_LED_Off(LED3);
     BSP_LED_Off(LED6);
     BSP_LED_Off(LED7);
     BSP_LED_Off(LED4);
     BSP_LED_Off(LED10);
     BSP_LED_Off(LED8);
     BSP_LED_Off(LED9);
     BSP_LED_Off(LED5);
}




int main(void)
{
 
  HAL_Init();
  
  /* Configure the system clock to 72 Mhz */
  SystemClock_Config();
    UserPressButton = 1;
  
  /* Initialize LEDs and User_Button on STM32F3-Discovery ------------------*/
  BSP_LED_Init(LED4);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED5);
  BSP_LED_Init(LED7);
  BSP_LED_Init(LED9);
  BSP_LED_Init(LED10);
  BSP_LED_Init(LED8);
  BSP_LED_Init(LED6);


 /////// BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI); 
  
  /* Toggle LEDs between each Test */
  /*while (!UserPressButton) Toggle_Leds();
  BSP_LED_Off(LED3);
  BSP_LED_Off(LED4);
  BSP_LED_Off(LED5);
  BSP_LED_Off(LED6);*/


  /* 1. Start Test: Wait For User inputs -------------------------------------*/
  while (1)
  {
   
   //// BSP_examples[DemoIndex++].DemoFunc();
        
    
    
         UartHandle.Instance        = USARTx;


  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
        
  {   
    Error_Handler();
  }
    
//uint8_t aTxBuffer[] = "A";
    
   /* if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
  {
    Error_Handler();  
  }*/
// 


//  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
//  {
//    Error_Handler();   
//  }
    


    ACCELERO_MEMS_Test();
    
}
    
          
}




static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;


  #ifdef USE_FULL_ASSERT
  uint32_t ret = HAL_OK;
  #endif /* USE_FULL_ASSERT */
  
  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;


#ifdef USE_FULL_ASSERT
  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  if(ret != HAL_OK)
  {
    assert_failed((uint8_t *)__FILE__, __LINE__);
  }
#else
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
#endif /* USE_FULL_ASSERT */
        
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;


#ifdef USE_FULL_ASSERT
  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  if(ret != HAL_OK)
  {
    assert_failed((uint8_t *)__FILE__, __LINE__);
  }
#else
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
#endif /* USE_FULL_ASSERT */
}


/**
  * @brief  EXTI line detection callbacks.
  * @param  GPIO_Pin: Specifies the pins connected EXTI line
  * @retval None
  */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if (USER_BUTTON_PIN == GPIO_Pin)
  {
    while (BSP_PB_GetState(BUTTON_USER) != RESET);
    UserPressButton = 1;
  } 
}


/**
  * @brief Toggle Leds
  * @param  None
  * @retval None
  */
void Toggle_Leds(void)
{
    BSP_LED_Toggle(LED3);
    HAL_Delay(100);
    BSP_LED_Toggle(LED4);
    HAL_Delay(100);
    BSP_LED_Toggle(LED6);
    HAL_Delay(100);
    BSP_LED_Toggle(LED8);
    HAL_Delay(100);
    BSP_LED_Toggle(LED10);
    HAL_Delay(100);
    BSP_LED_Toggle(LED9);
    HAL_Delay(100);
    BSP_LED_Toggle(LED7);
    HAL_Delay(100);
    BSP_LED_Toggle(LED5);
    HAL_Delay(100);
}




void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
  /* Turn LED3 on: Transfer error in reception/transmission process */
  BSP_LED_On(LED3); 
}


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
    /* Turn LED10/3 (RED) on */
    BSP_LED_On(LED10);
    BSP_LED_On(LED3);
    while(1)
 {
 }
}


#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 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) */


  /* Infinite loop */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */




Outcomes