AT328 Handout
AT328 Handout
1 Introduction
This document is a short introduction to the architecture of the Microchip ATmega328P microcontroller
and provides some information on using it in EE 459 projects. Additional documents on the EE 459 web
site describe using the C software development system. For more complete information on any of the topics
below, see the full Microchip datasheet or programming manual.
Note: The ATmega328P microcontroller used to be made by Atmel until Atmel was acquired by Microchip
Technology in 2016. Some documents on the EE459 web site may still refer to it as Atmel ATmega328P,
but it’s the same microcontroller and only the company name has changed.
The Microchip ATmega328P is one member of the Microchip AVR 8-bit microcontroller family. It is the
microcontroller that is used on the Arduino Uno although this document is meant to describe how to use it
as a “bare metal” chip just by itself.
Each member of the ATmega family has different amounts of RAM, ROM, I/O ports, etc. Depending
on the number of external pins required they may come in packages with more than a hundred pins, or with
as few as eight. The ATmega328P was selected for the EE 459 class for a variety of reasons:
• Available in 28-pin DIP (dual-inline package) that fits into available IC sockets. Other members of the
family are available in 20-pin and 40-pin packages.
• Enough TTL compatible1 I/O pins (21) to handle most EE 459 project tasks.
• FLASH memory for easy and fast reprogramming.
2 Hardware
The ATmega328P contains the following components:
• Two 8-bit and one 16-bit timer/counters. These can count internal clock cycles or external events and
generate an interrupt when reaching a specified count value.
• 6 channels of 10-bit analog-to-digital converter (ADC).
• Serial communications port. This can be used to communicate to the COM port of a computer.
the very common 74LS and 74HCT integrated circuits. The I/O pins produce compatible voltage levels and they can sink or
source the necessary amount of current.
Not all of these functions are available at the same time. Most of the pins on the chip are connected
to multiple functional units and it is up to the programmer to decide what a particular pin does. For
example, a pin might be used as a general purpose I/O line, or it might be ADC input, but it can’t be both
simultaneously.
2.1.2 Clock
Some sort of clock signal must be provided in order for the microcontroller to operate. On the ATmega328P
the clock can come from one of three different sources. The selection of the clock source is done by program-
ming fuse bits in the chip.
A TTL-compatible clock signal can be generated externally by other logic and connected to the XTAL1
input (pin 9.) This probably the easiest way to generate the clock for the EE 459 projects. The lab has a
supply of DIP oscillators in some of the more common frequencies. These output a TTL level square wave
that can be fed directly into the microcontroller and to other chips.
Alternatively, the processor can generate a clock if a crystal is connected to the XTAL1 and XTAL2
inputs. This method uses a plain crystal, not the DIP crystal oscillators as described above.
The third method uses an internal oscillator that runs at approximately 8MHz. This is probably the
least accurate way to generate a clock. Do not use this method if your project requires a clock running close
to a specified frequency. The advantage of using the internal clock is that you do not need to provide any
external signal and other functions are now available on pin 9. For example it can now be used as Port B
bit 6 (PB6) thus giving the microcontroller 22 I/O pins.
In applications where the UART0 serial communications interface is being used, the choice of clock
frequency determines the baud rates that can be used for transmitting and receiving serial data. The
accuracy of the frequency of the baud rate depends on the clock frequency used for the microcontroller. If
a high degree of accuracy is required, an external oscillator of the correct frequency will be needed. See
Sec. 2.4 for more information.
2.1.3 Reset
The reset input (RESET, pin 1) must be in the high state for the processor to operate normally. This pin
has an internal pull-up and does not have to be externally pulled-up to VCC in order for the processor to
operate normally.
Input, Input,
pull-up off 0, 0 0, 1 pull-up on
Figure 1: State of port bits when changing between input and output
2.3 Timer/Counters
The ATmega328P contains three timers:
Timer/Counter0 - an 8-bit counter.
2.5 I2 C Interface
The ATmega328P has the ability to communicate to other IC’s using the IIC or I2 C serial interface. This is
a two line bi-directional interface designed for medium speed communications between ICs on a board. One
line is the clock, the other is for data. Numerous ICs are available on the market that have I2 C interface
such as EEPROMs, temperature sensors, real-time-clocks, RAM, etc.
On the 328P, the I2 C interface is available on pins 27 and 28. These pins are shared with both the ADC
function and by Port C (PC4 and PC5). If the I2 C interface is enabled then any other function on those
pins is not available.
If the pins mentioned above are not available for use, it is still possible to implement an I2 C interface
by using software to perform all the I2 C transactions. Several I2 C libraries are available from Internet sites,
most of which will probably have to be modified to work with the 328P.
For more information on using I2 C, see the document “Using the IIC Interface” on the class web site.
This document also contains information on using the oscilloscopes in the lab to debug I2 C transactions.
while (1) {
ADCSRA |= (1 << ADSC ); // Start a conversion
while ( ADCSRA & (1 << ADSC )); // wait for conversion complete
3 Software
There are several ways to create the code that gets programmed into the FLASH memory of the processor.
The “avr-gcc” software for compiling C programs, linking, and downloading the executable program to the
ATmega328P is based on the “gcc” compiler package and is available for free from various Internet sites for
Macs and Windows systems. The installation notes for installing the avr-gcc software used in EE109 should
work for setting up systems to do programming in EE459 More detailed information on acquiring, installing
and using the software for the 328P is discussed in a separate document available on the EE459 web site.