Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion to solve system clock issue #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions ATmegaX8/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,82 @@
#include <avr/io.h>
#include <avr/interrupt.h>

#define BAUD_PRESCALE(baud) (((F_CPU / (baud * 8UL))) - 1)

/* baud settings */
static inline void
serial_baud_2400()
{
UCSR0A = (UCSR0A & ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0)))
| _BV(U2X0);
UBRR0 = 832;
UBRR0 = BAUD_PRESCALE(2400);
}

static inline void
serial_baud_4800()
{
UCSR0A = (UCSR0A & ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0)))
| _BV(U2X0);
UBRR0 = 416;
UBRR0 = BAUD_PRESCALE(4800);
}

static inline void
serial_baud_9600()
{
UCSR0A &= ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0) | _BV(U2X0));
UBRR0 = 103;
UBRR0 = BAUD_PRESCALE(9600);
}

static inline void
serial_baud_14400()
{
UCSR0A = (UCSR0A & ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0)))
| _BV(U2X0);
UBRR0 = 138;
UBRR0 = BAUD_PRESCALE(14400);
}

static inline void
serial_baud_19200()
{
UCSR0A &= ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0) | _BV(U2X0));
UBRR0 = 51;
UBRR0 = BAUD_PRESCALE(19200);
}

static inline void
serial_baud_28800()
{
UCSR0A = (UCSR0A & ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0)))
| _BV(U2X0);
UBRR0 = 68;
UBRR0 = BAUD_PRESCALE(28800);
}

static inline void
serial_baud_38400()
{
UCSR0A &= ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0) | _BV(U2X0));
UBRR0 = 25;
UBRR0 = BAUD_PRESCALE(38400);
}

static inline void
serial_baud_57600()
{
UCSR0A = (UCSR0A & ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0)))
| _BV(U2X0);
UBRR0 = 34;
UBRR0 = BAUD_PRESCALE(57600);
}

static inline void
serial_baud_250k()
{
UCSR0A &= ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0) | _BV(U2X0));
UBRR0 = 3;
UBRR0 = BAUD_PRESCALE(250000);
}

static inline void
serial_baud_500k()
{
UCSR0A &= ~(_BV(FE0) | _BV(DOR0) | _BV(UPE0) | _BV(U2X0));
UBRR0 = 1;
UBRR0 = BAUD_PRESCALE(500000);
}

/* bit size */
Expand Down