Lab3 Manual: Microprocessor Systems and Interfacing
Getting Started
Lab3
Multiple Operations on Single PORT
Microprocessor Systems and Interfacing
Prepared by
Mumtaz Ali/Sumera Butt
COMSATS Institute of Information Technology
Lab3 Manual: Microprocessor Systems and Interfacing
Objectives
To learn how to use single PORT for multiple operations in C-Code Programming
Software Requirement
AVR Studio produced by Atmel
Proteus
Introduction
Bitwise operations on any port are not easy when programming microcontroller using c-code. It
put a limitation on use of single PORT for multiple operations. Multiple operations on single PORT are
some times necessary when you do not have enough pins to connect external devices. We will learn
about how to use a single PORT for multiple operations by following lab experiment. You will draw a
following circuit in Proteus using ATmega16 microcontroller.
Figure 1
You will program microcontroller for this circuit so that your circuit operate according to the following
description. You will run 7-segment LED on first four pins of PORTC and implement shift register on
remaining pins of PORTC.
Input Display on LED Value on Shift Register
Push Button 1 Increment on 7-segment LED ( 0 to F in Stays same as before
hex).
Push Button 2 Stays same as Before Shift to right (1000 to 0100 or
0100 to 0010 or 0010 to 0001 or
0001 to 1000)
COMSATS Institute of Information Technology
Lab3 Manual: Microprocessor Systems and Interfacing
Push Button 1 and Push Button 2 will be connected to pin0 and pin1 of PORTA respectively. Pin0 to pin3
of PORTC will be used to operate 7-segment LED and pin4 to pin7 of PORTC will be used to implement
shift register. Initially circuit will start with 0 on LED and 1000 on shift register.
Code for microcontroller Program
#include<avr\io.h>
void main()
{
DDRA=0b11111100; \\ setting up input pins
PORTA=0x00; \\ initialize porta
DDRC=0xff; \\ set portc as output
PORTC=0b00010000; \\ initialize portc with zeror on LED and 1000 sequence on shift register
char c1,c2,c3,c4;
c1=0b11110000; \\ c1 will mask upper four bits
c2=0b00001111; \\ c2 will mask lower four bits
while(1)
{
if(PINA==1) \\ check if button 1 is pressed
{
c3=PORTC&c1; \\ mask upper four bits into c3
c4=PORTC&c2; \\ mask lower four bits into c4
c4=c4+0b00000001; \\ increment for LED display
c4=c4&c2; \\ again mask lower four bits in case there was a carry to fifth bit.
PORTC=c4|c3; \\ again combine upper and lower bits of PORTC
while(PINA!=0) \\ stays in while loop until button is released
{}
}
if(PINA==2)
{
c3=PORTC&c1;
c4=PORTC&c2;
switch(c3) \\ check status of upper four bits of PORTC and change accordingly
{
case 0b00010000: c3=0b00100000;
break;
case 0b00100000: c3=0b01000000;
break;
case 0b01000000: c3=0b10000000;
break;
case 0b10000000: c3=0b00010000;
break;
}
PORTC=c4|c3;
while(PINA!=0)
{}
}
}
}.
Components required for simulation in Proteus
• Push Buttons
• 7-segment LED Binary Coded Decimal
• 470 ohms resistors
• Battery 5 volts
• ATmega16 Microcontroller
COMSATS Institute of Information Technology
Lab3 Manual: Microprocessor Systems and Interfacing
LAB TASK
Draw and simulate a similar circuit on Proteus and program microcontroller of the circuit. This
time the circuit will have some changes. Display on 7-segment LED will depend on your registration
number. For example if your registration number is 075, then the LED will be initialized by zero. By
pressing push button 1, next number LED should display will be 7 and then 5 and then again 0 and so on.
Also shift register in previous circuit was shifting bits to the right. Your circuit should shift bits to the left.
COMSATS Institute of Information Technology