Embedded C Firmware Library : Arduino / Atmega328p  1
Register Level Embedded C Hardware Abstraction Library for AVR ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI
avr_usart.h File Reference

Library for USART Communication. More...

#include <avr/io.h>
#include <avr/interrupt.h>
#include "avr_macros.h"
#include "avr_utils.h"

Go to the source code of this file.

Data Structures

struct  USART_ConfigData
 

Macros

USART_MODE

Options for USART Operation Mode

#define USART_SYNCHRONOUS_CLOCK_PRIORITY   0
 
#define USART_MODE_ASYNCHRONOUS   (0<<UMSEL00)
 
#define USART_MODE_SYNCHRONOUS   (1<<UMSEL00) | USART_SYNCHRONOUS_CLOCK_PRIORITY
 
#define USART_MODE_MASK   (1<<UMSEL00)
 
USART_PARITY_BIT

Options for Parity Bit in USART Data Frame

#define USART_PARITY_BIT_NO   (0<<UPM00)
 
#define USART_PARITY_BIT_EVEN   (1<<UPM01)
 
#define USART_PARITY_BIT_ODD   (1<<UPM01) | (1<<UPM00)
 
#define USART_PARITY_BIT_MASK   (1<<UPM01) | (1<<UPM00)
 
USART_STOP_BIT

Options for Stop Bit in USART Data Frame

#define USART_STOP_BIT_ONE   (0<<USBS0)
 
#define USART_STOP_BIT_TWO   (1<<USBS0)
 
#define USART_STOP_BIT_MASK   (1<<USBS0)
 
USART_DATA_BIT

Options for Data Bit Length in USART Data Frame

#define USART_DATA_BIT_FIVE   (0<<UCSZ00)
 
#define USART_DATA_BIT_SIX   (1<<UCSZ00)
 
#define USART_DATA_BIT_SEVEN   (1<<UCSZ01)
 
#define USART_DATA_BIT_EIGHT   (1<<UCSZ01) | (1<<UCSZ00)
 
#define USART_DATA_BIT_MASK   (1<<UCSZ01) | (1<<UCSZ00)
 
USART_INTERRUPT

Options for USART Interrupts

#define USART_RX_COMPLETE_INTERRUPT   (1<<RXCIE0)
 
#define USART_TX_COMPLETE_INTERRUPT   (1<<TXCIE0)
 
#define USART_DATA_REGISTER_EMPTY_INTERRUPT   (1<<UDRIE0)
 

Typedefs

USART_CONFIGURATION_DATA

Structure used to Configure USART

typedef struct USART_ConfigData USART_ConfigData
 

Functions

 ISR (USART_RX_vect)
 Interrupt Service Routine for Reception. More...
 
 ISR (USART_UDRE_vect)
 Interrupt Service Routine for Transmission. More...
 
USART_FLUSH_TRANSMIT_RECEIVE_BUFFER

Public Functions to Flush USART Transmit and Receive Buffer

void USART_FlushTransmitBuffer ()
 Public Function to Flush the Transmit Buffer. More...
 
void USART_FlushReceiveBuffer ()
 Public Function to Flush the Receive Buffer. More...
 
USART_TRANSMIT_INTERRUPT_CONTROL

Public Functions to Control USART Transmit Interrupts

void USART_EnableTransmitInterrupt ()
 Public Function to Enable Transmit Interrupt. More...
 
void USART_DisableTransmitInterrupt ()
 Public Function to Disable Transmit Interrupt. More...
 
USART_RECEIVE_INTERRUPT_CONTROL

Public Functions to Control USART Receive Interrupts

void USART_EnableReceiveInterrupt ()
 Public Function to Enable Receive Interrupt. More...
 
void USART_DisableReceiveInterrupt ()
 Public Function to Disable Receive Interrupt. More...
 
USART_CONFIGURATION_AND_CONTROL

Public Functions to Control and Configure USART

void USART_Init (USART_ConfigData Data)
 Public Function to Configure and Initialize USART. More...
 
void USART_DeInit ()
 Public Function to De-Initialize USART. More...
 
USART_RECEIVE

Public Functions to Receive Data via USART

uint8_t USART_ReceiveBlocking ()
 Public Function to Receive 1 Byte of Data in Polling. More...
 
uint8_t USART_ReceiveByte (uint8_t *Buffer)
 Public Function to Receive 1 Byte of Data. More...
 
uint8_t USART_ReceiveTwoBytes (uint16_t *Buffer)
 Public Function to Receive 2 Bytes of Data with LSB Byte First. More...
 
uint8_t USART_ReceiveBytes (uint8_t *Buffer, uint16_t Size)
 Public Function to Receive Multiple Bytes of Data. More...
 
