You are on page 1of 14

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

Experiment 3: Microcontroller Configuration and LED interfacing with ATmega 32


Objective:
To identify the Atmel ATmega32 microcontroller, AVR trainer kit, and associated
hardware.
Create a new project in CodeVisionAVR.
Use AVR Programmer to write Hex code into the ATmega32 microcontroller.
Simulate the project scenario in the Proteus.
Equipment needed:
AVR trainer kit
ATMEGA 32 Microcontroller
CodeVisionAVR (Compiler)
AVR Programmer (To write Hex code)
Proteus software (Simulator)
Introduction:

A microcontroller often serves as the brain of a mechatronic system. Like a mini, selfcontained computer, it can be programmed to interact with both the hardware of the system
and the user. Even the most basic microcontroller can perform simple math operations,
control digital outputs, and monitor digital inputs. As the computer industry has evolved, so
has the technology associated with microcontrollers. Newer microcontrollers are much faster,
have more memory, and have a host of input and output features that dwarf the ability of
earlier models. Most modern controllers have analog-to-digital converters, high-speed timers
and counters, interrupt capabilities, outputs that can be pulse-width modulated, serial
communication ports, etc.
ATmega32 microcontroller:

United International University (UIU)

Page 1

CSE 426 Lab Microprocessor, Microcontroller and Interfacing


Figure 1. Pinout ATmega32

Atmels ATMega32 8-Bit Processor. 32K of program space, 32 I/O lines, 8 of which are
10bit Analog to Digital converter capable. Runs up to 16MHz with external crystal.
Digital I/O ports (PORTA ~ PORTD) of ATMEL ATMEGA32

8 pins are grouped together as ports. Generally there are 4 or more ports in an AVR.
The ports are named in in alphabetic letters, such as PORTA, PORTB, PORTC and
PORTD etc. Each port has 8 pins, from 0 to 7. For example, the pins of port B are
PB7, PB6, PB5, PB4, PB3, PB2, PB1, PB0
As you can see above, the atmega32 has 4 ports, A, B, C and D and each of them
have 8 pins. Most of the pins have some additional bracketed name, (for example
(RXD) on PD0.
This means that this pin is also an RXD pin).

They are controller by 3 registers (consider x to be port name, eg, C)


DDRx > data direction register (determines if pin is input or output)
PORTx > sets the output value to 1 or 0 (1 is so source and 0 is to sink)
PINx> reads the input pin value (read only)
AVR trainer kit:
BASE BOARD DIAGRAM OF AVR TRAINER KIT VER.2

United International University (UIU)

Page 2

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

A well designed Trainer board is always essential for smooth development/experimentation with
microcontrollers. Every hobbyist or student must have some kind of development setup. Some people
use breadboard while others use general PCB to fabricate their development system. Development is
not very easy if bread boards or general PCB are used. If bread board is used, after few days of use the
wire will become loose or come out of the hole. While making large circuit in a general PCB is a
tedious job.
Such problems are solved by using AVR Trainer kit. It is a circuit board with basic connection
required to drive the microcontroller and also with features like serial port, LCD display, seven
segment displays, prototyping area, power supply and it supports 40 and 28 PDIP pin AVR Microcontrollers etc.
A trainer board is very useful as you can program the microcontroller and also check the outputs using
the LEDs or LCD display or even connect it to the rest of the project using wires or connectors. The
power supply provided on the board can be used to supply power to other circuits. The board also has
provision to run DC motors or even stepper motors with the help of motor driver ICs present on the
board.
A very good advantage of such a trainer board is that all the basic project requirements are present on
the board and it also occupies less space even with all the modules like LCD and seven segment
displays. The AVR trainer kit has features which helps you in making and testing a project involving a
variety of modules like:

Standard 40 Pin and 28 Pin PDIP Atmel AVR Series Chip support.
On Board integrated universal Atmel AVR USB Programmer with ZIP Socket
support
On Board 3 different Power Sources (5V, 12V, Variable 0-15V).
8 Logical LED display.
LCD Display
4 Digit 7 Segment Display
8*8 Dot-matrix Display
External 512byte EEPROM
DS1307 RTC(Real Time Clock) with 3V Battery Supported
RS232 Serial Communication Supported Via PC
Motor Support: DC, Stepper, Servo
IR Transmitter and Receiver Supported
Relay Supported for 2 different High voltage System Control
4 Input Switch
4*4 Matrix keypad
LDR (Light Dependent Resister)
LM35 Temperature Sensor
Variable Resistor(Pot) for volume support
A bread board also installed on trainer kit for extra components interfacing support
User friendly Output PORT excess

CodeVisionAVR:
CodeVision avr is a C cross-compiler, integrated development and automatic program
United International University (UIU)

Page 3

CSE 426 Lab Microprocessor, Microcontroller and Interfacing


generator designed for the Atmel avr family of microcontrollers.
Follow all the steps.
1. Download and open CVAVR. Goto file>new.

2. Select file type Project and click ok.

