| 1 |
|
| 2 |
/* uart.c -- routines for manipulating the ATmega16 UART. |
| 3 |
|
| 4 |
Copyright (C) 2008 Ajith Kumar, Inter-University Accelerator Centre, |
| 5 |
New Delhi and Pramode C.E, GnuVision.com. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <avr/io.h> |
| 14 |
|
| 15 |
#define CPU_CLOCK 8000000 // 8 MHz clock is assumed |
| 16 |
#define COMPUTE_BAUD(b) ((uint32_t)(CPU_CLOCK)/((uint32_t)(b)*16) - 1) |
| 17 |
|
| 18 |
|
| 19 |
//Initialise UART: format 8 data bits, No parity, 1 stop bit |
| 20 |
void uart_init(uint16_t baud) |
| 21 |
{ |
| 22 |
UCSRB = (1 << TXEN) | (1 << RXEN); |
| 23 |
UBRRH = (COMPUTE_BAUD(baud) >> 8) & 0xff; |
| 24 |
UBRRL = (COMPUTE_BAUD(baud)) & 0xff; |
| 25 |
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
uint8_t uart_recv_byte(void) |
| 30 |
{ |
| 31 |
while( !(UCSRA & (1 <<RXC)) ); |
| 32 |
return UDR; |
| 33 |
} |
| 34 |
|
| 35 |
void uart_send_byte(uint8_t c) |
| 36 |
{ |
| 37 |
while( !(UCSRA & (1 <<UDRE) ) ); |
| 38 |
UDR = c; |
| 39 |
} |
| 40 |
|