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

Introducing loop stops HAL_ADC_GetValue from working properly?

Hi, this is my first time posting in a tech forum and I am very new to all of this and learning it all on my own - any help is greatly appreciated. I'm using an STM32F446RE to try to develop a pair of noise-cancelling headphones, and my first step in doing this was to continuously read in ADC values. I got this working totally fine, and the data made sense.

However, I run into trouble when I try to put these values into an array by iterating backwards through the loop to shift the values to the right. Here is a portion of my code (apologies if it's too much to post):


/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "stdint.h"
#include "arm_math.h"



/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

DAC_HandleTypeDef hdac;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

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

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
uint32_t adcVal;
int buffSize;
uint32_t adcArray[128];
uint32_t testVal;
void delay(unsigned int);
uint32_t countVal;
/* USER CODE END 0 */


int main(void)
{
  /* USER CODE BEGIN 1 */
  buffSize = 128;
  countVal = 0;

  // Initialize array
  for (int i = 0; i < buffSize - 1; i = i + 1)
  {
    adcArray[i] = 0;
  }

  /* 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_ADC1_Init();
  MX_DAC_Init();
  /* USER CODE BEGIN 2 */
  HAL_ADC_Start(&hadc1);
  /* USER CODE END 2 */

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

    // Get ADC value, store it in first index of array
    adcArray[0] = HAL_ADC_GetValue(&hadc1);
    countVal = countVal + 1;

    // Shift array
    for (int j = buffSize - 1; j > 0; j = j - 1)
    {
        adcArray[j] = adcArray[j - 1];
    }

    testVal = adcArray[127];

  }


}

I put adcArray[0], countVal, and testVal in Watch 1. What happens when I run this code is that adcArray[0] assumes some normal value from the ADC, and the countVal continuously increases, but adcArray[0] then stays the same and does not seem to update. If, however, I remove the Shift Array loop and run the program, adcArray[0] updates continuously as is expected.

I'm not sure exactly what's going on here, and I'm not sure how to google search it. Also the debugger has been giving me lots of problems and doesn't seem to work as I expect it would (it doesn't run quite like the MATLAB debugger). So I hope someone can help. Thanks

0