Input-Output instructions for 8051 in Assembly language

This tutorial is in continuation with previous tutorials on 8051 Assembly Programming. Now we will learn how to give input and output instructions to 8051 microcontroller(s) using assembly programming language.

The microcontroller has total forty pins in all. Most of the pins of 8051 microcontroller have more than one function. The first function is the input /output operation and the second function can be some special function like they can be used as counters or for serial communication. 

In microcontroller there are four ports (collection of pins) P0, P1, P2 &P3. These ports have 8 pins each as shown. As we will see later on we will be able to address the whole port during programming.

Whenever we are talking about 8051 we are talking of the family of microcontrollers having the architecture of 8051. They are all same except for some additional features, pins distribution and packaging.

PIN DESCRIPTION OF 8051

 

Pin Description of 8051

 

 

8051 ports -P0, P1, P2 and P3

 

VCC and GND: - The pin 40 is Vcc i.e. it is given 5V and pin 20 is GND i.e. it is given 0V (supplied from power source) for powering up the microcontroller.

CONNECTING THE CRYSTAL (XTAL1- XTAL2): The pin numbers 18 - 19 are used to connect the crystal. The frequency of this crystal determines the machine cycle. With 8051 family we can use a crystal having frequency from 3 to 24 MHz.  

 

Connecting crystals in 8051

 

RESET PIN CONNECTION (RST): The reset pin i.e. pin number 9 is used to reset the program just like we restart a computer, the program starts executing from the very beginning. When reset is used the program counter is set back to zero and the values in all other registers are lost. 

 

8051 reset pin

 

EA/VPP: The EA pin no. 31 is known as external access. The 8051 microcontrollers have on chip ROM so the EA/VPP is set to one (or Vcc). In case there is no on- chip ROM, like in case of 8031 we set this pin equal to zero. This pin cannot be left unconnected. 

PSEN: This is an output pin. PSEN stands for “program store enable”. In 8051 we leave this pin unconnected until otherwise mentioned.

ALE/PROG: ALE refers to address latch enable. In 8051 we leave this pin unconnected until otherwise mentioned. 

 

8051 basic circuit diagram 

 

This is the basic setup of microcontroller that you need to have in order to perform an 8051 activity. The pins ALE/ PROG, EA/ VPP and PSEN may have some modifications. We will discuss this wherever it is necessary. 

 

8051 module

 

This is a module that has been prepared by us. You can see that a 40 pin base has been mounted on the PCB (printed circuit board) instead of directly soldering the microcontroller to PCB which maybe damaged while soldering.

 

AT89S52 Microcontroller

 

Here a microcontroller AT89S52 has been shown which is a member of 8051 family. The above circuit is valid for all the members of 8051 family except the AT89S51. In AT89S51, we also have to add pull up resistor at port zero for Input/ Output. (Check the data sheet of AT89S51 microcontroller for the same)

 

I/O Port Programming in 8051 (using Assembly Language)

So far we have discussed the basic setup required for initializing the microcontroller and now comes the interesting part i.e. how to program 8051 using assembly language i.e. carry out input/output operations.

All the ports of 8051 can be used for Input or Output. Let’s get started. Some illustrations are listed below showing how to access the different pins and ports.

In the following code, we have programmed the microcontroller to toggle the output pins of port 1and hence it will continuously toggle. 

ORG 0H

MOV     A, #55H ; A= 55 hex

BACK: MOV     P1, A ; P1= A

ACALL    DELAY ;Wait “say for 256 counter”  

CPL         A ; complement A

SJMP     BACK ; keep doing it

DELAY: ;delay subroutine for 256 counts

MOV R1,#0FFH ;load FF hex in R1  “counter”

AGAIN: DJNZ R1, AGAIN ; a delay loop

RET ;return to caller

END

Now suppose we wish to receive data on Port 0 and send it to Port 2. In other words the input on Port 0 can be directed as an output to port 2.

 Remember for receiving input the input pins or port must be initialized to logic HIGH i.e. 1 and in the subsequent example complete port has been used for receiving input. So rather than assigning logic high to each pin, FF hex value i.e. binary 11111111 is loaded to the input port. 

ORG 0H

MOV       A, #0FFH ; A= FF hex or binary 11111111

               MOV       P0,A ; make P0 an input port by initializing logic high to all 8 pins

BACK: MOV       A, P0 ; get data from P0

MOV      P1,A ; send it to port1

SJMP BACK ; keep doing it

END

 

 

 

Let’s see one more example. In the following code, Port 1 act as input and its complement is delivered as an output to Port 3.

 

ORG  0H

MOV       A, #0FFH ; A= FF hex

               MOV       A, P1 ; make P1 an input port by writing all one’s to it. 

BACK: MOV       A, P1 ; get data from P1

CPL         A ; complement A

MOV      P3, A ; send it to port3

SJMP BACK ; keep doing it

END

Checking an Input bit

Sometimes we may only need to observe a single bit and perform an action based upon it like: receiving an input of temperature alarm and if the signal goes high (i.e. temperature is above a certain limit) the buzzer goes off.

