px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
uart.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2006-2014 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13============================================================================= */
14
15/**
16 * @ingroup AVR_TUTORIALS
17 * @defgroup AVR_TUT04_UART 04_uart
18 *
19 * How to configure and use a UART in a polled fashion.
20 *
21 * File(s):
22 * - arch/avr/tutorials/04_uart/uart.c
23 *
24 * The UART is configured to 115200 BAUD, 8 Data Bits, No Parity and 1 Stop Bit.
25 * The string "UART\r\n" is sent upon start up. Thereafter all received
26 * characters are echoed back to the sender.
27 *
28 * The clock rate of the AVR is defined in "px_board.h" with the macro @b F_CPU.
29 * It is a good programming practice to precalculate values at compile
30 * time, instead of using "magic values". For example, the value to use for
31 * the UBBR register is calculated with the following macro:
32 *
33 * @code{.c}
34 * #define UART0_UBBR_VAL ((F_CPU / (UART0_BAUD * 16)) - 1)
35 * @endcode
36 *
37 * instead of using a hard coded value:
38 *
39 * @code{.c}
40 * #define UART0_UBBR_VAL 3 // Magic Value for 115200 Baud at a clock rate of 7.3728 MHz
41 * @endcode
42 */
43
44#include <stdint.h>
45#include <stdbool.h>
46#include <avr/io.h>
47
48#include "px_board.h"
49
50// Define baud rate
51#define UART0_BAUD 115200ul
52#define UART0_UBBR_VAL ((F_CPU / (UART0_BAUD * 16)) - 1)
53
54void uart_init(void)
55{
56 // Set baud rate
57 UBRR0 = UART0_UBBR_VAL;
58 // Set frame format to 8 data bits, no parity, 1 stop bit
59 UCSR0C = (0 << USBS0) | (1 << UCSZ01) | (1 << UCSZ00);
60
61 // Enable receiver and transmitter
62 UCSR0B = (1 << RXEN0) | (1 << TXEN0);
63}
64
65void uart_send_byte(uint8_t data)
66{
67 // Wait until data register is empty
68 while((UCSR0A & (1 << UDRE0)) == 0) {;}
69 // Load transmit register with new data
70 UDR0 = data;
71}
72
73uint8_t uart_receive_byte(void)
74{
75 // Wait until a byte has been received
76 while((UCSR0A & (1 << RXC0)) == 0) {;}
77 // Return received data
78 return UDR0;
79}
80
81int main(void)
82{
83 uint8_t data;
84
85 // Initialise board
87 // Initialise UART
88 uart_init();
89
90 // Send string
91 uart_send_byte('U');
92 uart_send_byte('A');
93 uart_send_byte('R');
94 uart_send_byte('T');
95 uart_send_byte('\r');
96 uart_send_byte('\n');
97
98 // Loop forever
99 while(true)
100 {
101 // Echo received characters
102 data = uart_receive_byte();
103 uart_send_byte(data);
104 }
105}
void px_board_init(void)
Initialise the board hardware.
Definition: px_board.c:168