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

Binary numbers don't appear as human readable ASCII. Please review Numeric Representation CS-101 course work.

{

char str[16];

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

}

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

the only one I see is the blinking LED, I have noticed that there a a few examples in atollic studio. but it is far from something to help you to learn developing codes on your own.

for example there are no libraries to connect something basic like touch tft.

It is a good board but you need to spend a lot of time to master it. And it is not for beginner.

Well not random ones, but there is support for those on EVAL, DISCO boards, and AdaFruit shield ones.

STM32Cube_FW_F4_V1.21.0\Drivers\BSP\Adafruit_Shield\stm32_adafruit_lcd.c

STM32Cube_FW_F4_V1.21.0\Drivers\BSP\STM32469I-Discovery\stm32469i_discovery_lcd.c

STM32Cube_FW_F4_V1.21.0\Drivers\BSP\STM32469I-Discovery\stm32469i_discovery_ts.c

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

I see, thanks I have to go over it, it seems to be a big file. Arduino IDE has support for some nucleo boards. I had the mcufriend touch tft 320 x 480 working right away with STM32F446 using Arduino IDE. But programming ADC, communication in Arduino IDE etc would require good knowledge of assembler and it is time consuming.

But I could never get it working the same tft with Keil.

Would it be an improvement if all nucleo board examples use the STLink debugger serial port + Teratem console to provide a more interactive / less button based (and more automated) examples?

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

Besides to the examples in the STM32Cube package (already suggested by Clive), you can refer to the STM32 Education page (https://www.st.com/content/st_com/en/support/learning/stm32-education.html) where several resources are available there to start learning STM32.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

PAbdu
Associate II

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.

>> 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 Venmo
Up vote any posts that you find helpful, it shows what's working..