3

I need 9 bit UART data on an Arduino Uno and so I have to do some manual setup of the Arduino UART. Basically, I don't understand this line of example code (from the datasheet), it is meant to enable a UART Tx and Rx pin on the Arduino, which done in simple and easy to understand machine language just means loading an immediate value to UCSR0B (USART Control and Status Register B) in a way that the RXE (Recieve Enable) bit, and TXE (Transmission Enable) bit are both high. In other words, load 00011000 into USCR0B.

Here's the example C code from the datasheet:

USCR0B = (1 << RXE) | (1 << TXE);
Paul R
  • 208,748
  • 37
  • 389
  • 560
Nathan Darker
  • 141
  • 1
  • 9
  • 3
    A very long way to ask a very basic question. Please note this guideline from the SO help pages: "This site is all about getting answers. It's not a discussion forum. There's no chit-chat." – kaylum Sep 10 '15 at 06:14
  • 1
    There's a perfectly fine answer, so just a little hint: newer versions of the avr libc include the `_BV` macro (bit value) that hides the shifting, resulting in code some would consider better readable: `USCR0B = _BV(RXE) | _BV(TXE)` –  Sep 10 '15 at 06:19
  • 3
    `1 << n` is the de facto standard way for masking out a single bit. – Lundin Sep 10 '15 at 06:32
  • 1
    @Lundin That's why I wrote "some would consider" :) It replaces the technical details by the semantics ... after all a matter of personal taste whether to use it or not. –  Sep 10 '15 at 06:41
  • @FelixPalmen Inviting obscure macros to do simple things is not personal taste, it is universally bad practice. Assume that C programmers know the C language, and that they don't know some home brewed macro-language. That _BV macro suggests "help I'm an embedded C programmer who doesn't know about bytes and bits". The solution to that is to learn, not to invent macros. – Lundin Sep 10 '15 at 06:50
  • @Lundin this macro is neither "obscure" nor "homebrewn", it's provided by the avr libc for a reason, and that is improving the semantic expressiveness of code -- did you EVER here about this? Using macros with bit operations is quite common practice, too. You don't HAVE to use it if you don't like it, but please stop considering your opinion the only valid one in the world. –  Sep 10 '15 at 07:08
  • 1
    @FelixPalmen It is homebrewn by Atmel and there is no sound rationale for using it. MCU C libraries are notoriously obscure and non-standard. Semiconductor manufacturers are notoriously poor at C programming. You can be absolutely certain that you will not find such a macro in any other MCU C library, but you can be certain to find some other obscure non-standard feature there instead, which you can use/absue. The key to sanity and portability when working with MCUs is to pick up as little non-C-standard, non-de-facto-standard crap as possible and to stick to pure standard code. – Lundin Sep 10 '15 at 09:01

1 Answers1

12

RXE and TXE are the bit indices, so (1<<RXE) | (1<<TXE) is a mask, where the TXE and RXE bits are both equal to 1 (and all other bits are 0).

E.g. I don't happen to know the actual values of RXE and TXE, but suppose TXE is bit 3 and RXE is bit 4, then the relevant header file definitions might look something like this:

#define TXE 3  // TX Enable = bit 3
#define RXE 4  // RX Enable = bit 4

and the mask calculation will work like this:

            1<<TXE  = 00001000 = 0x08
            1<<RXE  = 00010000 = 0x10
(1<<RXE) | (1<<TXE) = 00011000 - 0x18
Paul R
  • 208,748
  • 37
  • 389
  • 560