uint8_t USART_ReceiveChar (char *Character)
 Public Function to Receive char Data. More...
 
uint8_t USART_ReceiveInt (int *Number, uint8_t Size)
 Public Function to Receive int Data as ASCII. More...
 
uint8_t USART_ReceiveLong (long *Number, uint8_t Size)
 Public Function to Receive long Data as ASCII. More...
 
uint8_t USART_ReceiveString (char *Buffer, uint16_t Size)
 Public Function to Receive string Data. More...
 
void USART_CancelReceive ()
 Public Function to Cancel any Interrupt based Reception. More...
 
USART_TRANSMIT

Public Functions to Transmit Data via USART

uint8_t USART_TransmitBlocking (uint8_t *Buffer)
 Public Function to Transmit 1 Byte of Data in Polling. More...
 
uint8_t USART_TransmitByte (uint8_t *Buffer)
 Public Function to Transmit 1 Byte of Data. More...
 
uint8_t USART_TransmitTwoBytes (uint16_t *Buffer)
 Public Function to Transmit 2 Bytes of Data with LSB Byte First. More...
 
uint8_t USART_TransmitBytes (uint8_t *Buffer, uint16_t Size)
 Public Function to Transmit Multiple Bytes of Data. More...
 
uint8_t USART_TransmitChar (char Character)
 Public Function to Transmit char Data. More...
 
uint8_t USART_TransmitInt (int Number)
 Public Function to Transmit int Data as ASCII. More...
 
uint8_t USART_TransmitLong (long Number)
 Public Function to Transmit long Data as ASCII. More...
 
uint8_t USART_TransmitFloat (float Number, uint8_t Precision)
 Public Function to Transmit float Data as ASCII. More...
 
uint8_t USART_TransmitString (char *Buffer)
 Public Function to Transmit string Data. More...
 
void USART_CancelTransmit ()
 Public Function to Cancel any Interrupt based Transmission. More...
 

Detailed Description

Library for USART Communication.

Created: 01-05-2020 08:20:10 PM
Author : Arnab Kumar Das
Website: www.ArnabKumarDas.com
Microcontroller Supported: ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI

                +-\/-+
          PC6  1|    |28  PC5 (A5/ADC5)
RXD  (D0) PD0  2|    |27  PC4 (A4/ADC4)
TXD  (D1) PD1  3|    |26  PC3 (A3/ADC3)
     (D2) PD2  4|    |25  PC2 (A2/ADC2)
PWM  (D3) PD3  5|    |24  PC1 (A1/ADC1)
XCK  (D4) PD4  6|    |23  PC0 (A0/ADC0)
          VCC  7|    |22  GND
          GND  8|    |21  AREF
          PB6  9|    |20  AVCC
          PB7 10|    |19  PB5 (D13)
OC0B (D5) PD5 11|    |18  PB4 (D12)
OC0A (D6) PD6 12|    |17  PB3 (D11) PWM
     (D7) PD7 13|    |16  PB2 (D10) PWM
     (D8) PB0 14|    |15  PB1 (D9)  PWM
                +----+
Note
This library uses Interrupt based Non-Blocking Transmission

Macro Definition Documentation

◆ USART_SYNCHRONOUS_CLOCK_PRIORITY

#define USART_SYNCHRONOUS_CLOCK_PRIORITY   0

USART Clock Priority when Synchronous External Clock is used

◆ USART_MODE_ASYNCHRONOUS

#define USART_MODE_ASYNCHRONOUS   (0<<UMSEL00)

Sets the USART Mode of Operation to : Asynchronous UART

◆ USART_MODE_SYNCHRONOUS

#define USART_MODE_SYNCHRONOUS   (1<<UMSEL00) | USART_SYNCHRONOUS_CLOCK_PRIORITY

Sets the USART Mode of Operation to : Synchronous USRT

◆ USART_MODE_MASK

#define USART_MODE_MASK   (1<<UMSEL00)

Mask for USART Mode of Operation

◆ USART_PARITY_BIT_NO

#define USART_PARITY_BIT_NO   (0<<UPM00)

Sets the Parity Bit of USART Data Frame to : No Parity Bit

◆ USART_PARITY_BIT_EVEN

#define USART_PARITY_BIT_EVEN   (1<<UPM01)

Sets the Parity Bit of USART Data Frame to : Even Parity Bit

◆ USART_PARITY_BIT_ODD

#define USART_PARITY_BIT_ODD   (1<<UPM01) | (1<<UPM00)

Sets the Parity Bit of USART Data Frame to : Odd Parity Bit

◆ USART_PARITY_BIT_MASK

#define USART_PARITY_BIT_MASK   (1<<UPM01) | (1<<UPM00)

Mask for USART Parity Bit

◆ USART_STOP_BIT_ONE

#define USART_STOP_BIT_ONE   (0<<USBS0)

Sets the Stop Bit of USART Data Frame to : One Stop Bit

◆ USART_STOP_BIT_TWO

#define USART_STOP_BIT_TWO   (1<<USBS0)

Sets the Stop Bit of USART Data Frame to : Two Stop Bit

◆ USART_STOP_BIT_MASK

#define USART_STOP_BIT_MASK   (1<<USBS0)

Mask for USART Stop Bit

◆ USART_DATA_BIT_FIVE

#define USART_DATA_BIT_FIVE   (0<<UCSZ00)

Sets the Data Bit length of USART Data Frame to : Five Bit

◆ USART_DATA_BIT_SIX

#define USART_DATA_BIT_SIX   (1<<UCSZ00)

Sets the Data Bit length of USART Data Frame to : Six Bit

◆ USART_DATA_BIT_SEVEN

#define USART_DATA_BIT_SEVEN   (1<<UCSZ01)

Sets the Data Bit length of USART Data Frame to : Seven Bit

◆ USART_DATA_BIT_EIGHT

#define USART_DATA_BIT_EIGHT   (1<<UCSZ01) | (1<<UCSZ00)

Sets the Data Bit length of USART Data Frame to : Eight Bit

◆ USART_DATA_BIT_MASK

#define USART_DATA_BIT_MASK   (1<<UCSZ01) | (1<<UCSZ00)

Mask for the Data Bit length of USART Data Frame

◆ USART_RX_COMPLETE_INTERRUPT

#define USART_RX_COMPLETE_INTERRUPT   (1<<RXCIE0)

USART Interrupt Control Bit : Receive Complete Interrupt

◆ USART_TX_COMPLETE_INTERRUPT

#define USART_TX_COMPLETE_INTERRUPT   (1<<TXCIE0)

USART Interrupt Control Bit : Transmit Complete Interrupt

◆ USART_DATA_REGISTER_EMPTY_INTERRUPT

#define USART_DATA_REGISTER_EMPTY_INTERRUPT   (1<<UDRIE0)

USART Interrupt Control Bit : Data Register Empty Interrupt

Function Documentation

◆ USART_FlushTransmitBuffer()

void USART_FlushTransmitBuffer ( )

Public Function to Flush the Transmit Buffer.

Returns
void

◆ USART_FlushReceiveBuffer()

void USART_FlushReceiveBuffer ( )

Public Function to Flush the Receive Buffer.

Returns
void

◆ USART_EnableTransmitInterrupt()

void USART_EnableTransmitInterrupt ( )
inline

Public Function to Enable Transmit Interrupt.

Returns
void

◆ USART_DisableTransmitInterrupt()

void USART_DisableTransmitInterrupt ( )
inline

Public Function to Disable Transmit Interrupt.

Returns
void

◆ USART_EnableReceiveInterrupt()

void USART_EnableReceiveInterrupt ( )
inline

Public Function to Enable Receive Interrupt.

Returns
void

◆ USART_DisableReceiveInterrupt()

void USART_DisableReceiveInterrupt ( )
inline

Public Function to Disable Receive Interrupt.

Returns
void

◆ USART_Init()

void USART_Init ( USART_ConfigData  Data)

Public Function to Configure and Initialize USART.

Parameters
[in]Data: Structure Variable containing USART Configuration Data
Returns
void

USART_ConfigData Data2; Data2.BaudRate = 9600; Data2.DataBit = USART_DATA_BIT_EIGHT; Data2.ParityBit = USART_PARITY_BIT_NO; Data2.StopBit = USART_STOP_BIT_ONE; Data2.UsartMode = USART_MODE_ASYNCHRONOUS; USART_Init(Data2);

◆ USART_DeInit()

void USART_DeInit ( )

Public Function to De-Initialize USART.

Returns
void

◆ USART_ReceiveBlocking()

uint8_t USART_ReceiveBlocking ( )

Public Function to Receive 1 Byte of Data in Polling.

Returns
Received Byte of Data

◆ USART_ReceiveByte()

uint8_t USART_ReceiveByte ( uint8_t *  Buffer)

Public Function to Receive 1 Byte of Data.

Parameters
[out]Buffer: Pointer to Memory Location to Store Data
Returns
BUSY / OK

◆ USART_ReceiveTwoBytes()

uint8_t USART_ReceiveTwoBytes ( uint16_t *  Buffer)

Public Function to Receive 2 Bytes of Data with LSB Byte First.

