Last active
July 20, 2016 00:11
-
-
Save Jacajack/6fdbed4eaf9f733e85527b1a73722823 to your computer and use it in GitHub Desktop.
UART snippet for ATmega328
This file contains 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
#include <avr/io.h> | |
#include <stdlib.h> | |
#include <util/delay.h> | |
#include "../include/uart.h" | |
//Version for atmega328 | |
//Not used, just declared for user | |
char *uartBuffer; | |
static unsigned int uartBaud; | |
void uartSendChar( char c ) | |
{ | |
//Send single byte through UART | |
//c - character to send | |
while ( !( UCSR0A & ( 1 << UDRE0 ) ) ); | |
UDR0 = c; | |
} | |
char uartReadChar( unsigned int timeout ) | |
{ | |
//Receive and return single byte | |
//Returned value: received character | |
unsigned int i = 0; | |
for ( i = 0; i < timeout; i++ ) | |
{ | |
if ( UCSR0A & ( 1 << RXC0 ) ) | |
return UDR0; | |
_delay_us( 1000000.0 / (double)UART_BAUD * 10.0 ); | |
} | |
return 0; | |
} | |
void uartSend( const char *s ) | |
{ | |
//Send character word through UART | |
//s - pointer to string | |
size_t i = 0; | |
while ( s[i] ) | |
uartSendChar( s[i++] ); | |
} | |
size_t uartRead( char *buff, size_t bufferSize, unsigned int timeout ) | |
{ | |
//Receive 10/13/0 terminated string and store it in buffer | |
unsigned int j; | |
size_t i = 0; | |
for ( j = 0; j < timeout; j++ ) | |
{ | |
if ( UCSR0A & ( 1 << RXC0 ) ) | |
{ | |
buff[i] = UDR0; | |
j = 0; | |
i += i + 1 >= bufferSize ? 0 : 1; | |
} | |
_delay_us( 1000000.0 / (double)UART_BAUD * 10.0 ); | |
} | |
buff[i] = 0; | |
return i; | |
} | |
char uartStatus( ) | |
{ | |
//Is uart input buffer empty? | |
return UCSR0A & ( 1 << RXC0 ); | |
} | |
void uartInit( ) | |
{ | |
//Init UART with given BAUD rate | |
//baud - baud rate | |
unsigned int baud = UART_BAUD; | |
baud = F_CPU / 16 / baud - 1; | |
UBRR0H = (unsigned char) ( baud >> 8 ); //Set BAUD rate | |
UBRR0L = (unsigned char) baud; | |
UCSR0B = ( 1 << RXEN0 ) | ( 1 << TXEN0 ); //Enable RX and TX | |
UCSR0C = ( 0 << USBS0 ) | ( 3 << UCSZ00 ); //Set data format | |
} |
This file contains 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
#ifndef UART_H | |
#define UART_H | |
#include <stdlib.h> | |
#ifndef UART_BAUD | |
#define UART_BAUD 9600 | |
#endif | |
extern void uartInit( ); | |
extern void uartSendChar( char c ); | |
extern void uartSend( const char *s ); | |
extern char uartReadChar( unsigned int timeout ); | |
extern size_t uartRead( char *buff, size_t bufferSize, unsigned int timeout ); | |
extern char uartStatus( ); | |
extern char *uartBuffer; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment