Comsats University Microprocessor Systems & Interfacing EEE-342
Comsats University Microprocessor Systems & Interfacing EEE-342
Comsats University Microprocessor Systems & Interfacing EEE-342
Name
Registration
Number
OBJECTIVES:
Understand the minimal circuit required to start using a microcontroller
Learn to program (download code to program memory of) a microcontroller using
Arduino board.
To understand and use digital I/O ports of AVR microcontroller
REQUIRED TOOLS:
SOFTWARE TOOLS:
Atmel Studio (Version 7) or AVR Studio (Version 4)
Proteus ISIS
Arduino IDE
AVRDUDESS
HARDWARE TOOLS:
PRE-LAB
Operating Voltage 5V
Analog Input Pins 8 DC Current per I/O Pin 40 mA Flash Memory 32 KB (of which 2KB used
by bootloader) SRAM 2 KB EEPROM 1 KB Clock Speed 16 MHz
The prefix 0x signifies hexadecimal number. 0xFF means decimal 255 or binary 11111111
which means all the bits in register are at high logic level. 0x00 means all the bits in the register
are at low logic level. Each physical pin of a port is configured independently and thus a port can
have some of its pins configured as input and the others as output pins. For example:
DDRD = (1<<0)|(1<<4)|(1<<5)|(1<<7);
This is equivalent to
DDRD = 0b10110001;
or
DDRD=0xB1;
In the above example, port D is initialized such that the pins PD0, PD4, PD5 and PD7 are output
pins whereas pins PD1, PD2, PD3 and PD6 are input pins.
We can change the value of a single bit without disturbing the previous value of all the other bits
of the register. For example:
DDRD |= (1<<5);
This will set bit 5 of DDRD while retaining the values of all other bits.
Similarly,
PORTx is an 8-bit register which stores the logic values that are to be transferred on the physical
pins of Portx if the pins are configured as output pins. In short, once the direction of DDRx
register is defined as an output pins, we need to set them to give an output of HIGH logic or
LOW logic. So to write values to a port, you need to write the values to the “PORT” register of
that port.
PORTD = 0x49;
PINx is an 8-bit register that stores the logic value, the current state, of the physical pins on
Portx. When you set any port as input you have to read its status using this register.
This is equivalent to
PINB = 0b01001001;
Or
PINB = 0x49;
IN LAB-TASKS
Task-1: Write the given code in AVR studio and compile it. Use AVR dudes to upload the
hex file on the controller. Now see, the led will blink when connected to the controller.
Code:
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
int main()
{
DDRB = 0b11111111;
while(1)
{
PORTB = 0b00001000;
_delay_ms(1000);
PORTB = 0b00000000;
_delay_ms(1000);
}}
Task 2: Switches are connected to one port of ATmega328p for input and LEDs are
connected to another port for output. Using these, perform a task assigned by your lab
instructor. Students should write the code for given task, simulate it on proteus and then
implement it on hardware.
/*This code will configure Port B for output and then toggle Port B pin 3 with a delay of
1000ms*/
#include <avr/io.h> /*This header file includes the apropriate I/O definitions for the device
*/
#define F_CPU 16000000UL //XTAL Frequency =16MHz
#include <util/delay.h> /*This header file is required for generating delays*/
int main ()
{ // Write your code here
DDRB = 0b00000000; //Configure Port B as Output
DDRD = 0b11111111;
while (1) //This loop will run forever
{unsigned char x; x=PINB;
if(x== 0b00000000)
PORTD = 0b00001000;
else if (x==0b0000001)
PORTD=0b00010000;
else if (x==0b00000010)
PORTD=0b10000000 ;
else if (x==0b00000011)
PORTD=0b10001100 ;
else
PORTD=0b00000000 ;
}}
CIRCUIT IN PROTEUS
AVR DIGITAL INPUT/OUTPUT: Atmel AVR ATMega 328P provides 23 I/O pins to interact
with outside world in the form of logic values. Port B and Port D have 8 pins each whereas Port C
consists of 7 pins. In Arduino nano, 20 pins are available for digital I/O.
Interfacing of devices was done with microcontroller and in turn program microcontroller in
order to perform specified tasks.
TASK 1 OUTCOMES: In this task given code was built on AVR studio and later simulated on
proteus. Afterwards .Hex file was burned to ATMega328P. The code was implemented on
hardware using AVRDUDUESS.
TASK 2 OUTCOMES: In this task given code was built on AVR studio and later simulated on
proteus. The code configured Port B for output and then toggle Port B pin 3 with a delay of
1000ms. Afterwards .Hex file was burned to ATMega328P. The code was implemented on
hardware using AVRDUDUESS.