8 Programming parallel ports
In this lecture pin diagram of 8051 controller will be shown and discussed, an introduction to Ports of 8051 controller will be given and assembly programs for handling pins and ports of controller will be discussed.
1. Pin diagram of 8051 and ports
8051 family members come in different IC packages, such as DIP (dual in-line package), QFP (quad flat package), and LLC (leadless chip carrier), they all have 40 pins that are dedicated to various functions such as I/O, RD, WR, address, data, and interrupts. some companies provide 20-pin versions of the 8051 with a reduced number of I/O ports for less demanding applications. Majority of developers uses the 40-pin chip, we will concentrate on that. Figure 8-1 shows the pin diagram of 8051.
A set of 32 pins are set aside for the four ports P0,P1,P2 and P3 where each port takes 8 pins. Pin 40 provides supply voltage to the chip. The voltage source is +5V. Pin 20 is the ground. The rest of the pins are designated as XTAL1,XTAL2,RST,EA,PSEN and ALE. XTAL2/XTALI are for oscillator input which provides the clock pulse to microcontroller. The pins from 32 to 39 form the port0( AD0/AD7). P0.0 to P0.7 are multiplexed pins used for address and data The pins from 1 to 8 are for port 1 (P1.0 to P1.7); and the pins from 21 to 28 are for port 2. (P2.0 to P2.7) is used for sending address to memory. The 9th pin RST is used for Restarting the 8051 microcontroller. The pins from 10 to 17 are for port3 which is a special port, and it is multi-functional. The 30th pin is ALE(Address latch enable). When the pin is set to 1, then address is transferred through the pins AD0 to AD7. When the pin is set to 0 then data is transferred through the pins AD0 to AD7. The 29th pin is PSEN which is the Program Strobe Enable used for reading the program from external memory.
1.1 Crystal Oscillator
Figure 1.2 XTAL connection to an external clock source
The 8051 has an on-chip oscillator but requires an external clock to run it. Most often a quartz crystal oscillator is connected to inputs XTAL (pin 19) and XTAL(pin 18). The quartz crystal oscillator connected to inputs XTAL1 and XTAL2 also needs two capacitors of 30 pF value. One side of each capacitor is connected to the ground as shown in Figure 1.2.
It must be noted that there are various speeds of the 8051 family. Speed refers to the maximum oscillator frequency which is connected to XTAL. For example, a 12-MHz chip must be connected to a crystal with 12 MHz frequency or less. Likewise, a 20 MHz microcontroller requires a crystal frequency of no more than 20 MHz. When the 8051 is connected to a crystal oscillator and is powered up, the frequency on the XTAL2 pin is connected using the oscilloscope.
1.2 RST
Pin 9 is the Reset pin. It is an input and is active high. When the pin is applied to high, the microcontroller will reset and terminate.
1.3 EA
The 8051 family members will come with on-chip ROM for storing the programs. In such cases, the EA pin is connected to VCC. For the family members like 8031 and 8032, there is no on-chip ROM, hence the code is stored in external ROM and is fetched by 8031/8032. EA pin is connected to GND to indicate that the code is stored externally. EA stands for ” External Access” which is pin 31 in the DIP packages. It is an input pin and is connected to either Vcc or ground. 8031 uses this pin along with PSEN to access programs stored in ROM memory located outside the 8031. The next two pins are used mainly in 8031-based systems.
1.4 PSEN
This is an output pin. PSEN stands for ”Program store enable”. The external ROM holds the program code, this pin is connected to the OE pin of the ROM.
1.5 ALE
It is an output pin and is active high. When connecting an 8031 to external memory, port 0 provides both address and data. The ALE pin is used for demultiplexing the address and data by connecting to the G pin of the 74LS373 chip.
1.6 Ports 0,1,2 and 3
All the four ports in 8051 are bidirectional. Each consists of a latch an output driver, and an input buffer. P0,P2,and P3 will perform special function other than I/O. As shown in the figure below, there are four ports namely P0,P1,P2 and P3, each uses 8 pins, making them 8-bit ports. All the ports upon reset are configured as input, since P0-P3 have FFH on them.
The output drivers of Ports 0 and 2, and input buffers of Port 0, are used in accesses to external memory. In this application, Port 0 outputs the low byte of the external memory address, time-multiplexed with the byte being written or read. Port 2 outputs the high byte of the external memory address when the address is 16 bits wide.
As shown in the figure 1.3, port 0 is also designated as AD0-AD7, allowing it to be used for both address and data. When connecting 8051 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and data through port 0 to save pins. ALE indicates if P0 has address or data. When ALE=0, it provides data D0-D7, but when ALE=1 it has address A0-A7. Therefore ALE is used for demultiplexing the address and data. When there is no external memory connection, the pins of P0 must be connected externally to a ten K-ohm pull-up resistor. This is due to the fact that PO is an open-drain, unlike P1,P2 and P3. Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. In contrast to Port 0, P1,P2 and P3 do not need any pull-up resistors since they have pull-up resistors internally. Upon reset, ports P1,P2 and P3 are configured as input ports.
1.6.1 P1 and P2
In 8051 based systems with no external memory connection, both P1 and P2 are used as simple I/O. However in 8031/8051 based systems with external memory connection, port 2 must be used along with P0 to provide the 16-bit address for the external memory. Port 2 is designated as A8-A15, indicating its dual function. When 8051 is connected to external memory, P2 is used for the upper 8 bits of the 16-bit address and it cannot be used for I/O.
1.6.2 Port 3
It occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. Ports P1,P2 and P3 do not need any pull-up resistors. Port 3 has the additional function of providing some extremely important signals such as interrupts.
P3.0 and P3.1 are used for the RxD and TxD serial communications signals. Hence 8051 supports parallel as well as serial communications. Bits P3.2 and P3.3 are set aside for external interrupts. Bits P3.4 and P3.5 are used for Timers 0 and 1. P3.6 and P3.7 are used to provide the WR and RD signals of external memory connections, while the rest of the pins in port3 are normally used in the alternate function role.
Ports | Function | Pin number |
P3.0 | RxD(serial input port) | 10 |
P3.1 | TxD(serial output port) | 11 |
P3.2 | INT0 (external Interrput) | 12 |
P3.3 | INT1(external Interrput) | 13 |
P3.4 | T0(Timer/ Counter 0 external Input) | 14 |
P3.5 | T1(Timer/ Counter 1 external Input) | 15 |
P3.6 | WR ( External data memory write strobe) | 16 |
P3.7 | RD( External data memory read strobe) | 17 |
Table 1.1 Port 3 Alternate functions
2.Configuring ports
When ‘0’ is written to the port, it is configured as output. To reconfigure the port as input, ‘1’ must be written to port. Port0 is configured as input by writing ‘1’ to port and then it can be used as input.
Ex: MOV A,#0FFH ; A=FF hex
MOV P0,A ; write ‘1’ to port
MOV A,P0 ; A=P0
MOV P2,A ; P2=A
Initially Port0 is configured as input port. First, using the immediate addressing mode the hexadecimal number FFH is moved to accumulator and all the port pins in P0 is written to 1. This statement is used to declare that the port P0 is used as the input port. Port P0 is connected to some other input device. The data received by the port P0 is sent to accumulator and then finally Port 2 is the output port in which the data stored in accumulator is delivered to port2.
Another function of Port 0 is AD0-AD7. It is used for both address and data. When connecting an 8051/31 to an external memory, port 0 provides both address and data. Port 1 is used as input and output. Upon reset, port 1 is configured as an input port. Port 2 is also used as input or output. Upon reset, port 2 is configured as an input port. Port 2 along with P0 will provide the 16- bit address for the external memory. If 16 bit is needed for accessing the external memory then AD0-AD7 pins from Port 0 and A8-A15 pins from port 2 are combined in which port 2 contains the higher order byte and port 0 contains the lower order byte.
Instructions used in single bit operation are JB and JNB. These instructions monitor a bit and make a decision depending on whether it’s 0 or 1. These two instructions can be used for any bits of I/O ports 0, 1, 2, and 3. Table 1.2 lists the single bit operating instructions. Table 1.2 lists the single bit operations.
2.1 Access 1 or 2 bits
In order to access only 1 or 2 bits of the port the following commands can be used.
CPL P0.4 ;complement P0.4
SETB P1.4 ;set only P1.4
CLR P2.2 ; clear P2.2
SETB P1.0 ; set bit 0 of port 1 as high
The first statement CPL complements the P0.4 bit. If P0.4 bit is 1, then it is converted to 0 and if it is 0, then converted to 1. In the above example SETB functions are used for setting the corresponding bits P1.0 and P1.4 to 1. CLR command will clear the particular bit.
2.2 Configuring port pin to generate square wave
Following assembly code helps to generate a square wave at pin P1.3
LOOP: SETB P1.3 ; p1.3=1
LCALL DELAY ;delay subroutine
CLR P1.3 ;p1.3=0
LCALL DELAY
SJMP LOOP
For generating the square wave, the port is set to 1 for some period of time and then set to zero for the same time. Then a square wave can be generated. If the delay is changed, then the square wave becomes rectangular. Initially the port P1.3 is set to 1 using the SETB operation. LCALL is the long call which is the delay subroutine call, the pin P1.3 becomes 1 for certain period of time and then that pin is cleared using the CLR command. This pin maintains zero state for the same time and then using the control statement short jump, the loop continues and the square wave is generated.
3. Sample Programs
3.1 A switch is connected to pin P1.7. Write a program to check the status of Switch and perform the following:
(a) If SW=0, send letter ‘N’ to P2
(b) If SW=1, send letter ‘S’ to P2
SETB P1.7 ;make P1.7 an input
AGAIN:JB P1.7,OVER ;jump if P1.7=1
MOV P2,#’N’ ;SW=0, issue ‘A’ to P2
SJMP AGAIN ;keep monitoring
OVER: MOV P2,#’S’ ;SW=1, issue ‘S’ to P2
SJMP AGAIN ;keep monitoring
Initially the pin P1.7 is set to 1 which indicates that this pin is considered as the input pin. Then the condition of JB becomes true hence it goes to the loop OVER, the byte ‘S’ is sent to the output port P2. Then using the SJMP command the AGAIN loop continues. If the switch is pressed it means the pin P1.7 becomes 1, then the condition fails. Hence the byte ‘N’ is sent to the port P2. Likewise the loop is executed continuously. If the switch is pressed the byte ‘S’ is sent to the output port P2, if it is not pressed then the byte ‘N’ is sent to the output port P2.
3.2 Write a program to perform the following:
(a) Keep monitoring switch (at) P0.1 until it becomes high
(b) When P0.1 becomes high, Light LEDs connected at port 2
(c) Send a low-to-high (L-to-H) pulse to P1.2 to sound buzzer
SETB P0.1 ;make P0.1 as input (switch)
MOV A,#FFH ;A=11111111
AGAIN: JNB P0.1,AGAIN ; get out when P0.1=1
MOV P2,A ;Light LEDS by sending 1s to P2
CLR P1.2 ;make P1.2 low
SETB P1.2 ;make 01.2 high – L-to-H to sound buzzer
SJMP AGAIN
Initially the bit P0.1 is set as input port. Then the hexadecimal value FFH is stored in Accumulator. The jump condition fails and the Accumulator value is sent to port P2. Then the port P1.2 is cleared and it is set again for giving the low to high pulse to the port P1.2, then the alarm will ring. If the switch is pressed the LED’s connected to P2 will glow and the alarm will start ringing.
4. Summary
In this lecture pin diagram of 8051 controller is discussed. Introduction to pins and the ports of 8051 controller has been given. Some assembly programs are also written for handling pins and ports of controller.
5. References
1.The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.
2. http://what-when-how.com/8051-microcontroller/pin-description-of-the-8051/
Supporting & Reference Materials
- The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.
- http://what-when-how.com/8051-microcontroller/pin-description-of-the-8051/