# 8051 Open Drain Operation

While studying I ended up finding this question.

Write the value (in hex) of the P1 after the execution of the code below.

unsigned char var;
P1 = 0xff;
var = P1;
var = ~var;
P1 = var;
P1_0 = 1;
P1_4 = 0;


Consider P1 as Open Drain with Weak Pullups.

I tried to answer the question, but I'm not sure of my answer. Can anyone help me?

My approach to solve the question.

First I analyzed the code and I saw that the P1 output would be 0x01.

And then I analyzed the hardware.

P1.0 is High. P1.1 is Low. P1.2 is High. P1.3 is High. P1.4 is Low P1.5 is High P1.6 is Low P1.7 is Low.

It would lead to a output of 0x2D (0b0010 1101).

My doubt is.

In the code P1.0 is set to 1, and in the hardware is connect to 1 (High). I believe that P1.0 is going to be 1. Therefore the answer is 0x2D.

But what if we have something like, by code PX is set to 1 but it's hard-wired to GND. I learned that open-drain port configuration work as a di-directional port, working as an output and an input, but I didn't get what happens when he have a mismatch (trying to set a ouput, where would be a input).

• Welcome to EE.SE! This appears to be a homework question. As such, you need to show us your work so far, and explain which part of the question you're having trouble with. For future reference: Homework questions on EE.SE enjoy/suffer a special treatment. We don't provide complete answers, we only provide hints or Socratic questions, and only when you have demonstrated sufficient effort of your own. Otherwise, we would be doing you a disservice, and getting swamped by homework questions at the same time. See also here. – Dave Tweed Mar 22 at 22:53
• What about the var = ~var; statement? – Dave Tweed Mar 22 at 23:47
• Everything will be set to 0, and in the final the only bit set to 1 is P1_0. – Ivan Mar 23 at 15:25