3. Select your chip type and click ok.

United International University (UIU)

Page 4

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

4. Now a window opens for including various functions or header files in your program
automatically. First select your controller from chip option.

5. If you used LCD in your project then go to LCD and select LCD port.

United International University (UIU)

Page 5

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

6. After including all your requirements go to file>generate, save and exit.

United International University (UIU)

Page 6

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

7. Now this window opens 3 times save your project at the location you want and typr the file
name like evm in all 3 windows. All 3 file names must be same.

8. Now a project window opens where necessary header files are included automatically. You
also can include any header file you want from the CVAVR library.

United International University (UIU)

Page 7

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

11. Declare all the variables above and scroll down to see while(1) function and write your
code in while(1).

12. Click the button as shown in window for building your project.

United International University (UIU)

Page 8

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

13. Now a windows open make sure there is no error and no warning.

14. You can find your HEX file in EXE folder.


AVR programmer:
United International University (UIU)

Page 9

CSE 426 Lab Microprocessor, Microcontroller and Interfacing


Double click on AVR Prog icon.
A small window opens with options like Program, Read and Verify.
Click on Browse and select the hex file which is to be written into the microcontroller.
Then click on Program which is present below Flash and not the one which is below EEPROM.
After programming close the window and run the program.

Proteus:
Proteus has many features togenerate both analogue and digital result. However, this lab will
focus on only tools that will be used in digital schematic designs.

Figure: Example of Proteus software window

United International University (UIU)

Page 10

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

Figure: pick up 100 ohms resistor from proteus library


Steps :
1.
2.
3.
4.
5.
6.
7.

Open Proteus ISIS Schematic Capture


Select the Component Mode from the left Toolbar
Click On P (Pick From Libraries)
Add all the required components
Place the components on the workspace
Wire up the circuit
Click on Play Button on the bottom left to start simulation

LED interfacing
The first session in AVR Trainer kit V2 will begin with an experiment that flashes an LED on
and off. While this looks very simple it is the best project to start because this makes sure that
one can successfully wrote the program, compiled it, loaded inside the AVR, and check the
hardware simulation using AVR trainer kit.
In this lab session connect an LED to one of the port pin of Atmega32 and flash it
continuously with 1 sec duration.
One of the most basic features of any microprocessor is to be able to use the pins as general
input and output pin. The Pins as output pins can be used as both source (logic 1) and sink
(logic 0). The pins also have an internal pull-up resistor to avoid floating.

United International University (UIU)

Page 11

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

let us assume we want to set PORTC as output and drive logic 1 on the first 4 LSB
pins. So, we need to set this port as output first using the DDR register setting a logic
one on the corresponding port. Take a look at the 3 example below
DDRC = 0b11111111; //here all the 8 pins of Port C are set as output
PORTC = 0b11110000; // PC7, PC6, PC5, PC4 will drive logic 1
This is a better example (red means logic 1, black means logic 0)

So the final code will be like this

#include <atmega32.h>
main()
{
DDRC = 0b11111111;
PORTC = 0b11110000;
}

Important Notes :
If a pin is declared input (0 on DDR) but port pin has logic 1, there will still be
no output driven.
If a pin is declared output but Port pin drives logic 0 (sink), it acts like a ground
pin. (e.g.. Let us assume we have declared PB6 and PB5 as output but we are driving
logic 1 on PB5 only and logic 0 on PB6. We can then connect an LED with positive
terminal on PB5 and ground terminal on PB6. This method is very much used in
multiplexing LED or keypad inputs)
The PUD in SFIOR, when set to logic 1 will disable all pull up resistors. And if
set to logic 0, the DDR and PORT registers will declare it.

United International University (UIU)

Page 12

CSE 426 Lab Microprocessor, Microcontroller and Interfacing

Circuit Setup

Figure : Connection diagram of the LED Light Flashing.

1. First of all power up the trainer kit via given power supply adapter.
2. Then connect PORTB PB0 (pin1) with LED light placed on the trainer kit by given
connecting wire.
3. Now write the given code for flashing LED with 1 sec interval on codevision avr and build
the hex file.
4. Burn the code using in-built ISP programmer of AVR Trainer kit.
5. Check the hardware output.

Program:

Chip type
: ATmega32L
Program type
: Application
AVR Core Clock frequency: 8.000000 MHz
Memory model
: Small
External RAM size
:0
Data Stack size
: 512
*****************************************************/
#include <mega32.h>
#include <delay.h>
// Declare your global variables here
void main(void)
{
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
United International University (UIU)

Page 13

CSE 426 Lab Microprocessor, Microcontroller and Interfacing


PORTB=0x00;
DDRB=0x01;
while (1)
{
// Place your code here
PORTB.0=1;
delay_ms(1000);
PORTB.0=0;
delay_ms(1000);
}
}
Exercise:

1. There are eight led on the board. Blink all the even led and odd led. Use 2 second
delay.
2. Blink all the led one after another. Use 1 second delay.

United International University (UIU)

Page 14

You might also like