Arduino HIGH LOW

后端 未结 4 1099
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 23:38

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

相关标签:
4条回答
  • 2021-02-07 00:10

    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.

    0 讨论(0)
  • 2021-02-07 00:14

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

    The second argument to digitalWrite() will either:

    1. write a HIGH (3.3 or 5 V) or LOW (0 V) 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

    0 讨论(0)
  • 2021-02-07 00:25

    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.

    0 讨论(0)
  • 2021-02-07 00:27

    To add my two cents 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...

    0 讨论(0)
提交回复
热议问题