You are on page 1of 21

By the end of this session, you

should
◦Use various be able to:
data types in AVR C programs
◦Implement time delay functions in AVR C
programs.
◦Write IO programs in AVR C

2
Why AVR programming in C ?
• Writing programs in ASM is tedious and
time consuming where as programming in
high level language is less time
consuming and much easier.

• So high level language compilers are


developed for use with MPs/MCs.
The following are some major
reasons for writing program in
C instead of AL.
◦It is easier and less time consuming to write in
C than in assembly.

◦C is easier to modify and update.

◦You can use code available in function libraries.

◦C code is portable to other microcontrollers


with little or low modification.
Unsigned char
• 8-bit data type that takes values in the range of 0-
255(00-FF).
• Is one of the most widely used data types for the AVR.
• In many situations like counter value settings
• There is no need for signed data, you can use the
unsigned char instead of signed char
• Compiler uses signed char as the default unless it is
declared as unsigned.
• We can also an unsigned character data type for a
string of ASCII characters.
Signed char
• It is an 8-bit data type in which MSB is used to
indicate the sign.
• rest 7-bits represent the magnitude.
• takes values from -128 to +127.
• In situations where + and – are needed to
represent a given quantity such as
temperature, the use of signed char data type
is necessary.
Program segment to send vales -4 to +4 to
PORTB
#include<avr/io.h>
int main(void)
{
char num[]={-4,-3,-2,-1,0,+1,+2,+3,+4};
unsigned char x;
DDRB=0xFF;
for(x=0;x<=8;x++)
PORTB=num[x];
while(1); //stay here for ever
return(0);
}
On execution of the above program PORTB displays
FCH,FDH,FEH,FFH,00H,01H,02H,03H,04H being the Hex values for{-4,-3,-2,-
1,0,1,2,3,4}respectively
Unsigned int
• It takes 16-bit data type and takes the values
range from 0-65535(0000-FFFFH).
Pgm segment to toggle all bits of port-B 50,000 times.

• #include<avr/io.h>
Int main(void)
{
Unsigned int z;
DDRB=0XFF;
For(z=0;z<5000;z++)
{
PORTB=0x55;
PORTB=0XAA;
}
While(1);
Return(0);
}
Signed int
• It is a 16-bit data type that uses MSB to
represent sign(+ or -).
• The magnitude of the number is represented
by 15 bits (D0-D14) and takes the values from -
32768 to +32767.
• AVR c-compiler support long data type when
values are greater than 16-bit.
• For fractional numbers AVR C-compiler support
float or double data type.
Pgm segmen to toggle all bits of Port-B
100,000 times.
• #include<avr/io.h>
• Int main(void)
• Unsigned long z; //since it is more than 65535
• DDRB=0xFF;\
• For(z=0;z<10000,z++)
• {
• PORTB=0x55;
• PORTB=0xAA;
• }
• While(1);
• Return(0);
• }
Time delay
• In AVR C,we can create time delays I n 3
different ways.
• 1.using simple for loop
• 2.using pre-defined function
• 3.using AVR timers.
• While using time delays using for loop,take
care of two factors that affect the accuracy of
the time delay.
1.Crystal frequency
- frequency of the crystal connected to the XTAL1-
XTAL2 is the most important factor in the delay
calculation.
-Duration of clock period for the instruction cycle is
the function of this crystal frequency.
2.Compiler used:-
-different compiler produces different hex code.
-This will produce different time delays for the same
program.
-So using for loop to write delay for C ,we have to
measure the exact time duration using oscilloscope.
• Another way of generating time delay is to use
predefined function such as _delay_ms() and
_delay_us() defined in delay.h in WinAVR or
_delay_ms() and delay_us() defined in delay.h
in CodeVision.
• The only drawback of using these functions is
the portability problem because different
compilers do not use the same name for delay
function.

You might also like