Serial Musical Organ 1.0
Buzzer controller and musical organ
Loading...
Searching...
No Matches
Macros
CPU.h File Reference

CPU configuration and definitions for ATmega168. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __AVR_ATmega168__
 ATmega168 microcontroller selection.
 
#define BAUD   9600UL
 Default USART baud rate for serial communication.
 
#define F_CPU   16000000UL
 CPU clock frequency in Hertz.
 

Detailed Description

CPU configuration and definitions for ATmega168.

Author
Roybel Carbonell Camejo
Date
2026-04-29
Version
1.0

This header file centralizes all CPU-specific configurations for the ATmega168 microcontroller. It defines the processor type, clock frequency, and default serial communication baud rate.

Usage

Include this file before any AVR-specific headers or other modules that depend on CPU definitions:

#include "includes/CPU.h"
#include <avr/io.h>
#include <util/delay.h>
CPU configuration and definitions for ATmega168.

Dependencies

Notes

Warning
If F_CPU is not defined correctly, timing functions like _delay_ms() will produce incorrect delays.
Changing BAUD requires matching configuration on the serial terminal side.

Definition in file CPU.h.

Macro Definition Documentation

◆ __AVR_ATmega168__

#define __AVR_ATmega168__

ATmega168 microcontroller selection.

Ensures the compiler uses the correct memory map, register definitions, and hardware-specific optimizations for the ATmega168.

This macro is:

  • Required by avr-libc headers
  • Used by avr-gcc for proper code generation
  • Essential for correct interrupt vector table mapping
Note
The conditional guard prevents redefinition if already set by compiler flags
See also
https://www.nongnu.org/avr-libc/user-manual/using_tools.html

Definition at line 52 of file CPU.h.

◆ BAUD

#define BAUD   9600UL

Default USART baud rate for serial communication.

Sets the serial communication speed to 9600 bits per second. This is a standard baud rate commonly used for microcontroller communication and terminal interfaces.

Range of typical baud rates for ATmega168 @ 16MHz:

  • 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200
Note
The actual achievable baud rate depends on F_CPU and UBRR register precision
Warning
Baud rate error should ideally be less than 2% for reliable communication

Calculation formula:

UBRR = (F_CPU / (16 * BAUD)) - 1
For F_CPU=16MHz, BAUD=9600: UBRR = (16000000 / (16 * 9600)) - 1 = 103.166 → 103
Error = 0.16% (well within acceptable range)
#define F_CPU
CPU clock frequency in Hertz.
Definition CPU.h:121
#define BAUD
Default USART baud rate for serial communication.
Definition CPU.h:79
See also
F_CPU Required for baud rate calculation
USART.h Where this BAUD definition is typically used
Examples
/home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/includes/CPU.h.

Definition at line 79 of file CPU.h.

◆ F_CPU

#define F_CPU   16000000UL

CPU clock frequency in Hertz.

Defines the operating frequency of the ATmega168 microcontroller as 16 MHz. This macro is critical for timing functions and peripheral configuration.

Important uses:

  • Used by <util/delay.h> for _delay_ms() and _delay_us() functions
  • Required for USART baud rate calculations
  • Used in timer/Counter configurations
  • Affects PWM frequency calculations
  • Used for SPI and I2C timing

For ATmega168 typical clock configurations:

  • 1 MHz (default internal RC oscillator)
  • 8 MHz (internal RC oscillator calibrated)
  • 16 MHz (external crystal, as used here)
  • 20 MHz (maximum recommended with external crystal)
Note
This value MUST match the actual hardware clock configuration
Warning
If F_CPU is incorrect:
  • _delay_ms() will be too fast or too slow
  • USART will have baud rate errors
  • Timer-based code will fail

Derivation for UBRR calculation:

UBRR = (F_CPU / (16 * BAUD)) - 1
= (16000000 / (16 * 9600)) - 1
= (16000000 / 153600) - 1
= 104.166 - 1
= 103.166 → 103 (integer)
See also
BAUD Used together with F_CPU for baud rate calculation
sleep.h Used for sleep mode timing
Examples
/home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/includes/CPU.h.

Definition at line 121 of file CPU.h.