2018
July Wednesday 18

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Arduino Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com


2018 REVIEW

Arduino Serial Communications With AVR libc

ADVANCED COMPUTER-PROGRAMMING

2018 APRIL | by Gene Casanova

Senior Systems/Network/Internet/Robotics Engineer; Senior Technical Writer, Author


The AVR libc Library

When I was first introduced to the Arduino development board, I recognized it as a very inexpensive and optimal tool for AVR microcontroller (microcomputer) development. I quickly began to develop more computer embedded intelligence (CEI) applications, based on various AVR microcomputers.

The AVR "Libc" library provides proven printf(), puts(), and getchar() serial communication functions. This document presents effective implementation of the libc functions, used for serial communications.

The Arduino learning platform concept and the Arduino library are good at hiding the confusing features of commercial computer embedded programming. The Atmel AVR datasheets, are not as confusing as they first appear to persons reading them for the first time without previous experience and knowledge in how to read the data presented.


Configuring A UART

AVR microcontrollers have three control and status registers. Register "UCSR0A" mostly contains status data. Registers " UCSR0B" and "UCSR0C" contain all the communication-link configuration settings.

AVR Libc provides macros (subroutines) for baud rate calculations.

First, the header file requires "F_CPU" and "BAUD" to be defined.

Then include the header file.

After including the header file, "UBRRL_VALUE", "UBRRH_VALUE" and "USE_2X" are defined. The first two, are used to set "UART" speed. The last one, is used to determine if "UART" has to be configured to run in double speed mode with the specified baud rate.

'UCSZ20', 'UCSZ01', and 'UCSZ00', control the data size. Possible sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111). Most common used data size, is 8-bit.

Using the bits above, the most commonly-used serial communication configuration can be set: no parity, 8 data bits, 1 stop bit.

Example source code:

#define F_CPU 16000000UL
#define BAUD 9600
#include <util/setbaud.h>

void uart_init(void) {
     UBRR0H = UBRRH_VALUE;
     UBRR0L = UBRRL_VALUE;
  #if USE_2X
     UCSR0A |= _BV(U2X0);
 #else
     UCSR0A &= ~(_BV(U2X0));
 #endif

     UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
     UCSR0B = _BV(RXEN0) | _BV(TXE:N0);   /* Enable RX and TX */ }

Writing & Reading Through UART

Sending

Send data to "UART" by writing a byte to the "USART" Data Register "UDR0".

Start by checking if the UART is ready to send new data. This can be done by waiting until 'USART Data Register Empty UDRE' flag is set. Or, wait after each byte, for 'USART Transmit Complete TXC0' to be set, when UART is ready.

Examples:

Both methods are presented in the following source code:

void uart_putchar(char c) {
    loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
    UDR0 = c;
}

void uart_putchar(char c) {
    UDR0 = c;
    loop_until_bit_is_set(UCSR0A, TXC0); /* Wait until transmission ready. */
}

Receiving

Receive data from "UART" by reading a byte from the 'USART Data Register' "UDR0". "USART" Receive Complete 'RXC0' flag is set if to unread data exists in data register.

Example:

char uart_getchar(void) {
     loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
     return UDR0;
}

Redirecting STDIN & STDOUT to UART

The 'FDEV_SETUP_STREAM' macro, can be used, to setup a buffer, valid for "stdio" operations.

Initialized buffer will be of type 'FILE'. Separate buffers can be defined for 'input' and 'output'.

Alternatively, one buffer can be defined, for 'input' and 'output'.

The first and second parameters, are names of the functions, called when data is either 'read' or 'written'.

Example:

FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);

FILE uart_io FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

To prepare the "uart_putchar" and "uart_getchar" function to be used with streams, change the definition. To properly format output, add a carriage return after newline has been sent.

Example:

void uart_putchar(char c, FILE *stream) {
     if (c == '\n') {
         uart_putchar('\r', stream);
     }
     loop_until_bit_is_set(UCSR0A, UDRE0);
     UDR0 = c;
 }

char uart_getchar(FILE *stream) {
     loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
     return UDR0;
}

Now "STDIN" and "STDOUT" can be redirected to 'UART'. This enables using AVR Libc functions to 'read' and 'write' to the serial port.

Example

int main(void) {
     uart_init();
     stdout = &uart_output;
     stdin  = &uart_input;

     char input;
     while(1) {
         puts("Hello World!");
         input = getchar();
         printf("You wrote %c\n", input);
     }
     return 0;
}

Control & Status, Registers

UCSROA

UCSROB

UCSROC

UCSROA
UCSROA Bit #NameDescription
7 RXC0

USART Receive Complete.

Set when data is available and the data register has not be read yet.

6 TXC0

USART Transmit Complete.

Set when all data has transmitted.

5 UDRE0

USART Data Register Empty.

Set when the UDR0register is empty and new data can be transmitted.

4 FE0

Frame Error.

Set when next byte in the UDR0register has a framing error.

3 DOR0

Data OverRun.

Set when the UDR0 was not read before the
next frame arrived.

2 UPE0

USART Parity Error.

Set when next frame in the UDR0 has a parity error.

1 U2X0

USART Double Transmission Speed.

When set decreases the bit time by half doubling the speed.

0 MPCM0

Multi-processor Communication Mode.

When set, incoming data is ignored, if no addressing information is provided.

UCSROB

UCSROB
UCSROA Bit #NameDescription
7 RXCIE0

RX Complete Interrupt Enable.

Set to enable receive complete interrupts.

6 TXCIE0

TX Complete Interrupt Enable

Set to enable transmission complete interrupts.

5 UDRIE0

USART Data Register Empty Interrupt Enable.

Set to enable data register empty interrupts.

4 RXEN0

Receiver Enable.

Set to enable receiver.

3 TXEN0

Transmitter enable.

Set to enable transmitter.

2 UCSZ20

USART Character Size 0.

Used together with UCSZ01 and UCSZ00 to set data frame size. Available sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111).

1 URXB80

Receive Data Bit 8.

When using 8 bit transmission the 8th bit received.

0 TXB80

Transmit Data Bit 8.

When using 8 bit transmission the 8th bit to be submitted.

UCSROC

UCSROC
UCSROA Bit #NameDescription
7 & 6  

USART Mode Select 1 and 0.

UMSEL01 and UMSEL00 combined select the operating mode.

Available modes are asynchronous (00), synchronous (01) and master SPI (11).

5 & 4

UPM01

UPM00

USART Parity Mode 1 and 0.

UPM01 and UPM00 select the parity.

Available modes are none (00), even (10) and odd (11).

3 USBS0

USART Stop Bit Select.

Set to select 1 stop bit. Unset to select 2 stop bits.

2 & 1

UCSZ01

UCSZ00

USART Character Size 1 and 0.

Used together with with UCSZ20 to set data frame size.

Available sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111).

0 UCPOL0

USART Clock Polarity.

Set to transmit on falling edge and sample on rising edge.

Unset to transmit on rising edge and sample on falling edge.


Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova


Tier 1 Commercial Embedded Computer Programming Services,

Cyber Security Since 1979

Effective, Optimal, Proficient, Scalable, Sustainable

Commercial Support & Consulting Subscription

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com