0% found this document useful (0 votes)
41 views15 pages

FA21 - Lec08-2021-10-09 - AVR Programming in C

This document discusses AVR programming in C. It covers topics like loops, calculating clock cycles, writing simple assembly code with loops, I/O ports in AVR, inputting and outputting data, and the differences between assembly and C languages. It also discusses appropriate C data types for AVR like unsigned char, signed char, integer, and others. Finally, it discusses different ways to create time delays in AVR C code using for loops, delay functions, or timers.

Uploaded by

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

FA21 - Lec08-2021-10-09 - AVR Programming in C

This document discusses AVR programming in C. It covers topics like loops, calculating clock cycles, writing simple assembly code with loops, I/O ports in AVR, inputting and outputting data, and the differences between assembly and C languages. It also discusses appropriate C data types for AVR like unsigned char, signed char, integer, and others. Finally, it discusses different ways to create time delays in AVR C code using for loops, delay functions, or timers.

Uploaded by

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

AVR Programming

in C
LECTURE# 08
MICROPROCESSOR SYSTEMS AND INTERFACING

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Loop Revision
How it works
How many times the loop will iterate?
How many clock cycles each instruction needs?
How many times each instruction is repeated?

;this program adds value 3 to the R20 ten times


.INCLUDE "M32DEF.INC"
LDI R16, 10 ;R16 = 10 (decimal) for counter
LDI R20, 0 ;R20 = 0
LDI R21, 3 ;R21 = 3
MUST be together, AGAIN: ADD R20, R21 ;add 03 to R20 (R20 = sum)
to make decision on DEC R16 ;decrement R16 (counter)
DEC R16 result BRNE AGAIN ;repeat until COUNT = 0
OUT PORTB, R20 ;send sum to PORTB

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


Quiz 1b
Get ready for quiz
About to start

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 3


Quiz 1b
Q1. What is the value of the C and Z flags, upon last execution of DEC R16
◦ For the program given on the right LDI R17, 3
LDI R18, 10
LDI R16, 6 ;loop variable
loop: ADD R18, R17
Q2. Find the number of clock cycles needed DEC R16
◦ To execute the program given on the right BRNE loop

◦ Also find the duration when running on 2 MHz oscillator

Q3. Write a simple assembly code to load value 20 in R20.


