This utility provides functions to print pin states and debug AVR microcontrollers in a Linux terminal via UART. It allows developers to inspect register values and individual pin statuses easily.
Prints the binary representation of an 8-bit AVR register or pin value to the terminal.
- Parameters:
register_name: A string (e.g.,"PINB") to label the output for clarity.value: The 8-bit value (e.g.,PINB) to be displayed in binary format.
- Output: Prints the register name followed by its binary value (e.g.,
PINB: 10110011). - Example:
Output in terminal:
print_binary("PINB", PINB);
PINB: 10110011
Standard C-style formatted output function, redirected to UART for terminal display.
- Parameters:
format: A format string (e.g.,"PD2 status: %d\n") specifying the output format....: Additional arguments (e.g.,pin_value) to be formatted.
- Output: Prints formatted text to the terminal via UART.
- Example:
Output in terminal:
printf("PD2 status: %d\n", pin_value);
PD2 status: 1
To use this utility, initialize UART and redirect the standard output to the UART interface:
-
Include the UART utility: Include the
uart.cfile, which contains the UART initialization and output functions.#include "uart.c"
-
Initialize UART: Call
uart_init()to configure the UART module with the appropriate baud rate and settings.uart_init(); -
Redirect stdout to UART: Set the standard output stream (
stdout) to the UART output stream (uart_output) to send allprintfoutputs to the terminal.stdout = &uart_output;
#include <avr/io.h>
#include "uart.c"
int main(void) {
// Initialize UART
uart_init();
stdout = &uart_output;
// Example usage
print_binary("PINB", PINB); // Print PINB register in binary
printf("PD2 status: %d\n", (PIND & (1 << PD2)) >> PD2); // Print PD2 pin status
while (1) {
// Your application code
}
return 0;
}- Ensure
uart.cis in your project directory and configured for your AVR microcontroller (e.g., correct baud rate, clock frequency). - The
pin_valueused inprintfshould be derived from the appropriate register (e.g.,PIND & (1 << PD2)for PD2). - This utility assumes a Linux terminal with a serial monitor (e.g.,
minicom,screen) configured to receive UART output. - Verify your AVR’s clock frequency and UART settings match the
uart.cimplementation to avoid communication issues.
