Created
March 12, 2016 01:29
-
-
Save ekapujiw2002/665d5bbb7c9ff6c3a9ac to your computer and use it in GitHub Desktop.
This ready-to-use code is written for any AVR microcontroller to add some basic UART functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
Title: AVR UART library | |
Author: Robert-Maarten Schuurman <[email protected]> http://rms95.nl/ | |
Date: 28-06-2013 | |
Software: AVR-GCC 3.3 | |
Target: Any AVR device with a single UART and the registers named like | |
the ATmega32 (UCSRA-C, UBRRH and UBRRL). By modifying the code | |
a bit it is usable for any AVR UART. | |
DESCRIPTION | |
This file contains a function to set up the UART, to send and | |
receive bytes in a polling and non polling way. | |
There's also included a printf() function dedicated for the serial | |
connection, you can use this function like the normal printf() | |
USAGE | |
Just include these libraries, and if you need to, also change the | |
register names in the c file. At AVR microprocessors with multiple | |
UARTs it is usually required to put a number after the register name | |
but before the extension of the name: UCSRA --> UCSR0A or UCSR1A. | |
*****************************************************************************/ | |
#include <avr/io.h> | |
#include <stdarg.h> | |
/// Initialisation | |
void SERIAL_init(int baudrate) | |
{ | |
baudrate = ((F_CPU/(baudrate*8UL))-1); | |
UCSRA|=(1<<U2X); | |
UCSRB=(1<<RXEN)|(1<<TXEN); | |
UCSRC=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); | |
UBRRH=(baudrate >> 8) ; | |
UBRRL=baudrate; | |
} | |
/// Data transmission | |
void SERIAL_sendByte(char byte) | |
{ | |
while (!SERIAL_sendReady); | |
SERIAL_byte = byte; | |
} | |
char SERIAL_receiveByte() | |
{ | |
return SERIAL_receiveReady?SERIAL_byte:0; | |
} | |
char SERIAL_receiveBytePoll(unsigned int timeout) | |
{ | |
while (!SERIAL_receiveReady && timeout > 0) {timeout--;}; | |
return timeout?SERIAL_byte:0; | |
} | |
/// Serial printf | |
void SERIAL_printf(const char* format, ...) | |
{ | |
va_list argList; | |
va_start(argList, format); | |
char *buffer = malloc(SERIAL_printfBuffer); | |
vsprintf(buffer, format, argList); | |
va_end(argList); | |
while(*buffer) | |
{ | |
SERIAL_sendByte(*buffer++); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
Title: AVR UART library | |
Author: Robert-Maarten Schuurman <[email protected]> http://rms95.nl/ | |
Date: 28-06-2013 | |
Software: AVR-GCC 3.3 | |
Target: Any AVR device with a single UART and the registers named like | |
the ATmega32 (UCSRA-C, UBRRH and UBRRL). By modifying the code | |
a bit it is usable for any AVR UART. | |
DESCRIPTION | |
This file contains a function to set up the UART, to send and | |
receive bytes in a polling and non polling way. | |
There's also included a printf() function dedicated for the serial | |
connection, you can use this function like the normal printf() | |
USAGE | |
Just include these libraries, and if you need to, also change the | |
register names in the c file. At AVR microprocessors with multiple | |
UARTs it is usually required to put a number after the register name | |
but before the extension of the name: UCSRA --> UCSR0A or UCSR1A. | |
*****************************************************************************/ | |
#ifndef F_CPU | |
#define F_CPU 1000000 | |
#endif | |
#define SERIAL_receiveReady (!!(UCSRA&(1<<RXC))) | |
#define SERIAL_sendReady (!!(UCSRA&(1<<UDRE))) | |
#define SERIAL_byte UDR | |
#define SERIAL_printfBuffer 128 | |
#ifndef SERIAL_H_ | |
#define SERIAL_H_ | |
/// Initialisation | |
void SERIAL_init(int baudrate); | |
/// Data transmission | |
void SERIAL_sendByte(char byte); | |
char SERIAL_receiveByte(); | |
char SERIAL_receiveBytePoll(unsigned int timeout); | |
/// Serial printf | |
void SERIAL_printf(const char* format, ...); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment