Parallel I/O Ports of Embedded Microcontrollers- Explained
In this article, we go over what parallel I/O ports are and how to code them in C for embedded applications.
The parallel I/O ports are the most general-purpose I/O devices.
Each of the parallel I/O ports has 3 associated I/O registers: the data direction register, DDRx, the port driver register, typically called PORTx, and the port pins register, PINx.
The data direction register's, DDRx, purpose is to determine which bits of the port are used for input and which bits are used for output. In the expression, DDRx,"x" is A, B, C, and so on depending on the specific processor and parallel port being used. The input and output bits can be mixed as desired by the programmer. A processor reset clears all data direction register bits to logic 0, setting all of the port's bits for input. Setting any bit of the data direction register to a logic 1 sets the corresponding port bit for output mode. For instance, setting the least significant 2 bits of DDRA to a logic 1 and the other bits to logic 0 sets the least significant 2 bits of port A for output and the balance of the bits for input.
Writing to the output bits of port A is accomplished by the following code:
PORTA= 0x2;
Reading from the input bits of port A is accomplished as follows:
x= PINA;
In the line of code above, "x" would contain the value from all of the bits of port A, both input and output, because the PIN register reflects the value of all of the bits of the port.
Input port pins are floating, that is, there is not necessarily a pull-up resistor associated with the port pin. The processor can supply the pull-up resistor, if desired, by writing a logic 1 to the corresponding bit of the port driver register.
An example of this is shown below:
DDRA= 0xC0;
PORTA= 0x3;
Here is another example in which we set the upper 4 bits of PORTC as output and enable pull-ups on the 2 least
significant bits of PORTC. We then read the lower 4 bits of PORTC and shift them 4 bits left and output them to the upper 4 bits of PORTC.
#include
void main (void)
{
DDRC= 0xf0;
PORTC= 0x03;
while(1)
{
PORTC= (PINC << 4);
}