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 transmit_byte('0' + (byte / 100)); // Hundreds
125 transmit_byte('0' + ((byte / 10) % 10)); // Tens
126 transmit_byte('0' + (byte % 10)); // Ones
127}

◆ 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 135 of file usart.c.

136{
137 transmit_byte(byte);
138}

◆ 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 145 of file usart.c.

146{
147 transmit_byte(byte);
148}

◆ 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 155 of file usart.c.

156{
157 transmit_byte('0' + (word / 10000)); // Ten-thousands
158 transmit_byte('0' + ((word / 1000) % 10)); // Thousands
159 transmit_byte('0' + ((word / 100) % 10)); // Hundreds
160 transmit_byte('0' + ((word / 10) % 10)); // Tens
161 transmit_byte('0' + (word % 10)); // Ones
162}

◆ 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 169 of file usart.c.

170{
171 uint8_t bit;
172 for (bit = 7; bit < 255; bit--)
173 {
174 if (bit_is_set(byte, bit))
175 {
176 transmit_byte('1');
177 }
178 else
179 {
180 transmit_byte('0');
181 }
182 }
183}

◆ 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 190 of file usart.c.

191{
192 if (nibble < 10)
193 {
194 return ('0' + nibble);
195 }
196 else
197 {
198 return ('A' + nibble - 10);
199 }
200}

◆ 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 207 of file usart.c.

208{
209 uint8_t nibble;
210 nibble = (byte & 0b11110000) >> 4;
211 transmit_byte(usart_nibble_to_hex_character(nibble));
212 nibble = byte & 0b00001111;
213 transmit_byte(usart_nibble_to_hex_character(nibble));
214}
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 221 of file usart.c.

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