You are on page 1of 18

United International University

EEE424: Microprocessor and Interfacing Laboratory


Experiment#1

Familiarization with AVR Trainer Kit and Related Software


Objectives:
• 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.
• To familiarize with the Input and output operation of different pins of ATmega32.
• To revise the While loop, For loop, if else, Switch Case statements in embedded C.

Equipment needed:
• AVR trainer kit
• ATMega32 Microcontroller
• CodeVisionAVR ( IDE )
• Programmer (To write Hex code)
• Proteus software (Simulator)

1. Introduction:
A microcontroller often serves as the “brain” of a mechatronic system. Like a mini, self-contained
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.

1.1 Selection of Microcontrollers:


Main Difference between AVR, ARM, 8051 and PIC Microcontrollers:
8051 PIC AVR ARM
Bus width 8-bit for standard 8/16/32-bit 8/32-bit 32-bit mostly also
core available in 64-bit
Communication UART, UART, USART, UART, USART, UART, USART,
Protocols USART,SPI,I2C LIN, CAN, SPI, I2C, (special LIN, I2C, SPI,
Ethernet, SPI, I2S purpose AVR CAN, USB,
support CAN, Ethernet, I2S,
USB, Ethernet) DSP, SAI (serial
audio interface),
IrDA
Speed 12 Clock/instruction 4 Clock/instruction 1clock/ instruction 1 clock/ instruction
cycle cycle cycle cycle

1
Memory ROM, SRAM, SRAM, FLASH Flash, SRAM, Flash, SDRAM,
FLASH EEPROM EEPROM
ISA CISC Some features of RISC RISC
RISC
Memory Von Neumann Harvard Modified Modified Harvard
Architecture architecture architecture architecture
Power Average Low Low Low
Consumption
Families 8051 variants PIC16,PIC17, Tiny, Atmega, ARMv4,5,6,7 and
PIC18, PIC24, Xmega, special series
PIC32 purpose AVR
Community Vast Very Good Very Good Vast
Manufacturer NXP, Atmel, Silicon Microchip Atmel (Now Apple, Nvidia,
Labs, Dallas, occupied by Qualcomm,
Cyprus, Infineon, Microchip) Samsung
etc. Electronics, and TI
etc.
Cost (as Very Low Average Average Low
compared to
features provide)
Other Feature Known for its Cheap Cheap, effective High speed
Standard operation
Popular AT89C51, P89v51, PIC18fXX8, Atmega8, 16, 32, LPC2148, ARM
Microcontrollers etc. PIC16f88X, Arduino Cortex-M0 to
PIC32MXX Community ARM Cortex-M7,
etc.

1.2 Difference between AT89C51 and ATmega 32 microcontroller:


AT89C51 ATmega32
4K Bytes of In-System 32Kbytes of In-System Self-programmable Flash program
Reprogrammable Flash memory
Memory
128 x 8-bit Internal RAM 2Kbytes Internal SRAM,1024Bytes EEPROM

Two 16-bit Timer/Counters Two 8-bit Timer/Counters with Separate Prescalers and Compare
Modes, One 16-bit Timer/Counter with Separate Prescaler,
Compare Mode, and Capture Mode, Real Time Counter with
Separate Oscillator, Four PWM Channels
No internal ADC 8-channel, 10-bit ADC

Programmable Serial – Byte-oriented Two-wire Serial Interface


Channel – Programmable Serial USART
– Master/Slave SPI Serial Interface
– Programmable Watchdog Timer with Separate On-chip
Oscillator
– On-chip Analog Comparator

2
1.3 ATmega32 IC Package:

1.4 AVR trainer kit:

 On board Peripherals
1. ATMEGA32
2. Two 3A/220V Relay.
3. One RGB LED.
4. USB-TTL.
5. LM45 temperature sensor.
6. Potentiometer.
3
2. CodeVisionAVR:

CodeVisionAvr is a C cross-compiler, integrated development and automatic program generator


designed for the Atmel avr family of microcontrollers.

Figure: Different modules inside of the Code Vision AVR IDE

4
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.

5
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.

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

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

7
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.

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

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

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

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

9
3. Proteus:
Proteus has many features to generate both analog 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

10
Figure: Pick up 100 ohms resistor from Proteus library

By double clicking on the microcontroller bring a screen like below and change on the three places
as marked.

11
Steps:
1. Open Proteus ISIS Schematic Capture
2. Select the Component Mode from the left Toolbar
3. Click On P (Pick From Libraries)
4. Add all the required components
5. Place the components on the workspace
6. Wire up the circuit
7. Click on Play Button on the bottom left to start simulation

4. How to access Input and Output Ports:


Atmel AVR is 8 bit microcontroller. All its ports are 8 bit wide. Every port has 3 registers associated
with it each one with 8 bits. Every bit in those registers configure pins of particular port. Bit0 of
these registers is associated with Pin0 of the port, Bit1 of these registers is associated with Pin1 of
the port , …. and like wise for other bits.

These three registers are as follows :


(x can be replaced by A,B,C,D as per the AVR you are using)
– DDRx register
– PORTx register
– PINx register

4.1 DDRx register:

DDRx (Data Direction Register) configures data direction of port pins. Means its setting determines
whether port pins will be used for input or output. Writing 0 to a bit in DDRx makes corresponding port
pin as input, while writing 1 to a bit in DDRx makes corresponding port pin as output.

