You are on page 1of 12

• 2. it is necessary to apply a logic one (1) to appropriate port. This may seem confusing but don't loose your patience. In this case. . voltage level on appropriate pin will be 5V (as is the case with any TTL input). in total of 32 input/output pins enabling the microcontroller to be connected to peripheral devices are available for use. It all becomes clear after studying simple electronic circuits connected to an I/O pin. In order to configure a microcontroller pin as an input.3 Input/Output Ports (I/O Ports) • All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured as inputs or outputs. whether it is to be configured as an input (1) or an output (0). • Similarly. • Pin configuration. voltage level on appropriate pin will be 0. depends on its logic state. i.e. it is necessary to apply a logic zero (0) to appropriate I/O port bit. In this case. in order to configure a microcontroller pin as an input. Accordingly.

Port-1.7). 2.2. • Port-0 Pin Structure Port -0 has 8 pins (P0. Port 0 is called 'true bidirectional port' as it floats (tristated) when configured as input. The structure of a Port-0 pin is shown in fig 6. 3 are called 'quasi bidirectional port'.0-P0. .• I/O Port Configuration • Each port of 8051 has bidirectional capability.

.

the upper transistor is 'on' and the lower transistor is 'off'. This high impedance pin can be pulled up or low by an external source. the port is used for address/data interfacing. When the control is '1'. '1' is written to the latch. When the port is used as an input port. Let us assume that control is '0'. Hence the output pin floats. In this situation both the output MOSFETs are 'off'. When control is '1'. The output becomes '0'. the port can be used as a normal bidirectional I/O port. Port-0 latch is written to with 1's when used for external memory access . If the address/data bus (internal) is '0'. But when '0' is written to the latch. Hence the output becomes zero.• • • • Port-0 can be configured as a normal bidirectional I/O port or it can be used for address/data interfacing for accessing external memory. When the control is '0'. When the port is used as an output port. Hence for normal address/data interfacing (for external memory access) no pull-up resistors are required. If the address/data bus is '1'. the upper MOSFET is 'off' and the lower MOSFET is 'on'. Hence the output is '1'. the pin is pulled down by the lower MOSFET. An external pull-up is required to output a '1'. a '1' written to the latch again turns 'off' both the output MOSFETs and causes the output pin to float. address/data bus controls the output driver MOSFETs.

leading to a possible wrong reading. In this input mode when '1' is written to the pin by the external device then it read fine.The structure of a port-1 pin is shown in fig 6.1-P1. .• Port-1 Pin StructurePort-1 has 8 pins (P1. If the external device is not able to sink the current the pin voltage may rise. When used as output port. it is dedicated solely for I/O interfacing.e. the pin is pulled up or down through internal pull-up.7) . • Port-1 does not have any alternate function i.3. But when '0' is written to the pin by the external device then the external source must sink current due to internal pull-up. To use port-1 as input port. '1' has to be written to the latch.

.

The I/O operation is similar to Port-1.7) . • Port-2 is used for higher external address byte or a normal input/output port. Here again due to internal pull-up there is limited current driving capability. .• PORT 2 Pin Structure Port-2 has 8-pins (P2. The structure of a port-2 pin is shown in fig 6. Port-2 latch remains stable when Port-2 pin are used for external memory access.4.0P2.

.

This port also has internal pull-up and limited current driving capability. The structure of a port-3 pin is shown in fig 6. .7) .• PORT 3 Pin Structure • Port-3 has 8 pin (P3.5. Port-3 pins have alternate functions. To use the port as input port. '1' should be written to the latch. The alternate function can be activated only if the corresponding latch has been written to '1'. • Each pin of Port-3 can be individually programmed for I/O operation or for alternate function.0-P3.

the delays provided by the software are not very precise. The value of counter increases by one. it is more than often required to generate time delays. A Timer is used for producing precise time delay. These SFRs are used to configure Timers in different modes of operations. Timers in a controller not only generate time delays but they can also be used as counters. First is by using the code.Timer • Timer is a clock that controls the sequence of an event while counting in fixed intervals of time. There are two possible ways of generating time delays. Timers in a controller are inbuilt chips that are controlled by special function registers (SFRs) assigned for Timer operations. However. While working with microcontrollers. Timers provide time delays that are very precise and accurate. The other method is to use Timers. every time its corresponding action or event occurs. Timers in a controller: Why to use them Most of the microcontrollers have inbuilt Timers. They are used to count an action or event. it can be used to repeat or initiate an action after/at a known period of time. An example could be setting up an alarm which triggers at a point of time or after a period of time. like using for or while loops in a C program. • • • . This feature is very commonly used in several applications. Secondly.