Reading from input port pins of the 8051

Thread Starter

Joe24

Joined May 18, 2007
52
Hello,

I have a question that I would like someone to explain to me. I have been learning how to write code for the 8051 in C and assembly language. Now, when writing code in assembly, to read from a port pin we would write code such as:

MOV A,P1.6 ; READ PIN 6 OF PORT 1
; AND STORE IT IN ACCUMULATOR
OR

LOOP: JNB P1.6,LOOP ; STAY IN LOOP UNTIL THERE IS A HIGH ON
; PIN 6 OF PORT 1.


Now in C, it would look something like this, assuming we are using the Keil compiler and, we have defined:

sbit input_pin = p1^6;


So now to read this pin, we first have to write a 1 (logic HIGH) to it, then we are able to read this pin and use it in some kind of decision making, for example;

if ( input_pin == 0 )
{

// some code would go here

}



So my question is, why when programming in C we have to write a 1 to the port pin of interest, and in assembly language, it seems like we do not have to write to it before reading from it. I know that they are bi-directional, so it makes sense when writing in C that we have to write a HIGH to the pin of interest to "tell" it that it will be recieving input and not sending. But in assembly this is not the case. Is there something that I may have missed when reading my books on assembly language programming for the 8051? Please advise, thank you.
 

Papabravo

Joined Feb 24, 2006
21,228
Boy are you confused. First off you cannot read a port pin into the Accumulator. Single bits go to and from the carry flag. The carry flag is located in the PSW ( @ 0xD0 in the SFR space).

In your C example I see a relational comparison and no evidence of writing a one to the bit.

To clarify the essential point. In the 8051 architecture Ports 1, 2, and 3 have what are called "quasi bi-directional ports". They do not have a data direction register which defines which pins are input an which pins are outputs. The output structure is an n-channel FET which can pull low with a reasonable sink current (3.2 mA), and a very weak p-channel pullup which can source maybe 60 uA. To make a pin an input you write a 1 to that bit - ONE TIME ONLY - at initialization. Any external driving source can take the pin high or low as it desires and you can read it millions of times without ever giving it another thought.

Port 0 has open collector outputs, and that is a story for another post. You still need to write ones the Port 0 bits which you want to use as inputs.
 

Thread Starter

Joe24

Joined May 18, 2007
52
Boy are you confused. First off you cannot read a port pin into the Accumulator. Single bits go to and from the carry flag. The carry flag is located in the PSW ( @ 0xD0 in the SFR space).

In your C example I see a relational comparison and no evidence of writing a one to the bit.

To clarify the essential point. In the 8051 architecture Ports 1, 2, and 3 have what are called "quasi bi-directional ports". They do not have a data direction register which defines which pins are input an which pins are outputs. The output structure is an n-channel FET which can pull low with a reasonable sink current (3.2 mA), and a very weak p-channel pullup which can source maybe 60 uA. To make a pin an input you write a 1 to that bit - ONE TIME ONLY - at initialization. Any external driveing source can take the pin high or low as it desires and you can read it millions of time without ever giving it another thought.

Port 0 has open collector outputs, and that is a story for another post. You still need to write ones the Port 0 bits which you want to use as inputs.
Ahhh, yes you are right papabravo, It was my mistake.... I know that you cannot write a single bit into the accumulator, it was a momentary brain freeze. The carry flag is what I really meant. Now, in the C code, I did not explicitly write the code that writes a 1 to the port pin of interest here, but I mentioned it before hand that we would write a 1 before reading the port pin.

As far as your explanation about the actual hardware configuration for the 8051 ports, that is all nice to know, but still does not answer my original question.
 

Papabravo

Joined Feb 24, 2006
21,228
I disagree. I answered the question. It matters not to the processor which language you write in. To use a pin as an input, you write a 1 - ONE TIME ONLY - at initialization. Thereafter you can read it as many times as you want in either assembly language or in C. You may consider my previous answer "interesting", but unless you understand it you'll keep asking questions like this and wondering why your programs don't behave like you expect.
 

Thread Starter

Joe24

Joined May 18, 2007
52
I understand what you explained in your earlier post, but you see what I am confused about is that I have two seperate books. One that is programming the 8051 in C, and the other is a general 8051 introductory book that most of its examples are in assembly. Now I read the C book first, in that book the author stressed the fact that we need to write a 1 to port pins that we will use as inputs. Now in the assembly book, the author makes no mention of that fact, nor do I see any code in the example programs that writes a 1 to pins that are being used as inputs.

So if we MUST write a 1 to port pins that will be used as inputs even once and only once in BOTH C and assembly language, then I would assume the author has done a bad job in really emphasizing this point.
 

Papabravo

Joined Feb 24, 2006
21,228
I understand what you explained in your earlier post, but you see what I am confused about is that I have two seperate books. One that is programming the 8051 in C, and the other is a general 8051 introductory book that most of its examples are in assembly. Now I read the C book first, in that book the author stressed the fact that we need to write a 1 to port pins that we will use as inputs. Now in the assembly book, the author makes no mention of that fact, nor do I see any code in the example programs that writes a 1 to pins that are being used as inputs.

So if we MUST write a 1 to port pins that will be used as inputs even once and only once in BOTH C and assembly language, then I would assume the author has done a bad job in really emphasizing this point.
I would concur with your assesment. The ultimate authority on such matters is the semiconductor manufacturer, through datasheets, reference manuals, and application notes. I sympathize with your plight in recognizing and attempting to deal with this inconsistency between the two authors.

If I appeared judgemental, then I apologize; it was not my intention belittle your efforts to make sense of this material.
 

Thread Starter

Joe24

Joined May 18, 2007
52
It is alright, for I did not take it in that manner. I do want to thank you for enlightening me on this point. It is a very simple point, but nontheless extremely important.
 
Top