ES - AVR Programming in C - SecondPPts
ES - AVR Programming in C - SecondPPts
I/O PROGRAMMING IN C
• In all mc/mp input/output operation is
possible through i/o ports.
• While performing i/o operation port registers
are to be accessed.
• In AVR all port registers are bit accessible or
byte accessible.
• So we can access port registes as byte size or
bit size.
Byte size I/O
Each port in AVR is associated with three
registers –DDRx,PORTx,PINx.
-suppose if we access port-B registers as
PORTB,PINB,DDRB.
Bit level I/O programming in C
• All i/o ports of AVR are bit accessible.
• A bit in the port can be addressed by specifying the bit
position after the name of the port separated with a “.”.
• Eg:-
• The second pin of portB can be addressed as PORTB.2.
• This feature does not supported by all c compilers.
• So use bit wise AND or OR operation to access a single
bit of a given register without disturbing the rest of the
byte.
DATA CONVERSION
PROGRAMMING IN C
BCD(BINARY CODED DECIMAL )NUMBERS
}
Serializing data
• It is the most widely used application of the
rotate instruction.
• It is a method of transmitting a byte of data as
one bit at a time through a single pin of MC.
Data serialization in C
-sending data in one bit at a time through single
pin is called serializing.
Two methods are used to transfer data serially.
1)Using serial port for data transfer
2)Serializing data (to transfer data one bit at a
time and control the sequence of data and
spaces b/w them.
Qn:-pgm to send the values 55H serially via port C pin 3 of portc.TheLSB should go first.
#include<avr/io.h>
Int main()
{
Unsigned char byte=0x55H;
Unsigned char regLSB;
Unsigned char x;
regLSB=byte;
DDRC=DDRC | (1<< 3);
For(x=0;x<8;x++)
{
If (regLSB & 0x01)
{
PORTC=PORTC | (1<<3);
}
Else
{
PORTC=PORTC & ~(1<<3);
}//end else
regLSB=regLSB >> 1;
}//end for loop
Return 0;
}
Memory allocation in C
• AVR has three spaces to store data as listed
below.
• 1.64 KB of SRAM(0000-FFFFH)
• 2.2M or 4M byte of code space (000000-
1FFFFFH)
• 3.EEPROM to save variables that should not be
lost when the power is off.
chip Pgmmemory SRAM EEPROM
Atmega 8 8K 256B 256B
Atmega 16 16K 1K 512B
Atmega 32 32K 2K 1K
Atmega 64 64K 4K 2K
Atmega 128 128 K 8K 4K