AVRly - AVR Development Resources
usart.h File Reference

Driver file providing core USART communication between the target MCU and your PC. More...

#include <stdint.h>

Go to the source code of this file.

Macros

#define BAUD   9600
 
#define USART_HAS_DATA   bit_is_set(UCSR0A, RXC0)
 
#define USART_READY   bit_is_set(UCSR0A, UDRE0)
 

Functions

void init_usart (void)
 Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART. 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 (uint8_t nibble)
 Convert nibble to hex.
 
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.h.

Macro Definition Documentation

◆ BAUD

#define BAUD   9600

Definition at line 36 of file usart.h.

◆ USART_HAS_DATA

#define USART_HAS_DATA   bit_is_set(UCSR0A, RXC0)

Definition at line 45 of file usart.h.

◆ USART_READY

#define USART_READY   bit_is_set(UCSR0A, UDRE0)

Definition at line 46 of file usart.h.

Function Documentation

◆ init_usart()

void init_usart ( void  )

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

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}

◆ print_string()

void print_string ( const char  myString[])

Utility function to transmit an entire string from RAM.

Definition at line 67 of file usart.c.

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

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

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}

◆ print_byte()

void print_byte ( uint8_t  byte)

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

Definition at line 106 of file usart.c.

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}

◆ print_word()

void print_word ( uint16_t  word)

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

Definition at line 115 of file usart.c.

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}

◆ print_binary_byte()

void print_binary_byte ( uint8_t  byte)

Prints a byte out in 1s and 0s.

Definition at line 125 of file usart.c.

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}

◆ nibble_to_hex_character()

char nibble_to_hex_character ( uint8_t  nibble)

Convert nibble to hex character.

Definition at line 142 of file usart.c.

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

◆ print_hex_byte()

void print_hex_byte ( uint8_t  byte)

Prints a byte out in hexadecimal.

Definition at line 155 of file usart.c.

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

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}