cancel
Showing results for 
Search instead for 
Did you mean: 

12bit ADC output over USART on STM32

PAbdu
Associate II

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

17 REPLIES 17
Uwe Bonnes
Principal II

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

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

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

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

Output of ADC in the debugging screen:

0690X000006DDUzQAO.png

the code is right, you must have something incorrect done with initialization in cube mx or there is some problem with terminal in your computer. I assume you use USB to connect to your PC.

You enable asynchronous in cube mx, disable hardware flow control. Do the DMA data lenght byte for USART2_RX. Do the circular mode so it keeps transmiting This is all for cube mx.

this code works HAL_UART_Transmit(&huart2, data, 2, 10); there is nothing else you need. Put the code in while(); loop to test.

It has to work. Look at the terminal you use in your PC.

Thanks I will look and get back. I don't think it's my serial monitor because when I type the following, my USART works exactly as it should:

uint8_t mxTxData[13] = "Hello World\r\n";

HAL_UART_Transmit(&huart2,mxTxData,13,10 );

You are correct, I am connected to my computer over USB. I had already enabled asynchronous, I don't have RX USART enabled because I only need to send data out over the USART, I don't need to receive anything. Hardware flow control is disabled and "HAL_UART_Transmit(&huart2, data, 2, 10);" is in a while loop but I'm still getting the null character message. I might try to start fresh and see if I can get your example to work and then going to to those code to see where the problem is. I'm all ears if you have any suggestions 🙂 Thanks again for your help!

0690X000006DDVYQA4.png

>>The problem with STM is that they do not give you any sample programs to learn. 

The HAL libraries comes with literally hundreds of examples.

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