7
\$\begingroup\$

I'm a bit new to microcontrollers. Can you please tell me what is a port and pin in a microcontroller? What are their uses (pin and port)?

\$\endgroup\$

3 Answers 3

17
\$\begingroup\$

The pins are what stick out of an IC, and connect electrically to the outside world. Ports are represented by registers inside the microcontroller, and allow the program (firmware) to control the state of the pins, or conversely, read the state of the pins if they are configured as inputs. There is a one-to-one correspondence between the pins on the microcontroller and the bits in its registers.

Take, for example, this 28-pin microcontroller, which happens to be a PIC24FJ64A004:

enter image description here

This processor has a 16-bit architecture, so the ports and registers are all 16-bits wide. Other common data widths (and thus port widths) in microcontrollers are 8 and 32. As shown in the above diagram, the port pins do not necessarily have to be grouped together.

Since this particular chip has only 28-pins, there is only one register that can be fully implemented, that is has pins corresponding to all 16 bits in the I/O register. That is PORT B, and the pins are colored in red in the diagram above. There is another port that is partially populated, i.e. PORT A which has 5 bits implemented, RA0 through RA4. I will just stick with PORT B.

There are other microcontrollers in this same processor family that come in a 44-pin package, where additional register pins are defined (RA7-RA10, and RC0-RC9.

As mentioned earlier, the ports are manipulated through various registers.

Depending on the type of processor, the registers may appear in the same memory map as the processor's memory (RAM); in this case the addresses are reserved for this purpose and that segment of RAM is not implemented. The advantage here is that all of the instructions that operate on RAM will also work with the I/O registers.

The other scheme, is to have a special address space for I/O registers, and have special instructions (i.e. INP and OUT) which only access these registers. Which scheme is used is historical, back in the 1970's Intel started using the separate I/O addressing scheme, and Motorola among others chose to put both I/O and RAM addresses into the same memory space.

These registers associated with PORT B in the PIC24FJ64A004 are:

enter image description here

The configuration of the port is done via the TRISB and ODCB registers. TRIS states for tri-state, which is a condition where a pin is put into a high impedance state and cannot drive any outputs. The TRISB register determines whether each PORT B pin is an input or output. (On some other microcontrollers, this is called a Data Direction register, or DDR -- DDRB in the case of PORT B. I think DDR is a better name for this register then TRIS.)

Setting the associated bit in TRISB makes the pin an input, and setting it to 0 makes it an output.

Each pin, if configured as an output, can also be configured as either open-drain or push-pull. This is done with the ODCB register (open drain control register),

Open-drain means the pin only "sinks" current, i.e. it can control an output already connected to a positive voltage, such as an LED, or other I/O peripheral connected to Vcc (the system voltage) by turning it off. Push-pull means the pin can both "source" voltage (set the output to a high voltage equal to Vcc), or sink current the same as an open-drain output.

Setting the associated bit in ODCB makes the pin open-drain, and setting it to 0 makes it push-pull.

Why open-drain? One reason is it allows several devices to drive the same line, which is connected to a pull-up resistor. When the open-drain output is set to 1, it is put into a high-impedance state, same as an input. So only the device that is set to 0 (ground) actively controls the line.

If the pin is configured as an input, then reading the port will read the state of the pin. If the pin is configured as an output, then writing to either the LATB or PORTB registers will change the state of the output pin. Why both LATB and PORTB? Because reading PORTB always reads the state of the pin, whether the pin is an input or output. Reading the LATB register reads whatever was written to the port.

Setting the bit in the output register to 1 of a pin configured as open-drain output puts the pin in a high-impedance state, and setting it to 0 sets the output to ground. Setting the bit in the output register to 1 of a pin configured as push-pull drives the output high, and setting the bit to 0 drives the output low.

Although one can write to or read from the entire port all at once, there are instructions for setting or testing individual bits in a I/O register. This saves, for example, the need to remember the previous value of an I/O register when writing back the entire contents.

Note in the diagram of the microcontroller above, in addition to the RBx designations, there are a lot of other functions labeled with these pins. This allows the microcontroller to configure the pins for other purposes such as analog inputs, comparators, external oscillators, input capture/output PWM, UART, SPI, I²C, and PMP (parallel master port).

This diagram shows the internal logic associated with one pin of an I/O port, plus the circuitry used to share a pin with a peripheral.

enter image description here

To enable any of these peripherals, it is usually only necessary to set a enable bit in the control register for the peripheral. This allows the peripheral to control the corresponding pin, and disconnects it from the PORT registers.

One of the unusual features of the PIC24 family is that it has a feature called PPS (Peripheral Pin Select). That means many of the functions shown in the diagram of the microcontroller can actually be reassigned to other pins -- the diagram only shows the default locations. The pins labeled RP represent the remappable peripheral pins.

Finally, this microcontroller has a 10-channel ADC. Eight of these channels share pins with PORTB (RB0-RB3 and RB12-RB15). A register called AD1PCFG is used to control whether a pin is used as an analog input, or whether it is used by PORT B.

\$\endgroup\$
12
\$\begingroup\$

As an example, a port in a microcontroller such as that found in the Arduino development boards (like the Uno) is an array of usually 8 pins (because it's an 8-bit architecture, it makes sense to arrange pins in groups of 8) which can be configured either to act as digital (on/off) inputs, outputs, or be used by a special peripheral such as an Analogue to Digital Converter, Timer for things like Output Compare (PWM), and sometimes these can be connected to special internal interrupt generation for example to be woken up by an external source if the microcontroller is sleeping.

Here is a quick pin-out diagram of an example microcontroller:

pin out diagram

See how the labels are things like PB0 ... PB7, and PA0 to P7 etc? These are 8-bit ports labeled B and A respectively. Microcontrollers can have any number of ports, it's really up to the architecture, pin count of the package, and overall design which dictates how many pins are in a port, how many ports/pins are in a microcontroller, and what they might do. Each one is special, and different, but most follow the Port labeling in letters, with pin numbers specifically in numbers, and P means "Port". So PA0 refers to Port A, pin 0. Pin numbers usually start at 0, I assume due to firmware/programming preferred indexing starting at 0. Note that in footprints and schematic designs, in the picture too, the actual physical pin numbers always start at 1!

The following picture shows a simplified version of what is in a modern microcontroller's pin (which remember is usually grouped into ports, or arrays of this circuit all in a row).

pin circuit

If the pin is set as an input, it will route the connections of that pin to the "input" side of the circuit and disable the output side. Same thing goes with if the pin is configured as an output pin, the input side is disabled/disconnected.

The "logic functionality" block of each pin in the middle of the diagram is usually connected to the port's data register, where the processor can read the values on all of it's pins at any point in time. Note that the data register is merely a representation of the value seen at the pin, it's not exactly a direct connection. The data register may usually be written to, if the pin is set to an output, in order to change the output value to a 0 or a 1. Again, setting these values is just a representation, the output port drivers shown in the diagram as MOSFETs will at least try to match the required value, but external circuitry or problems may not actually make that come true.

Remember that logical input/output values are really just comparison thresholds of the given VCC/source voltage for the microcontroller. If you supply the microcontroller with 5V, it will use this as the comparison voltage for its logic outputs and inputs. Outputs merely because the output driver FETs source current and drive to the same potential difference (AKA voltage) as VCC when going "logic high" and sink/pull current towards the Ground reference when going "logic low".

For input, there are high impedance comparators (maybe with Schmitt Triggers to have some hysteresis and prevent logic high/low uncertainty if an input is riding the border of these values) which use VCC (the input power supply voltage, remember!) as the comparison point, and then a certain % of this is used to say whether an input signal is a logic HIGH or logic LOW signal. Often the percentages used are >66% of VCC for logic HIGH, and <33% for logic LOW. This leaves a nice gap for unknown or noisy signals to be rejected. If the microcontroller in my above example sees 4V on the input, this is above 66% of 5V (3.3V) and will be seen on that Port's data register, for that particular pin, as a "1".

\$\endgroup\$
1
\$\begingroup\$

Ok first the pins are the physical leads and the ports are the I/O input/output channel connected internally to those pins. The pins are used for indexing and orienting the physical chip. For instance pin 1 is almost always in the top left of the chip, but is always to the left of the half circle cut-out at one end of the chip.

The ports are used to communicate to and from the chip, for example PortA bit 1 might be on pin 1 and PortA bit 2 might be on pin 2. Of course this is just an example as it is different with every microcontroller.

\$\endgroup\$
4
  • \$\begingroup\$ So you mean all the legs we see outside the ic(except vcc, gnd..etc) are ports. We can't see pins outside? \$\endgroup\$
    – MacroBoy
    Oct 16, 2014 at 16:46
  • \$\begingroup\$ @MacroBoy pins are the legs of the package. There are ways to "multiplex" internal port connections to outside physical pins though, on some of the very large microcontrollers. This is because some ports may be used for many internal peripherals/functions while others are 'connected' to the outside world \$\endgroup\$
    – KyranF
    Oct 16, 2014 at 17:56
  • \$\begingroup\$ so What do you mean by DDRC and DDRB \$\endgroup\$
    – MacroBoy
    Oct 17, 2014 at 14:46
  • \$\begingroup\$ @MacroBoy -- see my answer regarding the DDR (data direction register) pins. \$\endgroup\$
    – tcrosley
    Oct 17, 2014 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.