2

I want to able to send my ADC values to a Nodemcu to put them a Web UI but I even could not send any UART value to a serial port and I did not catch the problem.

I set my variables and libs like:

#include "stdio.h"
#include "string.h"

uint8_t convEnd = 0;
uint32_t adcBuff[3];        
uint32_t adcData[3];

After this, I read my ADC values by using DMA Method, I set a flag to turn 1 when the conversion done.

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{   
    if (hadc->Instance == ADC1) {
    adcData[0] = adcBuff[0];
    adcData[1] = adcBuff[1];
    adcData[2] = adcBuff[2];
     convEnd = 1; 
    }     
}

Next, I started DMA and put this code to my while (1) loop,

  while (1) {
      if (convEnd == 1) {       // check is conv completed
          char txt[30];

          sprintf(txt, "ADC Val: %d %d\n\r", adcData[0], adcData[1]);           
          HAL_UART_Transmit(&huart2, (uint8_t *)txt, strlen(txt), 100);     
          HAL_Delay(500);                               
          convEnd = 0;
      }
  }

As a result, I can read ADC correctly but I can not send these values via UART to any Serial Port.

0andriy
  • 4,183
  • 1
  • 24
  • 37
  • Can you edit your question to show the code that sets up the UART? Maybe you have flow control enabled when it should not be. Also make sure that whatever is on the other end of the link is configured for the same baud rate, parity and stop bits. – pmacfarlane Mar 11 '23 at 13:17
  • Can you send arbitrary UART message with current UART configuration? Does it "hello world"? – Ilya Mar 11 '23 at 13:42
  • Yes all of the baudrates are same, and ı can send "hello world" with UART in same code. – MrElectronicEngineer Mar 11 '23 at 14:47
  • Are you sure strlen returns expected value? You have unintialized array on stack – Ilya Mar 11 '23 at 14:51
  • What do you offer me for that – MrElectronicEngineer Mar 11 '23 at 14:54
  • `convEnd` should probably be `volatile` since it can be modified from an ISR. The compiler might consider that you set `convEnd = 0` at the end of your loop, and since the loop then tests `if (convEnv == 1)` it could eliminate further tests of `convEnv`, i.e. convert your code to a single test, then an empty infinite loop. – pmacfarlane Mar 11 '23 at 15:13
  • Is your ADC DMA set to run continuously (i.e. circular-buffer mode)? If not, it will run once and you don't appear to re-start it. – pmacfarlane Mar 11 '23 at 15:20
  • the funny thing is when ı stop dma, ı can sent anything. DMA and UART does not work together in my code block – MrElectronicEngineer Mar 11 '23 at 15:53
  • What's your sample rate? If it is fast enough, it is possible the CPU spends all its time running the ISR and your main loop never has time to run. Try lowering the sample rate (a lot, to start with) and see if anything changes. – pmacfarlane Mar 11 '23 at 20:49

0 Answers0