Example:
o to make all pins of port A as input pins :
DDRA = 0b00000000;
o to make all pins of port A as output pins
: DDRA = 0b11111111;
o to make lower nibble of port B as output and higher nibble as input
: DDRB = 0b00001111;

4.2 PINx register:

PINx (Port IN) used to read data from port pins. In order to read the data from port pin, first you have
to change port’s data direction to input. This is done by setting bits in DDRx to zero. If port is made
output, then reading PINx register will give you data that has been output on port pins.

12
Now there are two input modes. Either you can use port pins as tri stated inputs or you can activate
internal pull up. It will be explained shortly.

Example:
o to read data from port A.
DDRA = 0x00; //Set port A as input
x = PINA; //Read contents of port A

4.3 PORTx register:

PORTx is used for two purposes.

i) To output data: when port is configured as output

When you set bits in DDRx to 1, corresponding pins become output pins. Now you can write data into
respective bits in PORTx register. This will immediately change state of output pins according to data
you have written.

Example:
o to output 0xFF data on port b
DDRB = 0b11111111; //set all pins of port B as outputs
PORTB = 0xFF; //write data on port
o to output data in variable x on port a
DDRA = 0xFF; //make port A as output
PORTA = x; //output variable on port

o to output data on only 0th bit of port c


DDRC |= 0b00000001; //set only 0th pin of port C as output
PORTC |= 0b00000001; //make it high.

ii) To activate/deactivate pull up resistors: when port is configured as input

When you set bits in DDRx to 0, i.e. make port pins as inputs, then corresponding bits in PORTx register
are used to activate/deactivate pull-up registers associated with that pin. In order to activate pull-up
resister, set bit in PORTx to 1, and to deactivate (i.e to make port pin tri stated) set it to 0.

NOTE: while using on-chip ADC, ADC port pins must be configured as tri-stated input.

Example:
o to make port a as input with pull-ups enabled and read data from port a
DDRA = 0x00; //make port A as input
PORTA = 0xFF; //enable all pull-ups
y = PINA; //read data from port a pins

13
o to make port B as tri stated input
DDRB = 0x00; //make port B as input
PORTB = 0x00; //disable pull-ups and make it tri state
o to make lower nibble of port A as output, higher nibble as input with pull-ups enabled
DDRA = 0x0F; //lower nibble -> output, higher nibble -> input
PORTA = 0xF0; //lower nibble -> set output pins to 0, higher nibble -> enable pull-ups

Figure: Internal pull up resistor

5. Revising Embedded C:
5.1 Data Types, for loop, while loop and array in C for AVR:

14
Figure: Schematic diagram for Example 1, 2 ,3 ,4

Example 1:
Turn on LEDs connected on PORTD and turn off LEDs after fixed delay. [This is an example of for loop]

#include <mega32.h>
void main(void)
{
unsigned int y= 0; // answer why y is unsigned int ; hint data size
DDRD=0b11111111; // set port D as output DDRD= 0xFF / 255
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++) // very the value of y for different delay time
{
}
PORTD=0b00000000; // PORTD= 0x00 / 0
}

15
// bit implementation of I/O [Compiler dependent-> be careful]
#include <mega32.h>
void main(void)
{
unsigned int y= 0;

DDRD.0=1;
DDRD.1=1;
DDRD.2=1;
DDRD.3=1;
DDRD.4=1;
DDRD.5=1;
DDRD.6=1;
DDRD.7=1;

PORTD.0=1;
PORTD.1=1;
PORTD.2=1;
PORTD.3=1;
PORTD.4=1;
PORTD.5=1;
PORTD.6=1;
PORTD.7=1;
for(y=0; y<65535; y++) //same as for(y=0; y<65535; y++); very the value of y for different delay time
{
}
PORTD=0b00000000; // PORTD= 0x00 / 0
}

Example 2:
Write a C program to toggle all bits of PORTD 100 times with delay. [This is an example of nested for loop]

#include <mega32.h>
void main(void)
{
unsigned char x= 0; // answer why x is unsigned int ; hint : data size
unsigned int y= 0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF /
255 for(x=0; x<100; x++)
{
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++); // for delay
PORTD=0b00000000; // PORTD= 0x00 / 0
for(y=0; y<65535; y++); // for delay
}
}

16
Example 3:
Write a C program to toggle all bits of PORTD infinitely with delay. [This is an example of nested for loop]
#include <mega32.h>
void main(void)
{
unsigned int y= 0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF / 255
for( ; ; ) // infinite loop : equivalent to while (1)
{
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++); // for delay
PORTD=0b00000000; // PORTD= 0x00 / 0
for(y=0; y<65535; y++); // for delay
}
}

Example 4:
Write a C program to show the contents of an array in PORTD. [This is an example of array]
#include <mega32.h>
void main(void)
{
unsigned char mylist [10] = {0,1,2,3,4,5,6,7,8,9};
unsigned char y= 0;
unsigned int x=0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF / 255
for(y=0 ;y<10 ;y++ ) // this loop will iterate 10 times
{
PORTD=mylist [y];
for(x=0; x<65535; x++); // for delay
}
}

6.2 Using Library delay Function:

#include <mega32.h>
#include <delay.h>

void main(void)
{
unsigned char x= 0;
DDRD=0b11111111;
for(x=0; x<100; x++)
{
PORTD=0b11111111
delay_ms(1000); // 1000ms delay
PORTD=0b00000000;
delay_ms(500); // 500ms delay
}
}
17

You might also like