A lightweight, transmit-only UART debugging library designed for the ATmega328PB microcontroller using UART1. It supports printing strings, integers, and floating-point numbers. This library is intended for bare-metal development in Microchip Studio and is compatible with AVR-GCC.
- Buffered UART1 transmission using a ring buffer (size: 100 bytes).
- Functions:
debugPrint
,debugPrintln
,debugPrintInt
,debugPrintIntln
,debugPrintFloat
,debugPrintFloatln
. - Configurable baud rate.
- Transmit-only: Does not support receiving data.
- Designed for ATmega328PB (which has two UARTs: UART0 and UART1).
- Adaptable for ATmega328P (which has only UART0) by modifying register names.
The library uses the ATmega328PB’s UART1 module to transmit data serially. UART (Universal Asynchronous Receiver/Transmitter) sends data as a series of bits over a single wire (TX pin, PD3 on ATmega328PB). The library:
- Configures UART1 with a specified baud rate, 8-bit data, no parity, and 1 stop bit.
- Uses a ring buffer to store outgoing data, allowing non-blocking writes.
- Employs interrupts (
USART1_UDRE_vect
) to transmit data when the UART data register (UDR1
) is empty. - Does not support receiving data, as it only enables the transmitter (
TXEN1
).
Connect the TX pin to a serial-to-USB adapter or similar device to view output in a serial monitor (e.g., PuTTY, Tera Term or Microchip Studio’s Data Visualizer).
Clone this repository or download the ZIP from GitHub Releases.
- Create a new project in Microchip Studio:
File > New > Project > GCC C Executable Project
. - Select your microcontroller (e.g., ATmega328PB).
- Copy the
src
folder (containingdebugSerial.h
anddebugSerial.cpp
) to your project directory. - In Solution Explorer, right-click the project >
Add > Existing Item
. - Select
debugSerial.h
anddebugSerial.cpp
from thesrc
folder.
The library requires F_CPU
to match your microcontroller’s clock frequency (e.g., 8000000UL
for 8 MHz or 16000000UL
for 16 MHz) for accurate baud rate calculations.
- Go to
Project > Properties > Toolchain > AVR/GNU C Compiler > Symbols
. - Add a symbol:
F_CPU=16000000UL
(orF_CPU=8000000UL
).
#define F_CPU 16000000UL
#include "debugSerial.h"
- Include the library:
#include "debugSerial.h"
- Initialize UART1 with
debugSerialBegin(9600)
- Use print functions (e.g.,
debugPrintln("Hello")
). - See the
examples/debugExample/main.c
for a sample program.
The ATmega328PB has two UARTs (UART0 and UART1), but the ATmega328P has only one (UART0). To use this library with ATmega328P:
- Open
debugSerial.cpp
- Replace the following UART1 registers with UART0 equivalents:
UBRR1H → UBRR0H
UBRR1L → UBRR0L
UCSR1A → UCSR0A
UCSR1B → UCSR0B
UCSR1C → UCSR0C
UDR1 → UDR0
USART1_UDRE_vect → USART_UDRE_vect
- Update the TX pin to PD1 (UART0 TX on ATmega328P).
- Recompile and upload.
- Transmit-Only: The library does not support receiving data.
- UART1-Specific: Designed for ATmega328PB’s UART1. Modification required for other UARTs or microcontrollers.