UART Output slow with DMA
I recently wrote some code with the help of stack exchange regarding an MQ2 gas sensor. I needed to alter the code to use DMA as the MCU I'm using for the project only has one ADC channel. My goal was to measure the ADC value of the MQ2 sensor along with a battery voltage for when it drops below 2.5 volts to blink a red LED. I'm noticing that the UART is very slow when outputting numbers, and when I adjust my voltage source voltage it takes a few seconds to update. It only seems faster when the Battery voltage isn't below 2.5V.
I'm not sure exactly what I need to fix with my code but it can be seen below:
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BATLED GPIO_PIN_3
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
UART_HandleTypeDef huart2;
uint16_t ADCValues[2];
uint16_t ADCCount = sizeof (ADCValues)/ sizeof (ADCValues[0]);
uint16_t ADC_MQ2_AVG;
uint16_t ADC_Voltage_AVG;
float Voltage = 0;
float result_ratio;
float Vresult_ratio;
float Ro = 10;
char msg[50];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART2_UART_Init(void);
uint8_t ADCConversionComplete = 0;
static void uart_send_str (const char* msg)
{
HAL_UART_Transmit (&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
ADCConversionComplete = 1;
}
static float ReadSensor (uint32_t sample_count, int pause_ms)
{
uint32_t i;
uint16_t MQ2ADCVAL;
uint32_t ADCsum1 = 0;
for (i = 0; i < sample_count; i++) {
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADCValues,ADCCount);
MQ2ADCVAL = (uint16_t)ADCValues[0];
ADCsum1 += MQ2ADCVAL;
HAL_Delay (pause_ms);
}
ADC_MQ2_AVG = ADCsum1 / sample_count;
return 0;
}
static float BAT_VOLT (uint32_t sample_count, int pause_ms)
{
uint32_t i;
uint16_t ADCVoltage;
uint32_t ADCsum2 = 0;
for (i = 0; i < sample_count; i++) {
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADCValues,ADCCount);
ADCVoltage = (uint16_t)ADCValues[1];
Voltage = (ADCVoltage *3.3) /(4095);
ADCsum2 += ADCVoltage;
HAL_Delay (pause_ms);
}
ADC_Voltage_AVG = ADCsum2/ sample_count;
if(Voltage<2.5){
HAL_GPIO_WritePin(GPIOC,BATLED,GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,BATLED,GPIO_PIN_RESET);
HAL_Delay(500);
}
return 0;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_USART2_UART_Init();
while(1)
{
result_ratio = ReadSensor (5, 50) / Ro;
Vresult_ratio = BAT_VOLT(5,50);
while((ADCConversionComplete == 0))
{
}
sprintf(msg, "MQ2ADCVAL: %hu\tADCVoltage: %hu Voltage: %f\r\n",ADC_MQ2_AVG,ADC_Voltage_AVG,Voltage);
uart_send_str (msg);
}
}
PuTTy terminal:
