[ITEM]
02.05.2020

Atmega 128 Uart

17
Routines for asynchronous UART communication on the ATMega328P
uart.c

Apogee mic cardioid condenser usb microphone for mac. AN1451 AVR306: Using the AVR UART in C on tinyAVR and megaAVR devices This Application Note describes how to set up and use the UART present in most 8-bit AVR devices. C code examples are included for polled and interrupt controlled UART applications.

Apocalypse

/*
* uart.c
*
* Asynchronous UART example tested on ATMega328P (16 MHz)
*
* Toolchain: avr-gcc (4.3.3)
* Editor: Eclipse Kepler (4)
* Usage:
* Perform all settings in uart.h and enable by calling initUART(void)
* Compile:
* make all
*
* Functions:
* - First call initUART() to set up Baud rate and frame format
* - initUART() calls macros TX_START() and RX_START() automatically
* - To enable interrupts on reception, call RX_INTEN() macros
* - Call functions getByte() and putByte(char) for character I/O
* - Call functions writeString(char*) and readString() for string I/O
*
* Created on: 21-Jan-2014
* Author: Shrikant Giridhar
*/
#include'uart.h'
// Debug Mode; comment out on Release
#define_DEBUG0
/*! brief Configures baud rate (refer datasheet) */
voidinitUART(void)
{
// Not necessary; initialize anyway
DDRD = _BV(PD1);
DDRD &= ~_BV(PD0);
// Set baud rate; lower byte and top nibble
UBRR0H = ((_UBRR) & 0xF00);
UBRR0L = (uint8_t) ((_UBRR) & 0xFF);
TX_START();
RX_START();
// Set frame format = 8-N-1
UCSR0C = (_DATA << UCSZ00);
}
/*! brief Returns a byte from the serial buffer
* Use this function if the RX interrupt is not enabled.
* Returns 0 on empty buffer
*/
uint8_tgetByte(void)
{
// Check to see if something was received
while (!(UCSR0A & _BV(RXC0)));
return (uint8_t) UDR0;
}
/*! brief Transmits a byte
* Use this function if the TX interrupt is not enabled.
* Blocks the serial port while TX completes
*/
voidputByte(unsignedchar data)
{
// Stay here until data buffer is empty
while (!(UCSR0A & _BV(UDRE0)));
UDR0 = (unsignedchar) data;
}
/*! brief Writes an ASCII string to the TX buffer */
voidwriteString(char *str)
{
while (*str != '0')
{
putByte(*str);
++str;
}
}
constchar* readString(void)
{
char rxstr[RX_BUFF];
char* temp;
temp = rxstr;
while((*temp = getByte()) != 'n')
{
++temp;
}
return rxstr;
}
#if _DEBUG
intmain(void)
{
initUART();
while(1)
{
writeString(readString());
putByte('r');
putByte('n');
}
return0;
}
#endif
uart.h
/*
* uart.h
*
* UART example for ATMega328P clocked at 16 MHz
*
* TODO :-
* - Implement string read function
* - Optimize for size
* - Add helper routines and compile to .a file
*
* Created on: 22-Jan-2014
* Author: Shrikant Giridhar
*/
#ifndef UART_H_
#defineUART_H_
#include<avr/io.h>
#include<stdint.h>
/* Probably already defined somewhere else. Define here, if isn't. */
#ifndef FOSC
#defineFOSC16000000UL
#endif
/* Settings */
#define_BAUD9600// Baud rate (9600 is default)
#define_DATA0x03// Number of data bits in frame = byte tranmission
#define_UBRR (FOSC/16)/_BAUD - 1// Used for UBRRL and UBRRH
#defineRX_BUFF10
/* Useful macros */
#defineTX_START() UCSR0B = _BV(TXEN0) // Enable TX
#defineTX_STOP() UCSR0B &= ~_BV(TXEN0) // Disable TX
#defineRX_START() UCSR0B = _BV(RXEN0) // Enable RX
#defineRX_STOP() UCSR0B &= ~_BV(RXEN0) // Disable RX
#defineCOMM_START() TX_START(); RX_START() // Enable communications
/* Interrupt macros; Remember to set the GIE bit in SREG before using (see datasheet) */
#defineRX_INTEN() UCSR0B = _BV(RXCIE0) // Enable interrupt on RX complete
#defineRX_INTDIS() UCSR0B &= ~_BV(RXCIE0) // Disable RX interrupt
#defineTX_INTEN() UCSR0B = _BV(TXCIE0) // Enable interrupt on TX complete
#defineTX_INTDIS() UCSR0B &= ~_BV(TXCIE0) // Disable TX interrupt
/* Prototypes */
voidinitUART(void);
uint8_tgetByte(void);
voidputByte(unsignedchar data);
voidwriteString(char *str);
constchar* readString(void);
#endif/* UART_H_ */
Atmega128 datasheet
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
[/ITEM]
[/MAIN]
02.05.2020

