Keil™, An ARM® Company

Discussion Forum

Reading from a port

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Author
Aaron S
Posted
4-Oct-2008 11:41
Toolset
C51
New! Reading from a port

Hi,

I have just started learning embedded programming on my own. The book I am using (Embedded C Book by Micheal Pont) says:
"because of the underlying hardware, we can only read from a pin if the
corresponding latch contains a '1'."
This line is confusing me as I have been able to read values of the port without initializing it first.
This is my code:

#include <reg52.h>
sbit pin = P2 ^ 1;
void main(void)
{ P2 = 0; while(1) { pin ^= 1; }
}

Debugging shows that it is working just as anticipated. Can someone please explain what's going on here? I am using the Keil C51 simulator (device selected is 8052), could that be the problem?

Thanks in advance.

Aaron

Author
Hans-Bernhard Broeker
Posted
4-Oct-2008 12:13
Toolset
C51
New! RE: Reading from a port

This line is confusing me as I have been able to read values of the port without initializing it first.

You've been deluding yourself. Nothing in that program of yours reads the port pin at all. You only use write and read-modify-write instructions. The former doesn't read at all, the latter reads the latch, not the pin.

Author
erik malund
Posted
4-Oct-2008 13:22
Toolset
C51
New! look in the datasheet

for each SFR there will be a reset value listed

Erik

Author
Aaron S
Posted
5-Oct-2008 06:43
Toolset
C51
New! RE: Reading from a port

Thanks for the reply.

By further playing with the simulator, I have concluded following. Please correct me if I am wrong.

"In order to read the value of a port (pin), the corresponding latch (pin) should first be set.
This doesn't affect the value being read from the PORT itself as the initialization is performed before the value is read off it."

Thanks and sorry for bothering you again.

Author
erik malund
Posted
5-Oct-2008 15:29
Toolset
C51
New! RE: Reading from a port

http://www.8052.com/faqs.phtml?FAQ=119914
http://www.8052.com/faqs.phtml?FAQ=119915
http://www.8052.com/faqs.phtml?FAQ=119916
http://www.8052.com/faqs.phtml?FAQ=119917
http://www.8052.com/faqs.phtml?FAQ=120176
http://www.8052.com/faqs.phtml?FAQ=121619

Author
Aaron S
Posted
6-Oct-2008 09:09
Toolset
C51
New! RE: Reading from a port

Thanks a lot Erik.

Aaron.

Author
Neil Kurzman
Posted
6-Oct-2008 10:12
Toolset
C51
New! RE: Reading from a port

This is an 8052 specific statement. The 8052 has pseudo bi-direction port pins (on standard versions). if you want to read a pin you must have first written a '1' to it (the reset state) this is because the port has the equivalent of a weak pull-up and a strong pull down. so an external signal can overcome a a port with a 1, but not a zero (in fact trying to put a 1 into a pin that has a 0 written to it can damage the pin)

Next Thread | Thread List | Previous Thread Start a Thread | Settings