AnsweredAssumed Answered

Problem with ADC Multi-channel DMA

Question asked by fan zhang on Dec 4, 2017
Latest reply on Dec 7, 2017 by fan zhang

Hello,

 

I am using ADC multi-channel DMA on stm32f105rc to receive some data on 3 adc channels. Ideally I would see 3 values with not much variations on my PC display. However, values I retrieved from the DMA were changing all the time and I do not know what goes wrong. Here is what I have for the code, I basically use cubeMX to generate the code and make a few modification on it. The idea is to retrieve the adc data by using DMA and transmit to PC terminal by UART.

 

  

#include "main.h"
#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */
#define LED_1 GPIO_PIN_12
#define SW_ENABLE_3V3 GPIO_PIN_1
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint32_t buff_rx1[3];
uint32_t adc[3];
int len0, len1, i;

char header[100];
/* USER CODE END PV */

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

 

 

int main(void)
{

HAL_Init();

SystemClock_Config();


MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();


while (1)
{

HAL_GPIO_WritePin(GPIOB, SW_ENABLE_3V3, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)buff_rx1, 3);
HAL_ADC_Start_IT(&hadc1);

HAL_GPIO_WritePin(GPIOB, SW_ENABLE_3V3, GPIO_PIN_SET);

sprintf(header, "%lu %lu %lu data done\r\n", (unsigned long)buff_rx1[0], (unsigned long)buff_rx1[1], (unsigned long)buff_rx1[2]);
len0 = strlen(header);
HAL_UART_Transmit(&huart1, header, len0, 500);
HAL_Delay(1500);
HAL_GPIO_TogglePin(GPIOC,LED_1);


}


}

 

/************************************/

I set 3 adc channels with sampling time of 1.5 cycle. The output value looks like:

783 560 378 data done
783 755 2565 data done
775 644 2326 data done
776 2564 382 data done
2638 2633 462 data done
775 634 408 data done
783 2564 373 data done
776 732 2553 data done
2637 2569 410 data done
776 522 365 data done
776 2620 437 data done
2636 2563 402 data done

 

Thanks for your help.

Outcomes