AVRly - AVR Development Resources

Driver file providing core USART communication between the target MCU and your PC. This file was adapted from Elliot Williams' Github repo hexagon5un. (link in the see also section below). More...

#include <util/setbaud.h>
#include <avr/io.h>
#include "usart.h"

Go to the source code of this file.

Functions

void init_usart (void)
 Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART ready for use. More...
 
void usart_print_string (const char myString[])
 Utility function to transmit a string. More...
 
void usart_read_string (char myString[], uint8_t maxLength)
 Define a string variable, pass it to this function. More...
 
void usart_print_byte (uint8_t byte)
 Prints a byte out as its 3-digit ascii equivalent. More...
 
void usart_print_decimal_digit (uint8_t byte)
 Prints a byte out as its 1-digit ascii equivalent. More...
 
void usart_print_char (char byte)
 Prints a byte out as its 1-character ascii equivalent. More...
 
void usart_print_word (uint16_t word)
 Prints a word (16-bits) out as its 5-digit ascii equivalent. More...
 
void usart_print_binary_byte (uint8_t byte)
 Prints a byte out in 1s and 0s. More...
 
char usart_nibble_to_hex_character (uint8_t nibble)
 Convert a nibble to a hex character. More...
 
void usart_print_hex_byte (uint8_t byte)
 Prints a byte out in hexadecimal format. More...
 
uint8_t usart_get_number (void)
 Takes in up to three ascii digits, converts them to a byte when press enter. More...
 

Detailed Description

Driver file providing core USART communication between the target MCU and your PC. This file was adapted from Elliot Williams' Github repo hexagon5un. (link in the see also section below).

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
15th March 2022
See also
https://github.com/hexagon5un/AVR-Programming

Definition in file usart.c.

Function Documentation

◆ init_usart()

void init_usart ( void  )

Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART ready for use.

Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART.

Definition at line 50 of file usart.c.

51{
52 // defined in setbaud.h
53 UBRR0H = UBRRH_VALUE;
54 UBRR0L = UBRRL_VALUE;
55
56#if USE_2X
57 UCSR0A |= (1 << U2X0);
58#else
59 UCSR0A &= ~(1 << U2X0);
60#endif
61
62 // Enable USART transmitter/receiver
63 UCSR0B = (1 << TXEN0) | (1 << RXEN0);
64 // 8 data bits, 1 stop bit
65 UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
66}

◆ usart_print_string()

void usart_print_string ( const char  my_string[])

Utility function to transmit a string.

Parameters
my_stringis the string constant youd like to print, and should be enclosed in "" quotation marks.

Definition at line 74 of file usart.c.

75{
76 uint8_t i = 0;
77 while (myString[i])
78 {
79 transmit_byte(myString[i]);
80 i++;
81 }
82}

◆ usart_read_string()

void usart_read_string ( char  my_string[],
uint8_t  max_length 
)

Define a string variable, pass it to this function.

The string will contain. whatever you typed over serial.

Parameters
my_stringis a pointer to the first element in the character array you'd like to store the received message in.
max_lengthis the maximum number of characters expected.

Definition at line 92 of file usart.c.

93{
94 char response;
95 uint8_t count = 0;
96
97 while (count < (maxLength - 1))
98 {
99 response = receive_byte();
100 transmit_byte(response); // echo
101
102 if (response == '\r') // enter marks the end
103 {
104 break;
105 }
106
107 else
108 {
109 myString[count] = response; // add in a letter
110 ++count;
111 }
112 }
113 myString[count] = 0; // terminal NULL character */
114}

◆ usart_print_byte()

void usart_print_byte ( uint8_t  byte)

Prints a byte out as its 3-digit ascii equivalent.

Parameters
byteis the 8 bits of data to be sent, must be unsigned.

Definition at line 121 of file usart.c.

122{
123 // Converts a byte to a string of decimal text, sends it
124 if (byte > 99)
125 {
126 transmit_byte('0' + (byte / 100)); // Hundreds
127 }
128 if (byte > 9)
129 {
130 transmit_byte('0' + ((byte / 10) % 10)); // Tens
131 }
132 transmit_byte('0' + (byte % 10)); // Ones
133}

◆ usart_print_decimal_digit()

void usart_print_decimal_digit ( uint8_t  byte)

Prints a byte out as its 1-digit ascii equivalent.

Parameters
byteis the 8 bits of data to be sent, must be unsigned, with a value of 0-9;

Definition at line 141 of file usart.c.

142{
143 transmit_byte(byte);
144}

◆ usart_print_char()

void usart_print_char ( char  byte)

Prints a byte out as its 1-character ascii equivalent.

Parameters
byteis the character to be sent.

Definition at line 151 of file usart.c.

152{
153 transmit_byte(byte);
154}

◆ usart_print_word()

void usart_print_word ( uint16_t  word)

Prints a word (16-bits) out as its 5-digit ascii equivalent.

Parameters
wordis the 16 bits of data to be sent, must be unsigned.

Definition at line 161 of file usart.c.

162{
163 transmit_byte('0' + (word / 10000)); // Ten-thousands
164 transmit_byte('0' + ((word / 1000) % 10)); // Thousands
165 transmit_byte('0' + ((word / 100) % 10)); // Hundreds
166 transmit_byte('0' + ((word / 10) % 10)); // Tens
167 transmit_byte('0' + (word % 10)); // Ones
168}

◆ usart_print_binary_byte()

void usart_print_binary_byte ( uint8_t  byte)

Prints a byte out in 1s and 0s.

Parameters
byteis the 8 bits of data to be sent, must be unsigned.

Definition at line 175 of file usart.c.

176{
177 uint8_t bit;
178 for (bit = 7; bit < 255; bit--)
179 {
180 if (bit_is_set(byte, bit))
181 {
182 transmit_byte('1');
183 }
184 else
185 {
186 transmit_byte('0');
187 }
188 }
189}

◆ usart_nibble_to_hex_character()

char usart_nibble_to_hex_character ( uint8_t  nibble)

Convert a nibble to a hex character.

Parameters
nibbleis the 4 bits of data to be sent, must be unsigned.

Definition at line 196 of file usart.c.

197{
198 if (nibble < 10)
199 {
200 return ('0' + nibble);
201 }
202 else
203 {
204 return ('A' + nibble - 10);
205 }
206}

◆ usart_print_hex_byte()

void usart_print_hex_byte ( uint8_t  byte)

Prints a byte out in hexadecimal format.

Parameters
byteis the 8 bits of data to be sent, must be unsigned.

Definition at line 213 of file usart.c.

214{
215 uint8_t nibble;
216 nibble = (byte & 0b11110000) >> 4;
217 transmit_byte(usart_nibble_to_hex_character(nibble));
218 nibble = byte & 0b00001111;
219 transmit_byte(usart_nibble_to_hex_character(nibble));
220}
char usart_nibble_to_hex_character(uint8_t nibble)
Convert a nibble to a hex character.
Definition: usart.c:190

◆ usart_get_number()

uint8_t usart_get_number ( void  )

Takes in up to three ascii digits, converts them to a byte when press enter.

Returns
an unsigned 8 bit integer is returned - this is the data received.

Definition at line 227 of file usart.c.

228{
229 char hundreds = '0';
230 char tens = '0';
231 char ones = '0';
232 char thisChar = '0';
233 do
234 {
235 hundreds = tens;
236 tens = ones;
237 ones = thisChar;
238 thisChar = receive_byte(); // get a new character
239 transmit_byte(thisChar); // echo
240 }
241 while (thisChar != '\r'); // until type return
242
243 // TODO: Bracketise this further
244 return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0');
245}