0% found this document useful (0 votes)
26 views2 pages

I/O Ports: Code Example

The document discusses the I/O ports on a mega 16 microcontroller. It explains that there are 32 general purpose digital I/O pins that each have corresponding bits in three registers: DDR, PORT, and PIN. The DDR register sets the pin as input or output, the PORT register controls the output value, and the PIN register reads the digital value of the pin. Writing to these registers sets or reads the binary values that control each pin's functionality.

Uploaded by

pawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

I/O Ports: Code Example

The document discusses the I/O ports on a mega 16 microcontroller. It explains that there are 32 general purpose digital I/O pins that each have corresponding bits in three registers: DDR, PORT, and PIN. The DDR register sets the pin as input or output, the PORT register controls the output value, and the PIN register reads the digital value of the pin. Writing to these registers sets or reads the binary values that control each pin's functionality.

Uploaded by

pawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy