Prasad Pachu
Prasad Pachu

Reputation: 1

How to set a single particular pin as Input or Output in 8051?

I know if I give P0 = 0xFE; it ll make one pin as output and the rest as input.

But what if I want to make just one pin as output or input. Like how we do in PIC Microcontroller:

TRISABITS.TRISA0 = 0; // for setting A0 as output

Please clarify

Upvotes: 0

Views: 1557

Answers (2)

the busybee
the busybee

Reputation: 12600

Pins on the 8051 can't be switched into input or output. There is no direction control. Each pin has a strong driver to GND and a weak driver to VCC. At the rising edge (level change from 0 [GND] to 1 [VCC]) the pin will be driven to VCC a bit stronger for two clock cycles; that is for a better edge.

To use a pin as input you have to set it to 1 and leave it there. A driving source outside the 8051 can now drive the pin to GND, giving a 0 when read. If the external source drives the pin to VCC or let it float, the pin will be read as 1.

To use a pin as output you'll set it to 0 and 1 as you need. Make sure that the driven load has an impedance high enough.

Upvotes: 1

phuclv
phuclv

Reputation: 41764

Depending on which compiler you're using. On Keil C you can access the bits just like normal struct fields using the extension for bit-addressable registers

P0.1 = 1; // set P0.1 as input
P0.5 = 0; // set P0.5 as output

Otherwise set a single bit with bitwise operations

char inoutReg = P0;
inoutReg &= ~(1 << n); // clear bit n in P0, i.e. set P0.n as output
inoutReg |= 1 << m;    // set bit m in P0, i.e. set P0.m as input

See How do you set, clear, and toggle a single bit?

Upvotes: 0

Related Questions