Deconstructing UCSR0B = (1 << RXEN0) | (1 << TXEN0);

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Newbie question.  I'm looking at the Atmel example code for the ATmega328P Xplained Mini.  I see where the USART tx and rx circuits are turned on by turning setting the RXEN0 and TXEN0 to 1 in the UCSR0B register:

  UCSR0B = (1 << RXEN0)  | (1 << TXEN0);

 

I'm just trying to understand the syntax.  Looks like a bitwise OR.  

 

But, what exactly is the meaning of the syntax 1 << RXEN0?  It looks like a bit shift left.  But, if they were trying to do that, I would expect them to be reversed around the '<<', and shifting left I would expect the bits to go to 0.  So, 1 << RXEN0 must mean something else?  Thanks ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101?skey=Bit%20manipulation%20101

Rxen0 is the bit number - lets say it is 5. Shift a one bit 5 places to the left you get 00100000 thats your bit mask

Last Edited: Thu. Jan 8, 2015 - 10:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it ... thanks!