You are on page 1of 26

Department of Electronics & Communication Engineering

AVR_LAB_1

AIM :Introduction to ATMEL Studio 6

Tool: ATMEL STUDIO 6

Introduction:

This experiment will teach you how to write, compile, and trace a simple
program in Atmel Studio

Downloading and Installing Atmel Studio:

Download the newest version of Atmel Studio from the Atmel website:

http://www.atmel.com/tools/atmelstudio.aspx

Run the downloaded program to install the Atmel Studio IDE

Opening Atmel Studio :

Go to the Start menu and open Atmel Studio.


Department of Electronics & Communication Engineering

Creating the first project :

1. Go to the File menu. Choose New and then Project.


Department of Electronics & Communication Engineering

2. In the opened dialog,

a. Choose Assembler.

b. Name the project as toggleProject.

c. Choose the path where you like to save the project by clicking on the
Browse button.

d. Press OK.

3. In the Device Selection dialog

a. Select megaAVR as the Device family.

b. Choose ATmega32 (or any other Chips you want to use)

c. Select OK.
Department of Electronics & Communication Engineering

The compiler automatically makes the toggleProject and adds an


assembly file to it.

Writing the first Assembly program

Type the following program.


Department of Electronics & Communication Engineering

LABLE PROGRAM COMMENTS


.INCLUDE "M32DEF.INC"
LDI R16,0xFF
OUT DDRA,R16
L1: OUT PORTA,R16
LDI R20,0
OUT PORTA,R20
RJMP L1

Building

Press F7 to assemble, or choose Build Solution from the Build menu. The
results of assembling the program are shown in the Output window.

Debugging

1. To start debugging, press Alt+F5 or choose Start Debugging from the


Debug menu. 2. The following Dialog appears and asks you to select the
debugging tool. Press Continue.

3. In the following window, choose Simulator as the debugger and then


close it by pressing the x next to the toggleProject.
Department of Electronics & Communication Engineering

4. Press Alt+F5 again. A Memory Watch windows appears which shows


the contents of the Flash memory. Close the window.

5. Now a yellow cursor is on the first line of the main program and the
IDE is ready to debug the program.
Department of Electronics & Communication Engineering

6. To execute the instructions line by line press F10 or click on the Step
over icon.

7. To monitor the peripherals, including the I/O ports, click on the Debug
menu, choose Windows and then I/O view.

8. The I/O view tab appears on the right hand side which shows the
peripherals of the microcontroller, including the I/O ports. Select PORTA.
The values of the related registers (PINA, DDRA, and PORTA) will be
shown below.
Department of Electronics & Communication Engineering

9. Press F10 (Step Over) a few times and see the PORTA register changes
in the I/O View.
Department of Electronics & Communication Engineering

AVR_LAB_2

AIM:To examine the flag bits of the SREG.

TOOL: ATMEL STUDIO 6

THEORETICAL BACKGROUND:

The status register is an 8-bit register. It is also referred to as the flag


register. See Figure 2-8 for the bits of the status register. The bits C, Z, N,
V, S, and H are called conditional flags, meaning that they indicate some
conditions that result after an instruction is executed. Each of the
conditional flags can be used to perform a conditional branch Jump.

The following is a brief explanation of the flag bits of the status register.
The impact of instructions on this register is then discussed.

C, the carry flag


This flag is set whenever there is a carry out from the D7 bit. This flag bit
is affected after an 8-bit addition or subtraction. Chapter 5 shows how the
carry flag is used.

Z, the zero flag


The zero flag reflects the result of an arithmetic or logic operation. If the
result is zero, then Z = 1. Therefore, Z = 0 if the result is not zero.

N, the negative flag


Binary representation of signed numbers uses D7 as the sign bit. The
negative flag reflects the result of an arithmetic operation. If the D7 bitthe
result is zero, then N = 0 and the result is positive. If the D7 bit is one,
then N = I and the result is negative. The negative and V flag bits are used
for the signed number arithmetic operations
Department of Electronics & Communication Engineering

This flag is set whenever the result of a signed number operation is too
large, causing the high-order bit to overflow into the sign bit. In general,
the carry flag is used to detect errors in unsigned arithmetic operations
while the overflow flag is used to detect errors in signed arithmetic
operations . The V and N flag bits are used for signed number arithmetic
operations

S, the Sign bit


This flag is the result of Exclusive-ORing of N and V flags. H, Half carry
flag

ACTIVITY 1
Write and assemble a program to add the following data and then use
the simulator to examine the C, H and Z flags after the execution of
each addition.
$92, $23, $66, $87, $F5

ACTIVITY 2
Write and assemble the following program. Use the simulator to single-
step and examine the flags and register content after the execution of
each instruction.
Department of Electronics & Communication Engineering

