Você está na página 1de 2

I/O PORTS

At mega 16 have 32 general purpose digital I/O pins. Corresponding to every pin, there are 3 bits
in 3 different registers which control its function. Let’s say we are talking about the pin PA0.
The three registers involved with this pin are DDRA, PORTA and PINA and the corresponding
bits are DDRA0, PORTA0 and PINA0.

DDR - is the Data Direction Register - writing 1 to DDRA0 makes the pin PA0 act like an
output pin and writing 0 makes it an input pin.

Code example:
DDRC=130;
or,
DDRC= 0b 10000010;
or,
DDRC= 0x 82;

Either of the above statements will make the PC1 and PC7 as output and rest as input.

It is to be noted that writing some value onto a register simply means that the bits of the
register will attain values such that the binary number represented by all the 8 bits of the
register together equals the number assigned to them. e.g. writing 0b10110101 means that
the bits in the register will become like this:
PORT register - If DDRA0 is set as 1,

 writing 1 to PORTA0 gives a high output on pin PA0


 writing 0 to PORTA0 gives a low output on pin PA0
If DDRA0 is set to 0 (input),

 writing 1 to PORTA0 simply pulls up the pin to Vcc via 100k resistance
 Writing 0 to PORTA0 makes the pin tri-stated (very high input resistance, practically
floating.) This means that in absence of any input from outside the pin will just read some
arbitrary value.
PIN register - This register is used for reading the digital value of the pin. It can be thought of as
actually connected to MCU physical pins. If voltage of the pin (either in case of input or output)
at any instant is low it will read as 0 otherwise 1.
Code example:
int read;
read = PINB; // stores the value of 8 bit PINB register in the variable read
or,
read =PINB & 4; // this statement stores the value of PB2 bit in read, guess how?

Você também pode gostar