AVRly - AVR Development Resources
usart.c File Reference

Driver file providing core USART communication between the target MCU and your PC. 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 print_string (const char myString[])
 Utility function to transmit an entire string from RAM. More...
 
void read_string (char myString[], uint8_t maxLength)
 Define a string variable, pass it to this function. More...
 
void print_byte (uint8_t byte)
 Prints a byte out as its 3-digit ascii equivalent. More...
 
void print_word (uint16_t word)
 Prints a word (16-bits) out as its 5-digit ascii equivalent. More...
 
void print_binary_byte (uint8_t byte)
 Prints a byte out in 1s and 0s. More...
 
char nibble_to_hex_character (uint8_t nibble)
 Convert nibble to hex character. More...
 
void print_hex_byte (uint8_t byte)
 Prints a byte out in hexadecimal. More...
 
uint8_t 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.

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

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

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}

◆ print_string()

void print_string ( const char  myString[])

Utility function to transmit an entire string from RAM.

Definition at line 66 of file usart.c.

67{
68 uint8_t i = 0;
69 while (myString[i])
70 {
71 transmit_byte(myString[i]);
72 i++;
73 }
74}

◆ read_string()

void read_string ( char  myString[],
uint8_t  maxLength 
)

Define a string variable, pass it to this function.

The string will contain whatever you typed over serial.

Definition at line 80 of file usart.c.

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}

◆ print_byte()

void print_byte ( uint8_t  byte)

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

Definition at line 105 of file usart.c.

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}

◆ print_word()

void print_word ( uint16_t  word)

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

Definition at line 114 of file usart.c.

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}

◆ print_binary_byte()

void print_binary_byte ( uint8_t  byte)

Prints a byte out in 1s and 0s.

Definition at line 124 of file usart.c.

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}

◆ nibble_to_hex_character()

char nibble_to_hex_character ( uint8_t  nibble)

Convert nibble to hex character.

Definition at line 141 of file usart.c.

142{
143 if (nibble < 10)
144 {
145 return ('0' + nibble);
146 }
147 else
148 {
149 return ('A' + nibble - 10);
150 }
151}

◆ print_hex_byte()

void print_hex_byte ( uint8_t  byte)

Prints a byte out in hexadecimal.

Definition at line 154 of file usart.c.

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}
char nibble_to_hex_character(uint8_t nibble)
Convert nibble to hex character.
Definition: usart.c:142

◆ get_number()

uint8_t get_number ( void  )

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

Definition at line 164 of file usart.c.

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}