◦ Increment R20 NNN times using a loop (where NNN is your reg#)
◦ Write incremented value to PORTD in each loop iteration
◦ PORTD must be declared output, in the start
◦ It is suggested to use BRNE conditional branching instruction

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 4


Last Lecture
I/Ports in AVR
◦ I/O Port registers (DDR, PORT and PIN registers)

Inputting data
◦ Role of DDR and PORT register
◦ Synchronizer delay

Dual Roles of Ports and Pins


I/O port bit-addressability
◦ Output using SBI and CBI instructions
◦ Input using SBIS and SBIC in instructions
◦ Examples

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 5


Assembly vs. C language
Hex files are downloaded into AVR Flash
Assembly language code is used to generate hex files
Assembly language is often tedious and time consuming
◦ C language is easier
◦ C language provide higher level programming
However, the generated hex files from C language may be large
◦ First C language code is compiled to get Assembly language code
◦ The generated assembly code is assembled to get hex file
The added process of compiling may not be as accurate as writing
assembly code
◦ There is a trade-off between convenience in coding and size/accuracy of
coding

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 6


Assembly vs. C language cont.
C language code can be ported easily to other devices
C language code libraries can be used
There are several third-party C compilers are available for AVR

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


C data types for AVR
Data types define the size required for the variable in the memory
AVR has limited registers and to generate smaller hex file
◦ We need to accurately declare variable types, how?

AVR is an 8-bit microcontroller


◦ So natural choice of C data type will be char (or unsigned char)

Storage of large variable (16- or 32-bit)


◦ Require additional space in the AVR (more than one register)
◦ Arithmetic operations on such variables require multiple iterations

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 8


C data types for AVR
unsigned char
Most proper choice, since AVR is an 8-bit microcontroller
The variable can store values 0-255 (0x00 - 0xFF)
This variable type should be first choice
◦ Unless the values to be stored are out of range

See Example 7-1


◦ Write a C program to send values 0x00 - 0xFF to Port B
◦ Will the loop ever break? #include <avr/io.h>
◦ No, z will always be int main (void)
{
◦ less than or equal to 255 unsigned char z; //loop count variable
◦ Because, z cannot be 256 DDRB = 0xFF;
for (z=0; z<=255; z++)
◦ Incrementing after 255 will overflow to 0 PORTB=z;
return 0;
}

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 9


C data types for AVR
unsigned char cont.
See Example 7-2
◦ Write a C program to send values 0,1,2,3,4,5,A,B,C,D to port B
◦ As ASCII characters
#include <avr/io.h>
int main (void)
{
unsigned char myList[] = "012345ABCD"; //character array

unsigned char z; //loop counter variable

DDRB = 0xFF; //port B as output

for (z=0; z<10; z++)


PORTB=myList[z]; //send characters to Port B

while(1); //the program stucks here

return 0;
}

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 10


C data types for AVR
unsigned char cont.
See Example 7-3
◦ Write an AVR C program to toggle all the bits of Port B 200 times.
#include <avr/io.h> //standard AVR header
int main(void) //the code starts from here
{
DDRB = 0xFF; //PORTB is output

PORTB = 0xAA; //Initial PORTB value is 0b 1010 1010

unsigned char z; //loop counter variable

for(z=0; z < 200; z++) //run the next line 200 times
PORTB = ~PORTB; //toggle PORTB

while(1); //stay here forever


return 0;
}

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 11


C data types for AVR
signed char
8-bit data type −𝟐𝟕 26 25 24 23 22 21 20
◦ Most significant bit represents sign D7 D6 D5 D4 D3 D2 D1 D0

The variable can store values -128 to +127


See Example 7-4 Value Hex
◦ Write an AVR C program to send values of -4 to +4 to Port B. -4 0xFC
#include <avr/io.h> //standard AVR header -3 0xFD
int main(void)
What binary
{ (hex) values -2 0xFE
char mynum[] = {-4,-3,-2,-1,0,+1,+2,+3,+4}; will be -1 0xFF
unsigned char z;
DDRB = 0xFF; //PORTB is output written on 0 0x00
for(z=0; z<=8; z++) //Nine iterations PortB? +1 0x01
PORTB = mynum[z];
while(1); //stay here forever +2 0x02
return 0;
+3 0x03
}
+4 0x04

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 12


C data types for AVR
Integer (int)
unsigned int
◦ 16-bit data type
◦ The variable can store values 0 to 65,536 (0x0000 to 0xFFFF)

signed int
◦ 16-bit data type
◦ Most significant bit represents sign
◦ The variable can store values -32,768 to +32,767

Since AVR is 8-bit microcontroller, will take 2 locations for each variable
Multiple instructions will be needed for performing operations

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


C data types for AVR
Other data types
If it is required to store larger number than 65536
◦ Or there is a need to store fractional numbers
◦ We may consider some other types

long (signed and unsigned) (32-bit)


float (for representing fractional numbers) (32-bit)
double (for representing fractional numbers) (32-bit)
Write a C program to toggle port B, 50,000 times or 100,000 times
◦ You may choose unsigned int for 50,000
◦ And unsigned long for 100,000

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 14


Time Delay
There are three possible ways of achieving time delay in AVR C
1. A simple for loop, etc. See example 7-7 (on the next slide)
◦ Inaccurate, Unable to theoretically ensure the delay
◦ A loop iteration may be mapped to different number of assembly instructions
◦ An empty loop may be removed during the optimization process
◦ Also depends upon the crystal frequency, verified using oscilloscope etc.
2. Predefined Functions
◦ _delay_ms() and _delay_us() functions
◦ varies across compiler, no standard functions
◦ If wrapper function is used for compatibility it will also consume some cycles
3. AVR timers
◦ Accurate
◦ Using hardware module inside the AVR (will be studied in later lectures)

End of lecture

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 15

You might also like