0% found this document useful (0 votes)
34 views

Lab Handout#03 & 04

The document discusses programming AVR microcontrollers in C language. It covers basics of C programming for AVR I/O ports, configuring ports as input/output, reading and writing data to ports, and using loops. It provides examples of programs that write data to ports, read switch data and output to LEDs, and toggle an LED with delay.

Uploaded by

hk2359140
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)
34 views

Lab Handout#03 & 04

The document discusses programming AVR microcontrollers in C language. It covers basics of C programming for AVR I/O ports, configuring ports as input/output, reading and writing data to ports, and using loops. It provides examples of programs that write data to ports, read switch data and output to LEDs, and toggle an LED with delay.

Uploaded by

hk2359140
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/ 14

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY, JAMSHORO

DEPARTMENT OF ELECTRONIC ENGINEERING

INTRODUCTION TO EMBEDDED SYSTEMS (ES-314)


Batch: 21ES (5th Semester)

Lab Experiment #3 & 4


AVR I/O PORT C Programming, Simulation of the Program in Proteus and
Programming the code on Microcontroller chip using Extreme Burner
Name Hitesh kumar Roll # 21ES048
Signature of Lab Tutor Date

RUBRICS:

DATA ANALYSIS
Performance Metric

PARTICIPATION

OBSERVATION/
ENGINEERING

CALCULATION
EXPERIMENT

AND CODING
CONDUCTING
TEAMWORK

Total Score
PROGRAM
MODERN

RESULTS
USE OF

0.5 0.5 0.5 TOOLS


0.5 1 1 1 05
Obtained

OBJECTIVE(S)
The purpose of this lab is to:
# Topic # Of Lectures CLO Taxonomy level
Understand the basics of C-Programming of AVR
1
I/O Port
3 4,5 P3, A2
To Program the code on Microcontroller chip using
2
Extreme Burner.

OUTCOME(S)

a. An ability to use the techniques, skills, and modern PLO5: Modern Tool Usage
engineering tools necessary for engineering practice.
b. An ability to function on multi-disciplinary teams PLO9: Individual and Teamwork

REQUIRED TOOLS AND SOFTWARES:


 WinAVR (GCC Compiler)
 AVR Studio 4 or any latest version - (for Programming)
 Proteus 8 or any latest version - (for Simulation)
 Extreme Burner – AVR (for Programming through ICSP)
 AVR Trainer or AVR ATMEGA 16 Chip
 USBAsp Programmer
AVR C-language Programming Basics:

The basic Structure of AVR Programming in C-language is:

#include “avr/io.h”
int main(void)
{
// Your Program Here

return 0;
}
Declaring a variable:
Variable declaration is like putting your data in registers of Microcontroller. Holding your data
in variables / registers is necessary to process the data or holding it for later use in program.

Data can be of 1 byte or more, C-language offers multiple data types, each data type has different
data holding capacities:

Table 1: Some Data types widely used by C Compilers

If you want to store data from a port (i.e. 1 Byte) and only positive numbers then use:

unsigned char variable;

if data is also negative numbers and value is between -128 to +127, then simply use:

char variable;

Configuring Port as Input / Output:


To configure / initialize a port to work as Input or output, there is a register named DDR (Data
Direction Register), which is used to set the direction of Port as Input or Output. Writing one (i.e.
LOGIC HIGH) on any bit, makes that particular bit as OUTPUT, for example:

DDRA = 0b00000001;

This makes bit 0 of PORT A as output, while remaining all bits as Input, because writing ZERO
(i.e. LOGIC LOW) makes that bit as Input.
To declare all pins of PORT A as output:
DDRA = 0b11111111; or
DDRA = 0xFF;

And as
input: DDRA = 0b00000000; or
DDRA = 0x00;

Reading or Writing Data to Ports:


To Read data from any Port, we will use PIN function, if we want to read data from Port
A, then syntax will be:

Unsigned char z = PINA;

This will read data from PORT A and store data in variable named “z”, whose data type is
unsigned char and can store values from 0 to 255 (0x00 to 0xFF).

And to write data to Port, the keyword PORT will be used, like:

PORTA=0xA2;

It will write data “A2” in hex to Port A, however you can also assign variable instead of
constant data 0xA2, like:

unsigned char z=0xA2;


PORTA=z;