Or receiving an input from IR sensor of a line follower robot and instructing it to switch motors (on/off) to turn or move straight. 

In the following code we will receive an input from P1.5 and if it is high then an output 55H is sent to port 0. 

ORG 0H

 SETB   P1.5 ; make P1.5 an input 

 MOV A, #55H

AGAIN:  JNB   P1.5, AGAIN ; get out when P1.5 = 0

 MOV P0, A ; issue A to P0

 END

 

Now you are ready for the real thing. We will start with a simple activity to give you a feel and also you can use this tutorial to implement your own ideas.

Suggestion: You should make the basic circuit on a PCB because it will save you a lot of time as you will always need this setup.

 

8051 blinking LEDs 

 

ORG 0H ; start at origin

BACK: SETB P3.1 ; switch on the LED at P3.1

CLR P3.3 ; switch off the LED at P3.3

LCALL DELAY ; to call delay subroutine

SETB P3.3 ; switch on the LED at P3.3

CLR P3.1 ; switch off the LED at P3.1

LCALL DELAY

SJMP BACK

; _______ this is a delay subroutine

DELAY:

MOV R1, # 255H ; load the counter with 255 hex

AGAIN: DJNZ R1, AGAIN ; remain in loop until counter become zero

RET ; return to the caller

END

 

Blinking LEDs using 8051 circuit diagram 

 

After you have written a program you have to burn/program it to a µC (symbol for microcontrollers).

 For this you will need a typical parallel programmer or an ISP (In system programmer). The ISP is generally cheaper and you don’t need to remove the µController from the circuit to burn it. The burners/programmers will cost you around Rs 1500-2500. Do try to buy a USB burner/programmer as parallel ports are no more there in PCs & laptops these days. 

You can also do a simple activity to understand how you can receive inputs and give outputs from a microcontroller.

Here we have used a simple switch, which will give a pin an input as low/high and corresponding to this input, an output will be generated at other pin to light an LED. 

 

8051 input and ouput simulation

 

ORG 0H ; start at origin

SETB P1.4 ; make P1.4 an input (initialization )

BACK: JB P1.4, LED ON ; jump if byte = 1

CLR P0.5 ; switch off the LED P0.5 

SJMP BACK ; keep doing it

LED ON: SETB P0.5 ; turn on the LED if switch is pressed

SJMP BACK ; keep doing it

END

 

8051 input and output simulation complete circuit diagram 

 

 

 

Tags:

Comments

Member since:
12 April 2009
Last activity:
4 years 15 weeks

i have highlighted where you need to add the line.. check this out..
 

ORG 0H ; start at origin

SETB  P1.4 ; make P1.4 an input (initialization )

BACK: JB  P1.4, LED ON ; jump if byte = 1

CLR  P0.5 ; switch off the LED P0.5 

SJMP  BACK ; keep doing it

 

LED ON: SETB  P0.5 ; turn on the LED if switch is pressed

;-----------------------------------------------------------

;Add this line here as shown below

SETB         P0.2      ;sound beeping
                 SJMP  BACK ; keep doing it

 

END 

Member since:
22 October 2009
Last activity:
8 years 1 day

Given this code from the example:

 

ORG 0H ; start at origin

SETB P1.4 ; make P1.4 an input (initialization )

BACK: JB P1.4, LED ON ; jump if byte = 1

CLR P0.5 ; switch off the LED P0.5 

SJMP BACK ; keep doing it

 

LED ON: SETB P0.5 ; turn on the LED if switch is pressed

SJMP BACK ; keep doing it

END

 

suppose in the LED ON mark and after the line SETB P0.5, i want a sound to beep i. e

 

LED ON: SETB P0.5 ; turn on the LED if switch is pressed

               //i want a beaping sound here

SJMP BACK ; keep doing it

END

 

how do i do that with the 8051 assembly language assuming the sound system is attaached to p0.2?

live is lived forward but understood backward

Member since:
19 August 2009
Last activity:
7 years 41 weeks

a great tuts thank u

Member since:
10 August 2009
Last activity:
8 years 14 weeks

 

I need help in coding for Printing a Pattern using emulator8086 implemented in assembly language. Pattern is like this:

 

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Member since:
24 March 2009
Last activity:
8 years 14 weeks

If you don't use a switch, you will not be a able to reset...

When you press the reset switch it make the program start from begning...

In that case you will have to switch off and on the power again to restart

Member since:
15 March 2009
Last activity:
8 years 14 weeks

In some books i came across with a reset circuitry with no switch connected.. just a capacitor and resistor is there.. can anyone plz explain how this works.. 

Member since:
15 March 2009
Last activity:
8 years 14 weeks

Again the limiting resistence should be between LED and contoller not between LED and ground.. please correct it... 

Member since:
15 March 2009
Last activity:
8 years 14 weeks

Ya i got it.. its active high... in AVR reset is active Low... 

Member since:
15 March 2009
Last activity:
8 years 14 weeks

in 8051 reset is active low or active high... 

Over all tutorial is excellent ! everything has been clearly explained
Just a little correction. The symbol of LED is little incorrect.(ie without a bar >|) and GND should be with three bars.. thats it .