You are on page 1of 39

IO programming in C

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

• It is the binary representation of decimal


numbers(0-9).
• Unpacked BCD
• Packed BCD
• Unpacked BCD requires one byte of memory or
an 8-bit register to represent a digit.
• Lower 4 bit represent BCD number and rest of the
bits are 0.
 On ASCII keyboards, when the “0” key is
activated, “0011 0000” (30H) is
provided to the computer.

 Similarly, 31H (0011 0001) is provided for the


“1” key.
Data conversion pgms in C
• Packed BCD to ASCII conversion
-the number is first converted to unpacked BCD
and then tagged with 30H
Eg:-
Convert 29H to ACSII
Packed BCD unpacked BCD ASCII
0x29 0x02,0x09 0x32,0x39
 The Real-Time Clock (RTC) provides the ti me o f day (hour, minute,
second) and the date (year, m o nt h , day) conti nuously, regardless o f
whether the power is on o r o ff .

 This data is provided in packed BCD.


Write an AVR C program to convert packed BCD 0x29 to

ASCII and display the bytes on PORTB and PORTC .


To convert packed BCD 0x42 to ASCII and display the bytes on
portB and port C
#include<avr/io.h>
main()
{
Unsigned char x,y;
Unsigned char mybyte=0x42;
DDRB=DDRC=0xFF;
x=mybyte & 0x0F;
PORTB=x | 0x30;
y=mybyte & 0xF0;
y=y>>4; //shift right to lower 4 bits
PORTC=y | 0x30
}
ASCII to packed BCD conversion
• To convert ASCII to packed BCD ,it is first
converted to unpacked BCD ,and then
combined the numbers to make packed BCD .
• Eg:procedure to convert ASCII of ‘4’ and ‘7’ to
packed BCD.
Pgm converting ASCII to packed BCD
#include<avr/io.h>
main()
{
Unsigned char bcdbyte;
Unsigned char x=‘5’;
Unsigned char y=‘7’;
DDRC=0xFF; // make portC o/p
x=x & 0x0F; //mask upper 4 bits
x=x << 4; //shift left to make upper BCD digit
y=y & 0x0F; //mask the upper 4 bit
bcdbyte=x | y;
PORTC=bcdbyte;

}
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

You might also like