Arduino HIGH LOW

2019-03-18 02:52发布

问题:

I have an Arduino and I am wondering exactly what HIGH and LOW mean as far as actual values go... Are they signed ints? Unsigned ints? Unsigned chars??? What are their values? I am guessing that HIGH and LOW are probably unsigned ints with all of the bits set to 1 and 0 respectively, but I'm not sure. I would like to be able to do bitwise operations using HIGH and LOW or pass values other than HIGH or LOW to digitalWrite. Also, how would I cast an integer to HIGH or LOW so I could do this?

回答1:

If you want to pass other values to digitalWrite() you can have a look at the function prototype

void digitalWrite(uint8_t, uint8_t);

So any integer value (well, 0 through 255) would work. No idea what the behavior of digitalWrite() could be if you passed it a value other than HIGH and LOW.

Since HIGH and LOW are simply defined constants, you can't cast an integer to them (nor would that operation make sense). It appears that you could use an integer anywhere that HIGH and LOW was expected.

Actually doing this is a bad idea though, for lots of reasons - the definitions of HIGH and LOW could change (unlikely but possible) and it doesn't make sense from a type perspective. Instead, you should use logic in your program to determine whether HIGH or LOW should be passed to the function call, and then actually pass the constant.



回答2:

Have a look at hardware/arduino/cores/arduino/Arduino.h (at least in Arduino 1.0.1 software), lines 18 and 19:

 #define HIGH 0x1
 #define LOW  0x0

Meaning, these defines being hexadecimal integer values, you can do whatever bitwise operations you want with them - how much sense that will make, however, is not really clear to me at the moment. Also bear in mind that these values might be subject to change at a later time - which would make bitwise operations on them even more unwise.



回答3:

To add my 2c to codeling's answer. Lines 18--25 of Arduino.h (1.0) are:

#define HIGH 0x1
#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1

#define true 0x1
#define false 0x0

Therefore, HIGH<==>OUTPUT<==>true<==>0x1 and LOW<==>INPUT<==>false<==>0x0. Then, HIGH<==>!LOW and LOW<==>!HIGH...



回答4:

The first argument to digitalWrite() is a pin number.

The second argument to digitalWrite() will either:

1) write a HIGH (3.3 or 5v) or LOW (0v) to a BINARY OUTPUT or
2) enable (HIGH) or disable (LOW) the internal pullup on a BINARY INPUT.

Bitwise operations for either argument make no sense. Perhaps you need to use analogWite()?

See the documentation: digitalWrite() Constants