|
Interfacing
to 8051 I/O ports.
SWITCH ON I/O PORTS

Good Circuit
It is always best connecting the switch to ground with a pull-up
resistor as shown in the "Good" circuit. When the switch is open, the 10k
resistor supplies very small current needed for logic 1. When it is
closed, the port pin is short to ground. The voltage is 0V and all the
sinking current requirement is met, so it is logic 0. The 10k resistor
will pass 0.5 mA (5 Volt/10k ohm). Thus the circuits waste very little
current in either state. The drawback is that the closure of switch gives
logic 0 and people like to think of a switch closure gives logic 1. But
this is not a matter because it is easy to handle
in software.
Fair circuit
The "Fair" circuit requires that the pull-down resistor be very
small. Otherwise, the pin will rise above 0.9V when the resistor passes
the 1.6mA sinking current. When the switch is closed, the circuit waste a
large current since virtually no current flows into the pin. The only
advantage is that a switch closure gives logic 1.
Poor circuit
In the "Poor" circuit, the logic 1 is stable when the switch is
closed. But when the switch is open, the input floats to a noise-sensitive
high rather than a low. An open TTL pin is usually read as logic 1 but the
pin may picks up noise like an antenna.
To conclude, driving a TTL input should always consider current
sinking (pulling input to 0V).
LED ON I/O PORTS

Since TTL outputs is designed to feed
multiple TTL inputs, they are good at current sinking but poor at current
sourcing. The Standard TTL can sink up to 16mA and source 250uA. The LS
logic family can sink 8mA and source 100uA. The 8051 port pin can sink
1.6mA (3.2mA for port 0) and source 60uA. Therefore, if you drive
significant current, try to arrange your circuits to use current
sinking.
Unlike diodes, Light-emitting diodes have a
forward voltage drop from 1.7 to 2.5 volts and most of them flow a forward
current 20mA.
Poor circuit
since the TTL output can't source above 1mA so the LED will
be very dim.
Fair circuit
The LED will conduct heavily at about 2V and the extra 3V has to be
dropped in the TTL circuitry. This causes high power dissipation in the
TTL or the LED fails.
Good circuit
The resistor limits the current. The resistance can be calculated
by assuming its voltage is about 2.5V and the TTL output is 0.9V. For 2.2V
LED, 1.9V is across the resistor so the 220ohm would limit the current to
8.6mA (1.9/220). For 1.7V LED, 2.4V is across the resistor so it would
limit the current to 10.9mA (2.4/220). The resistor should not less than
100ohm or the LED would fail.
CODE EXAMPLE
Connection -Switch -P1.0 , LED - P2.0
Condition - Turn on LED when switch is pressed.
|
ASSEMBLY
LANGUAGE |
|
C
LANGUAGE
(SPJ ) |
|
|
SETB P1.0 ;
input pin.
LOOP:
JB P2.0, LOOP
; not grounded then stay in loop
CLR
P0.0 ;To clear pin P0.0 when P1.0 is at 0 v
|
|
|
BIT button p1.0 / * Using BIT keyword for p1.0
definition*/ BIT LED p2.0 void main ( ) { while (1)
{ LED = button ; /* Note LED=button is wrong
*/ } } |
RELAY ON I/O PORT (2CO
Relay)

In A, NPN transistor (say a BC337 or
BC338) is being used to control a relay with a 5 V coil. Series base
resistor R1 is used to set the base current for Q1, so that the transistor
is driven into saturation (fully turned on) when the relay is to be
energized. That way, the transistor will have minimal voltage drop, and
hence dissipate very little power as well as delivering most of the 5V to
the relay coil.
How do
work out the value of R1?.
Let us say RLY1
needs 50mA of coil current to pull in and hold reliably, and has a
resistance of 24 Ohms so it draws this current from 5V. Our BC337/338
transistor will need enough base current to make sure it remains saturated
at this collector current level. To work this out, we simply make sure
that the base current is greater than this collector current divided by
the transistors minimum DC current gain hFE. So as the BC337/338 has a
minimum hFE of 100 (at 100mA), we'll need to provide it with at least
50mA/100 = 0.5mA of base current.
In practice,
you give it roughly double this value, say 1mA of base current, just to
make sure it does saturate. So if your resistance will be
TTL Logic High Voltage
(Min) /1ma ( 1K approx)
EXAMPLE

Connection -Port 0 is connected to
eight LEDs, each of them is connected to 5V through a 330ohm resistor.
Port 1 is connected to a DIP switch and a 10Kohm resistor
Condition - Corresponding led should
light up when switch pressed , i.e. if Switch at 1.0 is pressed ->
LED at P0.0 should light up.
CODE EXAMPLE
|
ASSEMBLY
LANGUAGE |
|
C
LANGUAGE
(SPJ SIDE51) |
|
|
LOOP:
mov p1,#0ffh ; To configure port for input.
mov a,p1
mov p0 ,a
sjmp LOOP ; Stay in infinite loop
|
|
.
|
1.
void main() {
while (1) { P0 = P1;
/* Note P1=P0 will not work } }
2.
voided main() {
char port_value; while (1)
{ port_value =
P1; P0 =
port_value;
} } |
USING ULN
Another option for driving relays
would be to use a high-voltage, high-current, Darlington array driver IC
such as the ULN2803. The ULN2803 can directly interface to the data
outputs of the 8051 pins, and provides much higher drive-current.
The ULN2803 also has internal diode protection that eliminates the need
for the fly-back diode as shown in the above relay driver schematics. You
can connect 8 relay using this IC.
So I think ULN is better choice if you have more than 3 relay. ( Simple
design of circuit & PCB as well ! )

|