0% found this document useful (0 votes)
190 views6 pages

Experiment No.: 5

1. The document describes an experiment using an AVR ATmega32 microcontroller to simulate LED blinking and a push button switch controlling an LED. 2. It provides code examples in both C and assembly language to toggle LEDs connected to PORTA by changing the PORTA output and inserting a delay. 3. The code sets the data direction register DDRA to make PORTA an output, then toggles the PORTA output between 0xAA and 0x55 with a 1 second delay to blink the LEDs.

Uploaded by

Dhrumil Maniyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views6 pages

Experiment No.: 5

1. The document describes an experiment using an AVR ATmega32 microcontroller to simulate LED blinking and a push button switch controlling an LED. 2. It provides code examples in both C and assembly language to toggle LEDs connected to PORTA by changing the PORTA output and inserting a delay. 3. The code sets the data direction register DDRA to make PORTA an output, then toggles the PORTA output between 0xAA and 0x55 with a 1 second delay to blink the LEDs.

Uploaded by

Dhrumil Maniyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR

DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

EXPERIMENT No.: 5

AIM: - To implement Simulation of following Program with AVR atmega32


a) LED blinking (port as well as pin)
b) Push button switch with LED

Apparatus: - Atmel Studio, Easy AVR 7 Trainer Board

Objectives:
 Overview of software and hardware tools used for programming & operation of ATmega16
 Basic circuitry required for operation of ATmega16
 To understand that how to make a port input or output
 First C Program to blink LEDs

We will be using the Atmel Studio 6.1 having built-in GCC


compiler to write the C code for ATmega32 AVR
Microcontroller. Proteus 7.6 for simulation and Easy
AVR 7 Trainer board for burning the code to ATmega32.

Theory:

Following three registers are associated to each I/O port of


ATmega16:

DDRx Data Direction Register to set the port input or


output
PORTx To write data on I/O pin if port configured as
output using DDRx
PINx To read data from I/O pins if port is configured
as input using DDRx

DDxn PORTxn I/O Comment


0 0 Input Tri-state (Hi-Z)
0 1 Input Pxn will source current if externally pulled down
1 0 Output Output Low (Sink)
1 1 Output Output High (Source)

For writing a high byte on Port A, we need to do the following:

DDRA = 0xFF; //0xFF = 1111 1111 (make Port A as output)


PORTA = 0xFF; //Send 1 to all pins of Port A

For reading a byte from Port A, we need to do the following:

Microprocessor and Microcontroller Subject Code: 3141008


GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

unsigned char x; //define a variable to store the value of PINA


DDRA =0x00; //0x00 = 0000 0000 (make PORTA as input)
x = PINA; //Read value to Port A and store on variable x

I/O Ports - Bit Manipulation using Macros:

Ports of the AVR ATmega16 cannot be accessed bitwise. So if we want to perform any function on a single bit,
following macros can be used for bit manipulation:

Macros definition:

#define BitGet(p, m) ((p) & (m))


#define BitSet(p, m) ((p) |= (m))
#define BitClear(p, m) ((p) &= ~(m))
#define BitFlip(p, m) ((p) ^= (m))
#define BitWrite(c,p,m) (c ? BitSet(p,m) : BitClear(p,m))
#define Bit(x) (0x01 << (x))

To set bit number 5 of PORTA:


Code: BitSet(PORTA, Bit(5)); // Set bit 5 (6th bit from LSB) of PORTA

To clear bit number 6 of PORTA with a bit mask:


Code: BitClear(PORTA, 0x40); // PORTA = x0xx xxxx (x are don’t cares)

To flip bit number 0 of PORTA:


Code: BitFlip(PORTA, Bit(0)); // Toggle Bit 0 of PORTA

To check bit number 3 of PORTA:


Code: if(BitGet(PORTA, Bit(3))) //Check if Bit 3 of PORTA == 1

To set or clear bit 0 of PORTB based on bit number 4 of PORTA:


