Question

I am learning 8051 assembly programming. I read in my book "In order make a port as input port, the port must be programmed by writing 1 to all its bits." Isn't this wrong, though?

If I set all the bits to one, and want to input 1010 at input, I will press button attached to 2nd pin and 4th pin to input 1010, but port will read all 1's. I have tried it on Keil uVision Simulation and found this problem.

Is this a problem or I am misunderstanding it?

org 00h

Reset:

mov P0,#00h
mov P1,#00h
mov P2,#00h
mov P3,#00h

TakeInput:

jb P2.0,Addition// in assembler simulation, this condition is true if I set P2 to #0ffh, which is not required. The user should set this bit for making this condition true

sjmp TakeInput


Addition:

mov A,P0 add A,P1

mov P3,A

acall Delay

ajmp Reset


Delay:

mov R0,#1

here:

mov R1,#1

again:

djnz R1,again

djnz R0,here

ret

end

If I don't reset all the ports to 0, so the jump instructions which depend on a port value by default become true.

Was it helpful?

Solution

It's the correct way, there must be something wrong with your simulation. The 8051 pins have this kind of circuit:

pin circuit

(source)

Note the pullup to Vcc is optional, check the datasheet of your particular chip.

This circuit works such that if you set a pin to 0, the output will be pulled to ground and can only be read as 0 because externally you can't pull it high. If you want to allow both levels to be input, you have to set the pin to 1. In this case you can:

  1. leave output floating if you have internal pullups (reads as 1)
  2. pull output to Vcc (reads as 1)
  3. pull output to ground (reads as 0)

It follows that if you connect a button you should wire it such that it pulls the pin to ground. Add an external pullup resistor if required.

Update to make this point clear: if you wire the button as directed above (ie. pushed button grounds the pin) the input will be read as 1 if button is not pressed, and as 0 if button is pressed. This is the usual way to connect buttons.

Illustration using MCU 8051 IDE. Notice two switches are connected to port 1 pins 7 and 6 (ie. the two top bits), one is open the other is closed (pushed).


If the port bits are set to zero, input will be zero no matter the state of the switches: port bits set to 0


If the port bits are set to one, input will be 1 for an open and 0 for a closed switch: port bits set to 1

The red wiring leading to the leftmost switch indicates that P1.7 is at Vcc (because output is 1 and it is not pulled down by the switch).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top