0
\$\begingroup\$

While studying I ended up finding this question.

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

Circuit

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).

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Dave Tweed Mar 22 at 22:53
  • \$\begingroup\$ What about the var = ~var; statement? \$\endgroup\$ – Dave Tweed Mar 22 at 23:47
  • \$\begingroup\$ Everything will be set to 0, and in the final the only bit set to 1 is P1_0. \$\endgroup\$ – Ivan Mar 23 at 15:25
0
\$\begingroup\$

Keep in mind that "open-drain" means that if you write a 0 to a pin, it will be forced to zero, but if you write a 1, then the pin will take on whatever state is determined by the external connection. The combination of the two for each pin is what you get when you read the port.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much. I was looking for the internal circuit of the open-drain configuration and it make sense now. \$\endgroup\$ – Ivan Mar 23 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.