whats (UBRRH=BAUD_PRESCALE>>8)?

Thread Starter

khan yousufzai

Joined Aug 31, 2012
19
in the below program BAUD_PRESCALE =((crystal freq/(baudrate*16)-1)=77
now i should do,UBRRL=BAUD_PRESCALE,BUT IN THE FOLLOWING FUNCTION ONE THING MAKES ME CONFUSED,UBRRH=(BAUD_PRESCALE>>8);WHATS THIS? PLEASE EXPLAIN,,,,THANKS SO MUCH,THE REAL HUMINITY

void usart_init()
{

UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
}
 

Papabravo

Joined Feb 24, 2006
21,228
>> is the shift right operator
<< is the shift left operator

The left operand is shifted left(right) by a number of bits equal to the right hand operand. It is a common way to refer to the upper byte of a 16-bit word.
 

takao21203

Joined Apr 28, 2012
3,702
>> is the shift right operator
<< is the shift left operator

The left operand is shifted left(right) by a number of bits equal to the right hand operand. It is a common way to refer to the upper byte of a 16-bit word.
If you have a BYTE PTR (or a char pointer), instead of shifting 8 times,
you could use *(byte_ptr+1)

If the upper byte is zero in your program, you can simply blank the destination. The lines maybe come from a generic template, where in some cases, the upper byte might be non-zero.
 

ErnieM

Joined Apr 24, 2011
8,377
IN THE FOLLOWING FUNCTION ONE THING MAKES ME CONFUSED,

UBRRH=(BAUD_PRESCALE>>8);
As has been noted the >> is the C shift operator. { something >> 8 } is one way to rotate the high byte of a 16 bit quantity to the lower byte.

If you would start with:

BAUD_PRESCALE = 0b1010101001010101;

then UBRRH would equal 0b10101010, or the high byte portion.
 

Papabravo

Joined Feb 24, 2006
21,228
And most compilers are smart enough to avoid shifting if at all possible. Some might argue that the shift operation has more clarity than the pointer operation.
 
Top