[SOLVED] 9 bit Usart not working (MPCM)

Status
Not open for further replies.

Adbadb

Junior Member level 1
Joined
Jan 14, 2015
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
118
So there is Master and Slave Attiny2313. The Master sends the 9 bits of data(the 9th bit TXB8 is set to 1), but the slave doesn't detect the 9th bit(RXB8 is still 0).
I think if the TXB8 bit is set in the Master the RXB8 bit on the Slave should be set automatically, or not?

Code:
Code:
void USART_Init(void)
{
	/* Set baud rate */
	UBRRH = (unsigned char)(BAUDRATE>>8);
	UBRRL = (unsigned char)BAUDRATE;
	
	/* Enable receiver and transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	
	/* Set frame format: 9data, 1stop bit */
	UCSRC = (7<<UCSZ0);
}

void USART_Transmit(unsigned int data)
{
	/* Wait for empty transmit buffer */
	while ( !( UCSRA & (1<<UDRE)) );
	
	/* Copy 9th bit to TXB8 */
	UCSRB &= ~(1<<TXB8);
	if ( data & 0x0100 )
		UCSRB |= (1<<TXB8);
	
	/* Put data into buffer, sends the data */

	UDR = data;

//Slave Receive Code

gned int USART_Receive( void )
{
	unsigned char status, resh, resl;
	
	/* Wait for data to be received */
	while ( !(UCSRA & (1<<RXC)) );
	
	
	
	/* Get status and 9th bit, then data from buffer */
	status = UCSRA;	
	resh = UCSRB;
	resl = UDR;
		return resh; ///test
	/* If error, return -1 */
	if ( status & ((1<<FE)|(1<<DOR)|(1<<UPE)) )
	return -1;
	
	/* Filter the 9th bit, then return */
	resh = (resh >> 1) & 0x01;	
	return ((resh << 8) | resl);
	
}
 

uh oh, I think the AVR-GCC still makes its operations on 8 bits if both operands are 8 bits, so
Code:
return ((resh << 8) | resl);
effectively returns resl only..

try making a cast

Code:
return (((unsigned int)resh << 8) | (unsigned int)resl);
I think the (int) cast on resl isn't necessary, but lets try...

also check with another port or variable, if resh is 0x20 before your /*Filter*/ if you are still in doubt...
 


Yes but even before all that i have
Code:
return resh; ///test
This is to test if the bit RXB8 in UCSRB is set to 1.
And it isn't, that is the problem.
 

I found what was the problem, the USART was initialised like this:
Code:
/* Enable receiver and transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	
	/* Set frame format: 9data, 1stop bit */
	UCSRC = (7<<UCSZ0);
But the UCSZ2 bit is in the USCRB register and not in UCSRC, so the right code will be:

Code:
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<UCSZ2);
UCSRC  = (1<<UCSZ0)|(1<<UCSZ1);
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…