Skip to main content
PAbdu
Associate II
January 23, 2019
Question

12bit ADC output over USART on STM32

  • January 23, 2019
  • 3 replies
  • 8622 views

My goal: I'm trying to read two ADC pins with 12 bit resolution and output the values through DMA over USART.

I can see that the ADC values are being read in the debug tab in STM32CubeMX. I can "talk" over USART to my serial capture program by having it print "Hello World". However, I cannot get the ADC values to be sent over USART. Part of the problem might be that I need to convert a uint32 to a uint_8 for the USART, although I thought had solved that (with help), but now I'm not so sure. I have a lot of commented out sections of code but would really appreciate someone advice on what I'm doing wrong. The Baud rate on the STM32 and serial monitor are both at 115200.

I attached a zip but I think the issue is likely with the UART transmit part of code:

uint32_t adcVal[2]; /*array to store ADC values*/
 
HAL_UART_Transmit(&huart2,(void *)adcVal, sizeof(adcVal),10); // Sends 4 bytes, binary data

This topic has been closed for replies.

3 replies

Uwe Bonnes
Chief
January 23, 2019

Transmitting binary data over UART needs much thought and a good protocol. Print the ADC values as numbers and send via UART.

PAbdu
PAbduAuthor
Associate II
January 23, 2019

Is there a tutorial anywhere you might suggest? I've watched 5 or 6 videos on YouTube and read through a few and my programming skills are pretty limited.

MNapi
Senior II
January 23, 2019

The problem with STM is that they do not give you any sample programs to learn. Arduino you just google all is there.

enable DMA is Cube Mx

and try something simple first

declare you data

uint8_t data[2];

and try

HAL_UART_Transmit(&huart2, data, 2, 10);

2 is the data array size

10 is 10 ms timeout

PAbdu
PAbduAuthor
Associate II
January 23, 2019

Thanks for taking the time to respond. I get the same results using that as I did with my original code. It prints "null". I don't currently have a potentiometer connected the the ADC pins, I'm just trying to get the code working before I try this on a different board with the same processor that will have states on the pin. STM definitely makes you work for it, you're exactly right about the Arduino world, that's the place I'm used to but I have to get this working on this chip :)

0690X000006DDUkQAO.png

PAbdu
PAbduAuthor
Associate II
January 23, 2019

Output of ADC in the debugging screen:

0690X000006DDUzQAO.png

PAbdu
PAbduAuthor
Associate II
January 24, 2019

Thanks everyone for all the help! I managed to get my ADC values printed out. I still have a couple questions.

1) That are the letters to the right on the numbers? They seem to be related to my \t and \n commands to tab and print a new line in the UART code.0690X000006DEP7QAO.png

HAL_UART_Transmit(&huart2, (void *)str, sprintf(str, "%d\t", adcVal[0]), 10);			 
HAL_UART_Transmit(&huart2, (void *)str, sprintf(str, "%d\r\n", adcVal[1]), 10);		
 

2) When I print my ADC values in the debug tab, they appear to be close but slightly different then the values printed over UART, why might this be? I put a delay in the uart print so I can compare them but really didn't think they would every be different. One thing I need to check is that I'm reading ADC2_IN14 on PC4 and ADC_IN15 on PC5, which I think correspond the picture below. I know I initialized them correctly in CubeMX but not 100% sure I'm using them correctly.

ADC_HandleTypeDef hadc2;
DMA_HandleTypeDef hdma_adc2;
 
TIM_HandleTypeDef htim2;
 
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;
 
uint32_t adcVal[2];
 
HAL_ADC_Start_DMA(&hadc2, adcVal, 2); 

Full code is attached in a zip too.

Tesla DeLorean
Guru
January 24, 2019

>> why might this be?

Because the values in the arrays are changing in the background faster than you can print them?

Copy the instantaneous values to a secondary buffer, and print those to the console/debugger.

With respect to the control characters in RealTerm, isn't that a checkbox item. Try selecting a different terminal font?

HT (Hard Tab), the ASCII 9 value

CR (Carriage Return)

LF (Line Feed)

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..