Professional Documents
Culture Documents
Tutorial On Atmega32 Uc - EFY March11
Tutorial On Atmega32 Uc - EFY March11
Part 1
TUTORIAL ON ATMEGA32
DEVELOPMENT BOARD
h k.
ris
s.v.n. ha
SAGAR G. YADAV
1 2 0 M a r c h 2 0 1 1 e l e c t ro n i c s f o r yo u
w w w. e f y m ag . co m
Construction
The ATmega32 is a high-performance, low-power, 8-bit AVR microcontroller. It has 32 kB of in-system
self-programmable flash, 2 kB of static
random access memory (SRAM) and
1 kB of electrically erasable programmable read-only memory (EEPROM).
Some of the on-chip peripheral features are:
1. Two 8-bit timers/counters with
separate prescaler and compare modes
2. One 16-bit timer/counter with
separate prescaler, comparator mode
and capture mode
3. Four pulse-width-modulated
(PWM) channels
4. 8-channel, 10-bit analogue-to-
Circuit
description
cONSTRUCTION
tions.
Five tactile pushbutton switches
are also provided for debugging purpose. All the five switches are activelow type and do not affect the normal
operation of port pins. Switches S1, S2,
S3 and S4 are connected to Port D pins
PD0, PD1, PD2 and PD3, respectively.
PD0 and PD1 form external interrupt
pins of the MCU.
A power-on reset circuit is built
around reset pin 9 with the help of
resistor R10 and capacitor C3. Switch
1 2 2 M a r c h 2 0 1 1 e l e c t ro n i c s f o r yo u
S5 is connected to pin
9 for resetting the MCU
externally.
At Port PD7 (pin
21), a piezobuzzer is
connected via switching
transistor T2 to further
help in debugging. The
buzzer can be disconnected from the circuit
at any point of time
by removing shorting
jumper JS.
In order to further
enhance the user interface of the development
board, a dedicated LCD
port is provided onboard. To minimise use
of I/O pins for LCD
control, the display is
made to function in 4-bit
mode. In 4-bit mode,
only four data pins are
required in addition to
RS, RW and E control
pins. In total, seven I/O
port pins are required for functioning
of the display.
Details about the LCD module and
its interfacing with the MCU are provided later in the article. Port C is used
to interface the LCD in the development board. Port-C pins PC4, PC5, PC6
and PC7 form data pins, whereas PC2
and PC3 are used as RS and E control
pins. Port-D pin PD6 is used to provide
R/W control signal.
In order to communicate with the
PC, the development board uses the
USART module of the MCU. functional details of this module are given
later in the article. The RS-232 signals
range between +12V and -12V and are
inverted (-12V is a logical 1). Applying this kind of voltage to the AVR is
dangerous, so MAXIMs MAX232 level
converter is used between the DB9
female serial port and the MCU. The
MAX232 uses capacitors to perform
the necessary level shifting in order
to receive and transmit signals. The
same DB9 serial port connector is used
to program the MCU by using the SPI
w w w. e f y m ag . co m
Construction
serial programming facility available
in the MCU.
Popular Open Source software
PonyProg is used for easy programming of the MCU. The same software
can also be used to set and read all the
flag bits in the MCU, to read/write the
on-chip EEPROM.
PonyProg also comes with a well-
defined hardware circuit for SPI programming. The same circuit is used in
the development board too by making
use of MOSI, MISO, SCK and RESET
pins of the MCU. To limit the line voltages on the SPI interface lines, 5.1V
zener diodes are used. DPDT switch
S7 is used to switch between USART
communication mode and SPI serial
programming mode. A step-by-step
procedure to program the MCU using
PonyProg is provided in the latter part
of the project design.
The microcontroller is clocked to
a maximum frequency of 16 MIPS by
using 16MHz quartz crystal X1. The
crystal should always be used along
with two capacitors of equal value
(connected as shown in the circuit diagram) for a stable operation. The value
chosen for these capacitors is 22 pF for
a frequency of 16 MHz.
In order to test the MCUs ADC
module, a variable voltage source is
made available on-board with the help
of preset VR1. Switch S6 should be
kept opened when the ADC application is not in use.
The 24C04 forms the external
EEPROM circuit of the development
board. It has a storage capacity of 4 kB
(512x8). The EEPROM can be read/
written using the TWI module avail-
Construction
The entire development board circuit
can be assembled on a single PCB.
An actual-size, single-side PCB for
the same is shown in Fig. 3 and its
component layout in Fig. 4. solder by
first connecting all the jumpers in the
circuit, followed by smaller-size passive components and then the bigger
components.
Software program
Before loading various application machine codes into the microcontroller,
you need to write the intended controller program in C language. Compile it
to obtain the hex code. Burn the generated hex code into the microcontroller.
To perform all these tasks in this
project, you can use free Open Source
software like WinAVR, AVR Studio 4
and PonyProg. The programs should
be installed in the PC before starting
the AVR applications.
WinAVR. WinAVR (pronounced
as whenever) is a suite of executable,
Open Source software development
tools for the ATMEL AVR series of
RISC microprocessors hosted on the
Windows platform. It includes the
GNU GCC compiler for C and C++
programming languages. Visit http://
winavr.sourceforge.net/index.html for
more details.
AVR Studio 4. In this project, we
have used AVR Studio 4 (Version 4.13)
to compile the code written in C language and generate the hex code. It can
also be used to simulate the program
running on the microcontroller.
PonyProg. PonyProg (Version
e l e c t ro n i c s f o r yo u M a r c h 2 0 1 1 1 2 3
cONSTRUCTION
code. This will make all the Port B pins
to go high. Now generate a time delay
of 500 ms using the WaitMs( ) function.
Once the time delay is generated, all
Fig. 10: Error message
the Port B pins need to be cleared to
switch off the LEDs. For this,
load 0x00 (complement of 0xFF)
to Port B. The entire process
repeats unconditionally inside a
while loop to generate the LED
blinking effect.
Load your application program into the ATmega32 chip
as follows:
1. Open AVR Studio. You
will get a window as shown
in Fig. 5. Click New Project
option.
2. Select AVR GCC and
enter a name and destination
folder for your project.
Fig. 11: Prototype of Atmega32 development board
3. Select AVR simulator in
Debug platform window and
Atmega32 in Device window (refer
2.07C) is a serial device programmer
Fig. 6).
software with a user-friendly GUI
4. Start writing your first program
framework available for Windows
blink_LED.c in the AVR Studio editor
95, 98, 2000, NT, XP and Intel Linux.
window. Configure the required opIt supports IC Bus, Microwire, SPI
tions in Tools and Project menus.
EEPROM, the Atmel AVR and Micro5. Go to Build menu and click
chip PIC micro. For details, visit www.
Build option to compile the code.
lancos.com/e2p/ponyprog2000.html
If there is no error, the hex code will
Example 1: Blinking LEDs
generate.
6. Connect the ATmega32 developInclude the following header files in
ment board to the serial port of the PC
the code to successfully compile the
through the serial cable. On the board,
blinking-LED application program
set switch S7 to its program position.
(blink_LED.c):
Switch on the power supply. Run
1. <avr/io.h> to include appropriPonyProg and select Interface Setup
ate I/O definitions for the MCU
option from Setup menu.
2. <avr/interrupt.h> to enable all
7. Set the I/O port as shown in
the interrupt-handling functions (not
Fig. 7.
used here).
Click Probe button to check
3. <util/delay.h> to generate time
whether you have correctly connected
delays
the development board to the PC. A
Also include a user-defined funcTest OK message should appear. It
tion (WaitMs( )) to generate time
will ensure that you have correctly
delays in milliseconds. The parameter
connected the serial cable to the board,
to be passed is an unsigned integer
the power supply is turned on, switch
value indicating the time delay to be
S7 is turned to its program position
produced in milliseconds.
and you have chosen the correct COM
In the main function, first configure
port.
Port B pins as output pins. For this, set
8. Select AVR Micro and ATmega32
the corresponding bits in the DDRB
options from Device Selection menu
register. After that, load 0xFF in the
1 2 4 M a r c h 2 0 1 1 e l e c t ro n i c s f o r yo u
Example 2: Sequential
light chaser effect
The source code (Chaser_Effect.c) for
generating a sequential chaser effect on
the LEDs (LED2 through LED9) is also
included in the EFY-CD. It makes the
LEDs connected to Port B of the MCU
light up in a sequential manner back
and forth.
Construction
of the pins.
LCD memory. The LCD contains
three memory blocks: DDRAM (display data RAM), CGROM (character
generator ROM) and CGRAM (character generator RAM).
DDRAM. DDRAM is a sort of RAM
where data can be written to and read
from, but loses its contents when the
power is removed. It is used to store
the characters (up to 80) that need to
be displayed (see Fig. 12).
If the cursor is on, it is displayed at
the location that is currently addressed.
When a character appears at the cursor
position, it automatically moves to the
next addressed location.
CGROM. It contains the default
character map with all the characters that can be displayed on the
screen. Each character is assigned to
one memory location. Addresses of
CGROM locations match the characters
of ASCII. For example, if the program
currently being executed encounters a
command send character P to port,
binary value 0101 0000 appears on the
port. This value is ASCII equivalent of
character P which will be displayed on
the LCD. This applies to all the letters
of the alphabet (capital and small), but
not to the numbers. In order to display
the numbers correctly, it is necessary
to add decimal number 48 to each of
the values prior to sending them to
the LCD.
For details of the LCD memory,
refer to the datasheet of HD44780 conw w w. e f y m ag . co m
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include lcd.h
e l e c t ro n i c s f o r yo u M a r c h 2 0 1 1 1 2 5
cONSTRUCTION
earlier, lcd.h is the driver header file
for the LCD module. Add the lcd.c
external C source file along with the
moving_display.c application source
file under Source Files directory in the
AVR Studio platform.
After including the required header
files, initialise your LCD module in
the main function with the required
display attributes using the lcd_init
(uint8_t dispAttr) function. The available display attributes are: LCD_DISP_
OFF (for LCD off), LCD_DISP_ON
(display on, cursor off), LCD_DISP_
ON_CURSOR (display on, cursor on)
and LCD_DISP_ON_CURSOR_BLINK
(display on, cursor on flashing).
In our program (moving_
display.c), we have used the LCD_
DISP_ON attribute to initialise the
display. After initialising the display,
clear it using the lcd_clrscr (void)
function. In order to point the cursor
at a desired location in the display,
use the lcd_gotoxy (uint8_t x, uint8_t
y) function. Here x and y denote the
coordinates. For example, if you need
to point the cursor to fifth position in
the first row of the LCD module, the
corresponding x and y coordinates
would be 0 and 4, respectively. The
lcd_putc (char c) function is used to
print a single character at the current
cursor location. Similarly, the lcd_puts
(const char *s) function prints a string
pointed to by s from the current cursor location.
After successfully burning the
hex code into the microcontroller,
the message ELECTRONICS FOR
VIN 1024
VREF
1 2 6 M a r c h 2 0 1 1 e l e c t ro n i c s f o r yo u
w w w. e f y m ag . co m