Lab Handout#03 & 04
Lab Handout#03 & 04
RUBRICS:
DATA ANALYSIS
Performance Metric
PARTICIPATION
OBSERVATION/
ENGINEERING
CALCULATION
EXPERIMENT
AND CODING
CONDUCTING
TEAMWORK
Total Score
PROGRAM
MODERN
RESULTS
USE OF
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
#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:
If you want to store data from a port (i.e. 1 Byte) and only positive numbers then use:
if data is also negative numbers and value is between -128 to +127, then simply use:
char variable;
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;
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:
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
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;
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).
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;
}
ICSP
Header
VCC
MOSI A
MISO V
SCK R
RST
GND
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.