Atmega 128 Uart

96
Routines for asynchronous UART communication on the ATMega328P
uart.c

Apogee mic cardioid condenser usb microphone for mac. AN1451 AVR306: Using the AVR UART in C on tinyAVR and megaAVR devices This Application Note describes how to set up and use the UART present in most 8-bit AVR devices. C code examples are included for polled and interrupt controlled UART applications.

Apocalypse

/*
* uart.c
*
* Asynchronous UART example tested on ATMega328P (16 MHz)
*
* Toolchain: avr-gcc (4.3.3)
* Editor: Eclipse Kepler (4)
* Usage:
* Perform all settings in uart.h and enable by calling initUART(void)
* Compile:
* make all
*
* Functions:
* - First call initUART() to set up Baud rate and frame format
* - initUART() calls macros TX_START() and RX_START() automatically
* - To enable interrupts on reception, call RX_INTEN() macros
* - Call functions getByte() and putByte(char) for character I/O
* - Call functions writeString(char*) and readString() for string I/O
*
* Created on: 21-Jan-2014
* Author: Shrikant Giridhar
*/
#include'uart.h'
// Debug Mode; comment out on Release
#define_DEBUG0
/*! brief Configures baud rate (refer datasheet) */
voidinitUART(void)
{
// Not necessary; initialize anyway
DDRD = _BV(PD1);
DDRD &= ~_BV(PD0);
// Set baud rate; lower byte and top nibble
UBRR0H = ((_UBRR) & 0xF00);
UBRR0L = (uint8_t) ((_UBRR) & 0xFF);
TX_START();
RX_START();
// Set frame format = 8-N-1
UCSR0C = (_DATA << UCSZ00);
}
/*! brief Returns a byte from the serial buffer
* Use this function if the RX interrupt is not enabled.
* Returns 0 on empty buffer
*/
uint8_tgetByte(void)
{
// Check to see if something was received
while (!(UCSR0A & _BV(RXC0)));
return (uint8_t) UDR0;
}
/*! brief Transmits a byte
* Use this function if the TX interrupt is not enabled.
* Blocks the serial port while TX completes
*/
voidputByte(unsignedchar data)
{
// Stay here until data buffer is empty
while (!(UCSR0A & _BV(UDRE0)));
UDR0 = (unsignedchar) data;
}
/*! brief Writes an ASCII string to the TX buffer */
voidwriteString(char *str)
{
while (*str != '0')
{
putByte(*str);
++str;
}
}
constchar* readString(void)
{
char rxstr[RX_BUFF];
char* temp;
temp = rxstr;
while((*temp = getByte()) != 'n')
{
++temp;
}
return rxstr;
}
#if _DEBUG
intmain(void)
{
initUART();
while(1)
{
writeString(readString());
putByte('r');
putByte('n');
}
return0;
}
#endif
uart.h
/*
* uart.h
*
* UART example for ATMega328P clocked at 16 MHz
*
* TODO :-
* - Implement string read function
* - Optimize for size
* - Add helper routines and compile to .a file
*
* Created on: 22-Jan-2014
* Author: Shrikant Giridhar
*/
#ifndef UART_H_
#defineUART_H_
#include<avr/io.h>
#include<stdint.h>
/* Probably already defined somewhere else. Define here, if isn't. */
#ifndef FOSC
#defineFOSC16000000UL
#endif
/* Settings */
#define_BAUD9600// Baud rate (9600 is default)
#define_DATA0x03// Number of data bits in frame = byte tranmission
#define_UBRR (FOSC/16)/_BAUD - 1// Used for UBRRL and UBRRH
#defineRX_BUFF10
/* Useful macros */
#defineTX_START() UCSR0B = _BV(TXEN0) // Enable TX
#defineTX_STOP() UCSR0B &= ~_BV(TXEN0) // Disable TX
#defineRX_START() UCSR0B = _BV(RXEN0) // Enable RX
#defineRX_STOP() UCSR0B &= ~_BV(RXEN0) // Disable RX
#defineCOMM_START() TX_START(); RX_START() // Enable communications
/* Interrupt macros; Remember to set the GIE bit in SREG before using (see datasheet) */
#defineRX_INTEN() UCSR0B = _BV(RXCIE0) // Enable interrupt on RX complete
#defineRX_INTDIS() UCSR0B &= ~_BV(RXCIE0) // Disable RX interrupt
#defineTX_INTEN() UCSR0B = _BV(TXCIE0) // Enable interrupt on TX complete
#defineTX_INTDIS() UCSR0B &= ~_BV(TXCIE0) // Disable TX interrupt
/* Prototypes */
voidinitUART(void);
uint8_tgetByte(void);
voidputByte(unsignedchar data);
voidwriteString(char *str);
constchar* readString(void);
#endif/* UART_H_ */
Atmega128 datasheet
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment