mirror of
https://github.com/ptrks/avr-usart-printf.git
synced 2025-07-08 19:31:02 +03:00
Added initial files
This commit is contained in:
32
usart_printf/usart_printf.c
Normal file
32
usart_printf/usart_printf.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/sfr_defs.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define BAUD_RATE 9600
|
||||
#define BAUD_PRESCALE (F_CPU/16/BAUD_RATE-1)
|
||||
|
||||
void usart_initialize(void) {
|
||||
UBRR0H = (BAUD_PRESCALE >> 8);
|
||||
UBRR0L = BAUD_PRESCALE;
|
||||
|
||||
//Enable receive and transmit
|
||||
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
|
||||
|
||||
// Set frame to 8 bits
|
||||
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
|
||||
}
|
||||
|
||||
void usart_send_byte(char byte, FILE *stream) {
|
||||
if (byte == '\n') {
|
||||
usart_send_byte('\r',stream);
|
||||
}
|
||||
loop_until_bit_is_set(UCSR0A,UDRE0);
|
||||
UDR0 = byte;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char usart_get_byte(FILE *stream) {
|
||||
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||
return UDR0;
|
||||
}
|
18
usart_printf/usart_printf.h
Normal file
18
usart_printf/usart_printf.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef USART_PRINTF_H
|
||||
#define USART_PRINTF_H
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
void usart_send_byte(char byte, FILE *stream);
|
||||
char usart_get_byte(FILE *stream);
|
||||
void usart_initialize(void);
|
||||
|
||||
|
||||
FILE uart_output = FDEV_SETUP_STREAM(usart_send_byte, NULL, _FDEV_SETUP_WRITE);
|
||||
FILE uart_input = FDEV_SETUP_STREAM(NULL, usart_get_byte, _FDEV_SETUP_READ);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user