cancel
Showing results for 
Search instead for 
Did you mean: 

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!

JWolf.3
Associate II
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();
}

3 REPLIES 3

hello

GETCHAR_PROTOTYPE must return the ch value.

I just added that but still no success

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

The provided information does not give enough clues help.