Code:
if (BitGet (PORTA, Bit (4))) // If Bit 4 of PORTA == 1 then set Bit 0 of PORTB
BitSet ((PORTB, Bit (0)); // Else clear Bit 0 of PORTB
else
BitClear (PORTB, Bit (0));

To copy the status of single bit of a register to single bit of another register:
Code:
BitWrite (BitGet (PORTA, BIT (4)), PORTB, Bit (0)); //PORTB, Bit0 = PORTA, Bit4

[A] LED blinking at port


1. Toggle LEDs on PORTA
C Code to Toggle LEDs on PORTA with a delay of 1 Assembly Language Code to Toggle
second: LEDs on PORTA with a delay:
.INCLUDE "M32DEF.INC"
LDI R20,HIGH(RAMEND)
#define F_CPU 1000000UL // Define the //crystal frequency OUT SPH,R20
#include<avr/io.h> // Standard header //file for input and LDI R20,LOW(RAMEND)
output functionality OUT SPL,R20

Microprocessor and Microcontroller Subject Code: 3141008


GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

#include <util/delay.h> // For delay function


LDI R20,0XFF
int main(void) OUT DDRA,R20
{ LOOP: LDI R20,0XAA
DDRA = 0xFF; OUT PORTA,R20
PORTA=0XAA; CALL DELAY
while(1) //Forever Loop LDI R20,0X55
{ OUT PORTA,R20
CALL DELAY
_delay_ms (1000); //Delay of 1 Second
JMP LOOP
PORTA = ~PORTA; //Toggle state of port A
DELAY: LDI R22,0XFF
} AGAIN: DEC R22
} BRNE AGAIN
RET

Simulation:
U1
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
D2 2
PB1/T1 PD6/ICP1
20
3
LED-RED 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

Exerceise : Load Above Program In Easy AVR 7 And Verify The Output with connected LED.

2. LED blinking at pin

Microprocessor and Microcontroller Subject Code: 3141008


GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

.INCLUDE "M32DEF.INC"
U1
LDI R20,HIGH(RAMEND) 9 22
OUT SPH,R20 RESET PC0/SCL
23
PC1/SDA
LDI R20,LOW(RAMEND) 13 24
XTAL1 PC2/TCK
12 25
OUT SPL,R20 XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
SBI DDRA,5 39 28
PA1/ADC1 PC6/TOSC1
38 29
LOOP: SBI PORTA,5 PA2/ADC2 PC7/TOSC2
37
CALL DELAY PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
CBI PORTA,5 35
PA5/ADC5 PD1/TXD
15
CALL DELAY 34 16
PA6/ADC6 PD2/INT0
33 17
JMP LOOP PA7/ADC7 PD3/INT1
18
PD4/OC1B
DELAY: LDI R22,0XFF 1
PB0/T0/XCK PD5/OC1A
19
AGAIN: DEC R22 2 20
PB1/T1 PD6/ICP1
3 21
BRNE AGAIN PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
RET 5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

[B] Push button switch with LED


We are using the same LED as we’ve used in above experiment. Here we’re interfacing the
pushbutton to control the LED.

LDI R20,HIGH(RAMEND)
OUT SPH,R20
LDI R20,LOW(RAMEND)
OUT SPL,R20
U1
CBI DDRC,5 9 22
SBI DDRA,5 RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
LOOP: SBIS PINC,5 12 25
XTAL2 PC3/TMS
RJMP LOOP 26
PC4/TDO
SBI PORTA,5 40 27
PA0/ADC0 PC5/TDI
CALL DELAY 39 28
PA1/ADC1 PC6/TOSC1
CBI PORTA,5 38 29
PA2/ADC2 PC7/TOSC2
37
CALL DELAY PA3/ADC3
JMP LOOP
36
PA4/ADC4 PD0/RXD
14 R1
35 15 10k
DELAY: LDI R20,0XFF PA5/ADC5 PD1/TXD
34 16
AGAIN: DEC R20 PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
BRNE AGAIN 18
PD4/OC1B
RET 1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
Simulation

Exercise : Load Above TWO Program In Easy AVR 7 And Verify The Output with connected LED.

HOMEWORK:

Microprocessor and Microcontroller Subject Code: 3141008


GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

1. Write a program If Bit 0 of PORTB is high, LEDs connected at PORTA will glow
from LSB to MSB.

SOLUTION :

.INCLUDE “M32DEF.INC”
LDI R20,HIGH(REMEND)
OUT SPH,R20
LDI R20,LOW(REMEND)
OUT SPL,R20

SBI DDRA,5
LOOP: SBI PORTA,5
CALL DELAY
CBI PORTA,5
CALL DELAY
LMP LOOP
DELAY: LDI R22,0xFF
AGAIN : DEC R22
BRINE AGAIN
RET

2. Write a program If Bit 0 of PORTB is low, LEDs connected at PORTA will glow
from MSB to LSB. In both cases, one LED should be ON at a time.

SOLUTION:

LDI R20,HIGH(REMEND)
OUT SPH,R20
LDI R20,LOW(REMEND)
OUT SPL,R20

CBI DDRC,5
SBI DDRA,5
LOOP: SBIS PINC,5
RJMP LOOP
SBI PORTA,5

Microprocessor and Microcontroller Subject Code: 3141008


GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .

CALL DELAY
CBI PORTA,5
CALL DELAY
JMP LOOP
DELAY : LDI R20,0xFF
AGAIN: DEC R20
BRINE AGAIN
RET

Conclusion:
Simulation of LED blinking at port as well as pin program in Atmega32 microcontroller
have been successfully performed and verified. Simulation of Push button switch with
LED program in Atmega32 microcontroller have been successfully performed and
verified.

Microprocessor and Microcontroller Subject Code: 3141008

You might also like