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