Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Kai Klaas
02/18/18 05:26
Modified:
  02/18/18 05:29

Read: 287 times
Germany


 
#190851 - P1.2, input and output at the same time?
Responding to: Eric Lin's previous message
Hi Eric,

bit 2 of Port1 for driving a LED and using it as an input, at the same time? This is rather unusual, because normally you would have enough port pins and you would take two different port pins for that. Using the same port pin makes little sense, because you would short circuit the source when emitting "0" at the port pin. Only the reading of a switch would be possible.

First, Port1 cannot source enough current to drive a LED directly. The 80C51 (CMOS version) can only source about 60 microampere. Look into the datasheet of your microcontroller. So, usually a LED is driven by sinking the LED current. Connect a 330R resistor from Vcc (5V) to the anode of your LED and connect the cathode to the port pin P1.2. When you write "1" into the SFR of P1.2 the output emits "1" and the LED is off. If you write "0" in the SFR of P1.2 the LED goes on and about 10mA is flowing through the LED.

For reading a switch contact at P1.2 connect another 330R resistor with one pin to P1.2 and with the other pin to one of the switch contacts. Connect the other switch contact to GND of microcontroller. Now distinguish two cases:

1. P1.2 is emitting "1", because you wrote "1" into the SFR of P1.2. Then only a weak internal pull-up is present and you can easily pull down the potential at P1.2 with the switch. By reading the port pin P1.2 you can find out whether the switch is opened or closed. Don't be surprised that the LED is glowing whenever you press the switch, though.

2. When emitting "0" at P1.2, or by other words when the LED is on, you cannot read the switch, of course. But you can use a trick and periodically turn-off the LED for a brief moment. This is done by writing "1" to the SFR of P1.2. As consequence the internal NMOS-FET of P1.2 turns off and the weak pull-up turns on. If you now read P1.2 you will see "0" if the switch is pressed and "1" if the switch isn't pressed. Directly after the reading you write "0" again into the SFR of P1.2 to make the LED shine again.

There's a disadvantage with this method: Whenever you write "1" into the SFR of P1.2 a strong internal pullup is activated for two oscillator periods (read the links Erik gave you!). If your switch is closed at that moment a huge current will flow out of P1.2 into GND. For that reason the second 330R is added. It will limit this short circuit current to a safe level of less than 15mA. But even then this method can cause trouble: If your layout is routed improperly (lack of massive ground plane, etc.) or decoupling capacitors are omitted, your microcontroller can stop working correctly.

Kai

List of 4 messages in thread
TopicAuthorDate
How can I use standard8051 p1^2 to drive led and read input?      Eric Lin      11/08/17 23:54      
   need to study this      Erik Malund      11/09/17 09:00      
   Standard??      Andy Neil      12/05/17 01:54      
   P1.2, input and output at the same time?      Kai Klaas      02/18/18 05:26      

Back to Subject List