ES AVR IOPort Programming C FirstPPTs
ES AVR IOPort Programming C FirstPPTs
ES AVR IOPort Programming C FirstPPTs
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.
• #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 in 3
different ways.
• 1.Using simple for loop
• 2 Using a pre-defined function
• 3.Using AVR timers.
• While using time delays using a 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.