AVRly - AVR Development Resources
usart.h
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file usart.h
25 * @ingroup usart
26 * @author Jason Duffy
27 * @date 15th March 2022
28 * @brief Driver file providing core USART communication between the target MCU
29 * and your PC. This file was adapted from Elliot Williams' Github repo
30 * hexagon5un (link in the see also section below).
31 * NOTE: This driver does involve blocking waits, this may be improved on in a
32 * later version.
33 * @bug No known bugs.
34 * @see https://github.com/hexagon5un/AVR-Programming
35 */
36
37
38#ifndef BAUD // If not defined in Makefile
39#define BAUD 9600 // Set a safe default baud rate
40#endif
41
42#ifndef USART_DOT_H
43#define USART_DOT_H
44
45#include <stdint.h>
46
47// These are defined for convenience
48#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0)
49#define USART_READY bit_is_set(UCSR0A, UDRE0)
50
51
52/**
53 * Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier,
54 * configures the hardware USART ready for use.
55 */
56void init_usart(void);
57
58
59/**
60 * Utility function to transmit a string.
61 * @param my_string is the string constant youd like to print, and should be
62 * enclosed in "" quotation marks.
63 */
64void usart_print_string(const char my_string[]);
65
66
67/**
68 * Define a string variable, pass it to this function. The string will contain.
69 * whatever you typed over serial.
70 * @param my_string is a pointer to the first element in the character array
71 * you'd like to store the received message in.
72 * @param max_length is the maximum number of characters expected.
73 */
74void usart_read_string(char my_string[], uint8_t max_length);
75
76
77/**
78 * Prints a byte out as its 3-digit ascii equivalent.
79 * @param byte is the 8 bits of data to be sent, must be unsigned.
80 */
81void usart_print_byte(uint8_t byte);
82
83
84/**
85 * Prints a byte out as its 1-digit ascii equivalent.
86 * @param byte is the 8 bits of data to be sent, must be unsigned, with a
87 * value of 0-9;
88 */
89void usart_print_decimal_digit(uint8_t byte);
90
91
92/**
93 * Prints a byte out as its 1-character ascii equivalent.
94 * @param byte is the character to be sent.
95 */
96void usart_print_char(char byte);
97
98
99/**
100 * Prints a word (16-bits) out as its 5-digit ascii equivalent.
101 * @param word is the 16 bits of data to be sent, must be unsigned.
102 */
103void usart_print_word(uint16_t word);
104
105
106/**
107 * Prints a byte out in 1s and 0s.
108 * @param byte is the 8 bits of data to be sent, must be unsigned.
109 */
110void usart_print_binary_byte(uint8_t byte);
111
112
113/**
114 * Convert a nibble to a hex character.
115 * @param nibble is the 4 bits of data to be sent, must be unsigned.
116 */
117char usart_nibble_to_hex_character(uint8_t nibble);
118
119
120/**
121 * Prints a byte out in hexadecimal format.
122 * @param byte is the 8 bits of data to be sent, must be unsigned.
123 */
124void usart_print_hex_byte(uint8_t byte);
125
126
127/**
128 * Takes in up to three ascii digits, converts them to a byte when press enter.
129 * @returns an unsigned 8 bit integer is returned - this is the data received.
130 */
131uint8_t usart_get_number(void);
132
133
134#endif // USART_DOT_H
135
136/*** end of file ***/
void usart_print_decimal_digit(uint8_t byte)
Prints a byte out as its 1-digit ascii equivalent.
Definition: usart.c:135
void usart_print_byte(uint8_t byte)
Prints a byte out as its 3-digit ascii equivalent.
Definition: usart.c:121
void usart_print_string(const char my_string[])
Utility function to transmit a string.
Definition: usart.c:74
void usart_print_binary_byte(uint8_t byte)
Prints a byte out in 1s and 0s.
Definition: usart.c:169
void usart_print_word(uint16_t word)
Prints a word (16-bits) out as its 5-digit ascii equivalent.
Definition: usart.c:155
void usart_read_string(char my_string[], uint8_t max_length)
Define a string variable, pass it to this function.
Definition: usart.c:92
char usart_nibble_to_hex_character(uint8_t nibble)
Convert a nibble to a hex character.
Definition: usart.c:190
uint8_t usart_get_number(void)
Takes in up to three ascii digits, converts them to a byte when press enter.
Definition: usart.c:221
void usart_print_char(char byte)
Prints a byte out as its 1-character ascii equivalent.
Definition: usart.c:145
void init_usart(void)
Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART ...
Definition: usart.c:50
void usart_print_hex_byte(uint8_t byte)
Prints a byte out in hexadecimal format.
Definition: usart.c:207