PORTA = z; will write data of z to PORT A, and the data in z variable is 0xA2, hence 0xA2 will
be written to PORT A.

Looping in Programs:
Sometimes we need to do counting in program, or repeat some portion of code in
program, this can be done through loops. AVR supports same C-language loops, like: FOR-
LOOP, WHILE LOOP and DO-WHILE LOOP.

For Loop:
char i=0;

for(i=0;i<10;i++)
{
// code here
}
This will repeat a portion of code, 10 times.

While Loop:
while (a< 10)
{
// code here
}

This will repeat the portion of code, until value of “a” is less than 10. For an infinite loop, use:

while(1)
{
// code here
}
This will repeat code unlimited times.

Example Program # 01: A program that will write a constant Data 0xF0 to Port B (upper nibble
of Port B as ON and lower nibble as OFF).

Program:
#include “avr/io.h”
int main(void)
{
DDRB=0xFF; // Port B as output
PORTB=0xF0; // Writing F0 to Port B

while(1); // Infinite Loop Here

return 0;
}
Explanation:
First, we need to initialize ports that we will use in this program, Ports as input or Output,
then do the task as said, write the data to PortB. For writing we will use PORTB = data;

Simulate the Program in Proteus

 Write the above program#01 in AVR Studio and build the Program. Note the Hex file
creation and its path.
 Open Proteus, draw the schematic in Proteus and then link the Hex file created by AVR
Studio with the Microcontroller in the Proteus as done in previous lab.
 Observe the output, as shown in the figure below.
Example Program # 02: Get Data from Port C (Switches) and put it on Port B (LEDs)
Program:
#include "avr/io.h"
int main(void)
{
DDRB=0xFF; // Output
DDRC=0x00; // Input
while(1)
{
char z=PINC;
PORTB=z;
}
return 0;
}

Explanation:

First initialize the ports, PORTB as output and PORTC as input, then copy the data from
switches, to copy / read we will use PINC (Reading from PortC). The data which is read from
switches must be stored in some variable; we have used “z” here. Now write the data, which is
read from switches, to PortC, to write, we will use PORTC (Writing to PortC).

Proteus Output Simulation result of program#02

Example Program # 03: A C-program that toggle an LED connected to PB5 of Port B with a
small delay. Use a predefined delay function in Win AVR.

Program:
#include "avr/io.h"
#include "util/delay.h"
int main(void)
{
DDRB=0xFF; // Output mode
while(1)
{
PORTB ^= (1<<5); //toggle PB5
_delay_ms(1000);

}
return 0;
}

Programming the AVR Microcontrollers


The Programming Software: The programming Software is required to transfer hex file to the
target MCU. There are various options available depending on the programmer you’re using. In
this tutorial series we will be using Extreme Burner AVR. There is another software that you
could also try is Khazama.
USBAsp Programmer Connection Diagram:
Whenever you design any circuit, just place a header connector as shown in Fig below, by
connecting USBAsp Programmer with this, you can easily program the chip on the board
without taking IC out of socket.

ICSP
Header
VCC
MOSI A
MISO V
SCK R
RST
GND

Programming the code on Microcontroller chip


To burn the code on ATMega16 AVR chip and observe the effects directly on the hardware we
need to connect the AVR chip with PC/Laptop using USBASP programmer as described in
above figure. To burn the code in AVR chip follow the following steps.
 Write the above program#03 in AVR Studio and build the Program. Note the Hex file
creation and its path.
 Open the Extreme Burner AVR-Programmer Application. Click on OPEN icon as shown
in below figure.
Write All
Load Hex

 Browse for your required Hex file and then click on the write All icon as shown in above
figure.

 The following window will appear when you click on the Write All icon, it will show you
the Burn program progress.
 After successfully uploaded your program in AVR microcontroller, Now observes the
results on hardware.

Lab Tasks:
Simulate on Proteus and execute the following programs on AVR Chip. Attach the C-program
files and simulation results snapshots along with the handouts.

1. Write a C program that gets data from switches connected at Port C and
display the complimented output data to Port B.
2. Write a C program that gets data from the lower nibble of Port C and put it to upper
nibble of port B.
3. Write a C program that counts numbers from 0 to 255 on Port B.
4. Write a program to generate these random numbers on Port C: 100, 255, 123, 80, 66,
36, 15, 00.
5. Write a C program that toggles an LED connected PA2 with a small delay.

You might also like