AVRly - AVR Development Resources
usart.c
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.c
25 * @ingroup ccs811
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.
30 * @bug No known bugs.
31 * @see
32 */
33
34#include <util/setbaud.h>
35#include <avr/io.h>
36
37#include "usart.h"
38
39static void transmit_byte(uint8_t data);
40static uint8_t receive_byte(void);
41
42
43/*
44 * Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier,
45 * configures the hardware USART.
46 */
47void init_usart(void)
48{
49 // defined in setbaud.h
50 UBRR0H = UBRRH_VALUE;
51 UBRR0L = UBRRL_VALUE;
52
53#if USE_2X
54 UCSR0A |= (1 << U2X0);
55#else
56 UCSR0A &= ~(1 << U2X0);
57#endif
58
59 // Enable USART transmitter/receiver
60 UCSR0B = (1 << TXEN0) | (1 << RXEN0);
61 // 8 data bits, 1 stop bit
62 UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
63}
64
65// Utility function to transmit an entire string from RAM.
66void print_string(const char myString[])
67{
68 uint8_t i = 0;
69 while (myString[i])
70 {
71 transmit_byte(myString[i]);
72 i++;
73 }
74}
75
76/*
77 * Define a string variable, pass it to this function. The string will contain
78 * whatever you typed over serial.
79 */
80void read_string(char myString[], uint8_t maxLength)
81{
82 char response;
83 uint8_t count = 0;
84
85 while (count < (maxLength - 1))
86 {
87 response = receive_byte();
88 transmit_byte(response); // echo
89
90 if (response == '\r') // enter marks the end
91 {
92 break;
93 }
94
95 else
96 {
97 myString[count] = response; // add in a letter
98 ++count;
99 }
100 }
101 myString[count] = 0; // terminal NULL character */
102}
103
104// Prints a byte out as its 3-digit ascii equivalent.
105void print_byte(uint8_t byte)
106{
107 // Converts a byte to a string of decimal text, sends it
108 transmit_byte('0' + (byte / 100)); // Hundreds
109 transmit_byte('0' + ((byte / 10) % 10)); // Tens
110 transmit_byte('0' + (byte % 10)); // Ones
111}
112
113// Prints a word (16-bits) out as its 5-digit ascii equivalent.
114void print_word(uint16_t word)
115{
116 transmit_byte('0' + (word / 10000)); // Ten-thousands
117 transmit_byte('0' + ((word / 1000) % 10)); // Thousands
118 transmit_byte('0' + ((word / 100) % 10)); // Hundreds
119 transmit_byte('0' + ((word / 10) % 10)); // Tens
120 transmit_byte('0' + (word % 10)); // Ones
121}
122
123// Prints a byte out in 1s and 0s.
124void print_binary_byte(uint8_t byte)
125{
126 uint8_t bit;
127 for (bit = 7; bit < 255; bit--)
128 {
129 if (bit_is_set(byte, bit))
130 {
131 transmit_byte('1');
132 }
133 else
134 {
135 transmit_byte('0');
136 }
137 }
138}
139
140// Converts a nibble to a hexadecimal character
141char nibble_to_hex_character(uint8_t nibble)
142{
143 if (nibble < 10)
144 {
145 return ('0' + nibble);
146 }
147 else
148 {
149 return ('A' + nibble - 10);
150 }
151}
152
153// Prints a byte as its hexadecimal equivalent
154void print_hex_byte(uint8_t byte)
155{
156 uint8_t nibble;
157 nibble = (byte & 0b11110000) >> 4;
158 transmit_byte(nibble_to_hex_character(nibble));
159 nibble = byte & 0b00001111;
160 transmit_byte(nibble_to_hex_character(nibble));
161}
162
163 // Gets a numerical 0-255 from the serial port, converts string to number.
164uint8_t get_number(void)
165{
166 char hundreds = '0';
167 char tens = '0';
168 char ones = '0';
169 char thisChar = '0';
170 do
171 {
172 hundreds = tens;
173 tens = ones;
174 ones = thisChar;
175 thisChar = receive_byte(); // get a new character
176 transmit_byte(thisChar); // echo
177 }
178 while (thisChar != '\r'); // until type return
179
180 // TODO: Bracketise this further
181 return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0');
182}
183
184// TODO: Replace these with non-blocking waits
185//Blocking transmit and receive functions.
186static void transmit_byte(uint8_t data)
187{
188 // Wait for empty transmit buffer
189 loop_until_bit_is_set(UCSR0A, UDRE0);
190 // Send Data
191 UDR0 = data;
192}
193
194static uint8_t receive_byte(void)
195{
196 // Wait for incoming data
197 loop_until_bit_is_set(UCSR0A, RXC0);
198 //return register value
199 return UDR0;
200}
201
202
203/*** end of file ***/
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 print_word(uint16_t word)
Prints a word (16-bits) out as its 5-digit ascii equivalent.
Definition: usart.c:115
void print_string(const char myString[])
Utility function to transmit an entire string from RAM.
Definition: usart.c:67
uint8_t get_number(void)
Takes in up to three ascii digits, converts them to a byte when press enter.
Definition: usart.c:165
void print_binary_byte(uint8_t byte)
Prints a byte out in 1s and 0s.
Definition: usart.c:125
char nibble_to_hex_character(uint8_t nibble)
Convert nibble to hex character.
Definition: usart.c:142
void read_string(char myString[], uint8_t maxLength)
Define a string variable, pass it to this function.
Definition: usart.c:81
void print_hex_byte(uint8_t byte)
Prints a byte out in hexadecimal.
Definition: usart.c:155
void print_byte(uint8_t byte)
Prints a byte out as its 3-digit ascii equivalent.
Definition: usart.c:106
Driver file providing core USART communication between the target MCU and your PC.