Parameters
[out]Buffer: Pointer to Memory Location to Store Data
Returns
BUSY / OK

◆ USART_ReceiveBytes()

uint8_t USART_ReceiveBytes ( uint8_t *  Buffer,
uint16_t  Size 
)

Public Function to Receive Multiple Bytes of Data.

Parameters
[out]Buffer: Pointer to Memory Location to Store Data
[in]Size: Variable to Store Number of Bytes to Receive
Returns
BUSY / OK

◆ USART_ReceiveChar()

uint8_t USART_ReceiveChar ( char *  Character)

Public Function to Receive char Data.

Parameters
[out]Character: Pointer to Memory Location to Store char Data
Returns
BUSY / OK

◆ USART_ReceiveInt()

uint8_t USART_ReceiveInt ( int *  Number,
uint8_t  Size 
)

Public Function to Receive int Data as ASCII.

Parameters
[out]Number: Pointer to Memory Location to Store int Data
[in]Size: Variable to Store Number of Digits to Receive
Returns
BUSY / OK

◆ USART_ReceiveLong()

uint8_t USART_ReceiveLong ( long *  Number,
uint8_t  Size 
)

Public Function to Receive long Data as ASCII.

Parameters
[out]Number: Pointer to Memory Location to Store long Data
[in]Size: Variable to Store Number of Digits to Receive
Returns
BUSY / OK

◆ USART_ReceiveString()

uint8_t USART_ReceiveString ( char *  Buffer,
uint16_t  Size 
)

Public Function to Receive string Data.

Parameters
[out]Buffer: Pointer to Memory Location to Store string Data
[in]Size: Variable to Store Number of Letters to Receive
Returns
BUSY / OK

◆ USART_CancelReceive()

void USART_CancelReceive ( )

Public Function to Cancel any Interrupt based Reception.

Returns
void

◆ USART_TransmitBlocking()

uint8_t USART_TransmitBlocking ( uint8_t *  Buffer)

Public Function to Transmit 1 Byte of Data in Polling.

Returns
OK

◆ USART_TransmitByte()

uint8_t USART_TransmitByte ( uint8_t *  Buffer)

Public Function to Transmit 1 Byte of Data.

Parameters
[in]Buffer: Pointer to Memory Location to Store Data
Returns
BUSY / OK

◆ USART_TransmitTwoBytes()

uint8_t USART_TransmitTwoBytes ( uint16_t *  Buffer)

Public Function to Transmit 2 Bytes of Data with LSB Byte First.

Parameters
[in]Buffer: Pointer to Memory Location to Store Data
Returns
BUSY / OK

◆ USART_TransmitBytes()

uint8_t USART_TransmitBytes ( uint8_t *  Buffer,
uint16_t  Size 
)

Public Function to Transmit Multiple Bytes of Data.

Parameters
[in]Buffer: Pointer to Memory Location to Store Data
[in]Size: Variable to Store Number of Bytes to Transmit
Returns
BUSY / OK

◆ USART_TransmitChar()

uint8_t USART_TransmitChar ( char  Character)

Public Function to Transmit char Data.

Parameters
[in]Character: Variable to Store char Data
Returns
BUSY / OK

◆ USART_TransmitInt()

uint8_t USART_TransmitInt ( int  Number)

Public Function to Transmit int Data as ASCII.

Parameters
[in]Number: Variable to Store int Data
Returns
BUSY / OK

◆ USART_TransmitLong()

uint8_t USART_TransmitLong ( long  Number)

Public Function to Transmit long Data as ASCII.

Parameters
[in]Number: Variable to Store long Data
Returns
BUSY / OK

◆ USART_TransmitFloat()

uint8_t USART_TransmitFloat ( float  Number,
uint8_t  Precision 
)

Public Function to Transmit float Data as ASCII.

Parameters
[in]Number: Variable to Store float Data
[in]Precision: Variable to Store Precision of Decimal Number
Returns
BUSY / OK

◆ USART_TransmitString()

uint8_t USART_TransmitString ( char *  Buffer)

Public Function to Transmit string Data.

Parameters
[in]Buffer: Pointer to Memory Location to Store string Data
Returns
BUSY / OK

◆ USART_CancelTransmit()

void USART_CancelTransmit ( )

Public Function to Cancel any Interrupt based Transmission.

Returns
void

◆ ISR() [1/2]

ISR ( USART_RX_vect  )

Interrupt Service Routine for Reception.

Parameters
[in]USART_RX_vect

◆ ISR() [2/2]

ISR ( USART_UDRE_vect  )

Interrupt Service Routine for Transmission.

Parameters
[in]USART_UDRE_vect