Skip to main content
JWolf.3
Associate
April 22, 2021
Question

I am trying to read in serial data using scanf through USART. The serial data is being sent by MATLAB. Here's the code that I have so far. printf works just fine. Scanning is the problem. Any suggestions are greatly appreciated!

  • April 22, 2021
  • 3 replies
  • 1132 views
int USART2_Write(int ch){
	//wait for TX buffer empty
	while (!(USART2->SR & USART_SR_TXE)) {}
	USART2->DR = ch;
	return 0;
}
 
int USART2_Read(void) {
	while (!(USART2->SR & USART_SR_RXNE)) {}
	return USART2->DR;
}
 
#ifdef __GNUC__
	#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
	#define GETCHAR_PROTOTYPE int __io_getchar (void)
#else
	#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
	#define GETCHAR_PROTOTYPE int fgetc(FILE * f)
#endif
 
PUTCHAR_PROTOTYPE{
	USART2_Write(ch);
	return ch;
}
 
GETCHAR_PROTOTYPE {
 uint8_t ch = 0;
 
 ch = USART2_Read();
}

This topic has been closed for replies.

3 replies

Vangelis Fortounas
Associate II
April 22, 2021

hello

GETCHAR_PROTOTYPE must return the ch value.

JWolf.3
JWolf.3Author
Associate
April 22, 2021

I just added that but still no success

Vangelis Fortounas
Associate II
April 22, 2021

Did USART2_Read(void) ever called? And if called, did ever received something ?

The provided information does not give enough clues help.