ACTIVITY 3
Write and assemble a program to load a value into each location of R20
– R23. Use the COM instruction to complement the value in each
register. Use the simulator to single-step and examine the flags and
register content after the execution of each instruction.

Find the value of the C flag after the execution of the following codes.

(a) LDI R20, $85


LDI R21, $92
ADD R20, R21

(b) LDI R16, $15


LDI R17, $72
ADD R16, R17

(c) LDI R25, $F5


LDI R26, $52
ADD R25, R26
Department of Electronics & Communication Engineering

AVR_LAB_3

AIM:To study looping and branching instruction of ATmega 32

Tool :ATMEL STUDIO 6

ACTIVITY 1

Write and assemble the following program to add two array and store the
result in other array
 include file of atmega32
 R16← 0x00
 R17← 0x00
 R26← 0x60
 R27← 0x00
 R20← 0x0A
 R21← 0x00
 load R17 from address store in register X and increment X
 R16=R16 +R17
 jump (branch) if carry flag if clear (not set) to XYZ
 increment R21
 decrement R20
 jump (branch) if zero flag is clear (not set) to BACK
 R18 = R16
 jump toHERE

ACTIVITY 2

Write and assemble the following program to add two array with carry
 include file of atmega32

 R16← 0x00
 R17← 0x00
 R26← 0x60
Department of Electronics & Communication Engineering

 R27← 0x00
 R28← 0x73
 R29← 0x00
 R30← 0x86
 R31← 0x00
 R20← 0x0A
 R21← 0x00
 load R17 from address store in register X and increment X
 load R16 from address store in register Y and increment Y
 R16=R16 +R17
 increment R30
 store R16 to address store in register Z and increment Z
 jump (branch) if carry flag if clear (not set) to XYZ
 increment R21
 decrement R30
 decrement R30
 store R21 to address store in register Z and increment Z
 increment R30
 increment R30
 decrement R20
 jump (branch) if zero flag is clear (not set) to BACK


R22 = R16
 jump to HERE
Department of Electronics & Communication Engineering

AVR_LAB_4

AIM:To Study I/O ports programming of ATmega 32

Tool :ATMEL STUDIO 6

ACTIVITY 1

Write and assemble the following program to toggle all bits of Port A
 include file of atmega32

 R20← 0x00

 write 0xff in DDRA register

 no operation

 R16 = PINA


complement R16

 jump to HERE

ACTIVITY 2

Write and assemble the following program such that when PB3 becomes
high write the value $45 to portc and also send high to low pulse to PD3
 include file of atmega32
 clear 3rd bit of port B as input pin
 R16← $ff
 write 0xff in DDRC register
 set 3rd bit of port D as output pin
 skip if bit 2 of port B is set
Department of Electronics & Communication Engineering

 jump to AGAIN
 R16← $45
 write 45 in port A register
 set 3rd bit of port D high
rd
 clear 3 bit of port D high
 jump to HERE

ACTIVITY 3

Write and assemble the following program such that a switch is connected
to pin PB2, check the status of switch and perform the following:

(a) If switch is zero send letter ‘N’ to port d


(b) If switch is one send letter ‘Y’ to port d
 include file of atmega32

nd
 clear 2 bit of port B as input pin

 R16← $ff

 write 0xff in DDRD register

 skip if bit 2 of port B is set

 jump to OVER

 R16← ‘Y’

 write ‘Y’ in port A register

 jump to AGAIN
Department of Electronics & Communication Engineering

 R16← ‘N’

 write ‘N’ in port A register

 jump to AGAIN
Department of Electronics & Communication Engineering

AVR_LAB_5

AIM: Interfacing LCD display with ATMEGA32

Tool :Arduino software


PROGRAM: -

/* connections

D0 - D7 to PB0 - PB7

RS - PD0, R/W - PD1, EN - PD2 */

#include <avr/io.h>

#define F_CPU 8000000UL

#include <util/delay.h>

#define LCD_DPRT PORTB // LCD data port

#define LCD_DDDR DDRB // LCD data DDR

#define LCD_DPIN PINB // LCD data pin

#define LCD_CPRT PORTD // LCD commands port

#define LCD_CDDR DDRD // LCD commands DDR

#define LCD_CPIN PIND // LCD commands pin

#define LCD_RS 0 // LCD RS


Department of Electronics & Communication Engineering

#define LCD_RW 1 // LCD RW

#define LCD_EN 2 // LCD EN

void lcd_command(unsigned char cmnd)

LCD_DPRT = cmnd; // send cmnd. To data port

LCD_CPRT &= ~(1<<LCD_RS); // RS = 0 for command

