# How can I detect HIGH or LOW input on a NXP microcontroller?

I am using NXP micro-controller P89V51RD2. I need to detect HIGH or LOW from 2 single bits. How do I store input state to a pre-defined address so that my functions can detect it as data?

I need to use 2 transistors coupled emitter to base to detect change in resistance(indirectly by measuring current and voltage) in a load cell. The coupling is an attempt for a rudimentary type of switching. This I need to give to a bit in e.g. port P1 for detection. I have trouble getting the right code ( H/L bit identification) for this AND connection configuration (buffer or another signal conditioner needed?) for this.

The code compiling is done in Keil μVision v4.

The official website for Keil says it can be done by code : sbit b1 = P1^0. However, I don't know how to proceed afterwards as this code doesn't give required results.

-

## migrated from programmers.stackexchange.comNov 22 '12 at 11:58

This question came from our site for professional programmers interested in conceptual questions about software development.

Please clarify the electronic design aspect of this question... Or is the question "What lines of code do I use to save or use a boolean value in NXP's assembly language?" Overall the question needs its vagueness eliminated. – Anindo Ghosh Nov 22 '12 at 12:11
Its more like scanning for signal to change output. – VedVals Nov 22 '12 at 13:36
b1 = P1^0 equals b1. It doesn't make sense to XOR anything with 0. – Lundin Nov 23 '12 at 7:51
b1 = P1^0 " ^ " assigns the bit 0 (LSB) of P1 to a variable b1. – VedVals Nov 23 '12 at 8:22
No it doesn't. Your question is tagged C. To assign bit 0 of P1 to b1, you would write b1 = P1 & 1; – Lundin Nov 23 '12 at 10:48

I looked at the guide you linked to, and your problem is that you have confused a declaration with a program statement. When you write sbit b1 = P1^0;, you have told the compiler how to get the bit you want, but you haven't created any code yet. The way you would use it, then, is to treat this declared "b1" as if it were a variable representing the port bit you wanted. So,

if(b1) {
led_on();
}
else {
led_off();
}


When the compiler encounters b1, it will go read the port and extract the bit, based on the earlier declaration.

That particular way of declaring port bits is particular to that compiler, so it confuses anyone who hasn't seen it before (me included).

-
Got it. But as you said when the compiler encounters b1, it will go read the port and extract the bit, based on the earlier declaration. I want to store it in a permanent variable (as, say, unsigned char bit1). – VedVals Nov 25 '12 at 7:29
bit1 = b1; will do that for you. That initial declaration causes b1 to act like a variable already, so you're free to copy it anywhere. But then, bit1 only has the value from the last time you copied it. Maybe that's what you want. – gbarry Nov 27 '12 at 1:52

Your code is not correct. b1 = P1^0 means P1 XOR 0. Anything XOR 0 gives the original value.

To assign bit 0 to b1, you need to write b1 = P1 & 1, or preferably:

b1 = P1 & (1<<0)

The latter form is universal, to get bit 1 you'd write 1<<1. For bit 2, you'd write 1<<2, and so on.

-
Cx51 User's Guide - The official help site says my code is correct. I was also successful in manipulating bits to turn LEDs on the learner kit on and off. I should have mentioned this in the question that as per above site, port P1 is a sfr - special function register. – VedVals Nov 24 '12 at 14:59
@ved2254 The C standard ISO 9899:2011 6.5.11: "Bitwise exclusive OR operator. Syntax: exclusive-OR-expression ^ AND-expression". You should get a C compiler instead of whatever non-C compiler you are currently using. I'm certain there must GCC ports available for 8051-ish MCUs. – Lundin Nov 26 '12 at 7:32
In addition, it seems very suspicious that Keil would decide to remove the XOR operator when they re-made C to the Keil language. XOR is quite an important operator in embedded programming: bit toggling, CRC calculations, encryption and so on. – Lundin Nov 26 '12 at 7:46
Keil hasn't removed XOR. This type of declaration is valid only on global declaration of sbits. If used inside any function, it gives an error. – VedVals Nov 26 '12 at 16:45