LCD_CPRT &= ~(1<<LCD_RW); // RW = 0 for write

LCD_CPRT |= (1<<LCD_EN); // EN = 1 for high to low pulse

_delay_us(1); // wait to make enable wide

LCD_CPRT &= ~(1<<LCD_EN); // EN = 0 for high to low pulse

_delay_us(100); // wait to make enable wide

void lcd_data(unsigned char data)

LCD_DPRT = data; // send data to data port

LCD_CPRT |= (1<<LCD_RS); // RS = 1 for data

LCD_CPRT &= ~(1<< LCD_RW); // RW = 0 for write

LCD_CPRT |= (1<<LCD_EN); // EN = 1 for high to low pulse


Department of Electronics & Communication Engineering

_delay_us(1); // wait to make enable wide

LCD_CPRT &= ~(1<<LCD_EN); // EN = 0 for high to low pulse

_delay_us(100); // wait to make enable wide

void lcd_init()

LCD_DDDR = 0xFF;

LCD_CDDR = 0xFF;

LCD_CPRT &= ~(1<<LCD_EN); // LCD_EN = 0

_delay_us(2000); // wait for init.

lcd_command(0x38); // init. LCD 2 line, 5X7 matrix

lcd_command(0x0E); // display on, cursor on

lcd_command(0x01); // clear LCD

_delay_us(2000); // wait

lcd_command(0x06); // shift cursor right

void lcd_print(char *p, int l)

if(l==1)lcd_command(0x80); // if l = 1, write in first line


Department of Electronics & Communication Engineering

if(l==2)lcd_command(0xC0); // if l = 0, write in second line

while(*p)

lcd_data(*p++);

int main(void)

lcd_init();

lcd_print("MICROCONTROLLER",1); // print ‘microcontroller’ in first line

lcd_print(" & INTERFACE",2); // print ‘& interface’ in second line

_delay_ms(200);

while(1); // stay here forever

}
Department of Electronics & Communication Engineering
Department of Electronics & Communication Engineering

AVR_LAB_6

AIM: Interfacing of keypad with ATMEGA32.

Tool :Arduino software

PROGRAM: -

#include <avr/io.h>

#define F_CPU 8000000UL

#include <util/delay.h>

#define KEY_PRT PORTC // keyboard port

#define KEY_DDR DDRC // keyboard DDR

#define KEY_PIN PINC // keyboard pin

void delay_ms(unsigned int d)

delay_ms(d);

unsigned char keypad [4] [4] ={ “0”, “1”, “2”, “3”, “4’,

“5”, “6”, “7”, “8”, “9”,

“A”, “B”, “C”, “D”, “E”, “F”};


Department of Electronics & Communication Engineering

int main(void)

unsigned char colloc, rowloc;

DDRD=0XFF;

KEY_DDR = 0XF0;

KEY_PRT = 0xFF;

while(1)

do

KEY_PRT &=0x0F; // ground all rows at once

colloc = (KEY_PIN & 0x0F); // read the columns

} while(colloc != 0x0F); // check until all keys released

do

do

delay_ms(20); // call delay

colloc =(KEY_PIN&0x0F); // see if any key is pressed


Department of Electronics & Communication Engineering

} while(colloc == 0x0F); // keep checking for key press

delay_ms(20); // call delay

colloc=(KEY_PIN & 0x0F); // read columns

} while(colloc == 0x0F); // wait for key press

while(1)

KEY_PIN =0xEF; // ground row 0

colloc = (KEY_PIN & 0x0F); // read the columns

if(colloc != 0x0F) // column detected

rowloc =0; // save row location

break; //exit while loop

KEY_PRT = 0xDF; // ground row 1

colloc = (KEY_PIN & 0x0F); // read the columns

if (colloc !=0x0F) // column detected

rowloc = 1; // save row location


Department of Electronics & Communication Engineering

break; //exit while loop

KEY_PRT = 0xBF; // ground row 2

colloc = (KEY_PIN & 0x0F); // read the columns

if (colloc !=0x0F) // column detected

rowloc = 2; // save row location

break; //exit while loop

KEY_PRT = 0x7F; // ground row 3

colloc = (KEY_PIN & 0x0F); // read the columns

rowloc = 3; // save row location

break; //exit while loop

// check column and send result to port D

if (colloc == 0x0E)

PORTD = (keypad[rowloc][0]);

else if(colloc == 0x0D)

PORTD = (keypad[rowloc][1]);
Department of Electronics & Communication Engineering

else if(colloc == 0x0B)

PORTD = (keypad[rowloc][2]);

else

PORTD = (keypad[rowloc][3]);

Return 0;

You might also like