C++ Arduino uint8_t 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10457210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:05:11  来源:igfitidea点击:

Arduino uint8_t variables

c++arduino

提问by David Flanagan

I'm having trouble with some arduino code. im using an Ethernet tutorial code i found and some IR emitter and receiver code i found, and im trying to combine them.

我遇到了一些 arduino 代码的问题。我正在使用我发现的以太网教程代码和我发现的一些红外发射器和接收器代码,我试图将它们结合起来。

http://www.ladyada.net/learn/sensors/ir.html

http://www.ladyada.net/learn/sensors/ir.html

http://g33k.blogspot.com/2010/09/arduino-data-webserver-sample-web.html

http://g33k.blogspot.com/2010/09/arduino-data-webserver-sample-web.html

Both codes work fine by themselves.

这两个代码本身都可以正常工作。

The code compiles but when i call the following void IRDetector() , it doesn't work. I have debugged it and so far ive found when i use the variable uint8_t or uint16_t (ive tried replacing them with ints and longs). Do i have to import and libraries to use uint8_t ? Any thoughts?

代码编译但当我调用以下 void IRDetector() 时,它不起作用。我已经调试了它,到目前为止我发现当我使用变量 uint8_t 或 uint16_t 时(我尝试用整数和长整数替换它们)。我是否必须导入库才能使用 uint8_t ?有什么想法吗?

Any help would be appreciated.

任何帮助,将不胜感激。

 uint16_t pulses[100][2];  // pair is high and low pulse 
   uint8_t currentpulse = 0; // index for pulses we're storing

    uint8_t highpulse, lowpulse;  // temporary storage timing

      void IRDetectCode(void)
   {
    while(true){

highpulse = lowpulse = 0; // start out with no pulse length

while (IRpin_PIN & (1 << IRpin)) {
  // pin is still HIGH

  // count off another few microseconds
  highpulse++;
  delayMicroseconds(RESOLUTION);

  // If the pulse is too long, we 'timed out' - either nothing
  // was received or the code is finished, so print what
  // we've grabbed so far, and then reset
  if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
     Serial.print(" usec, ");
  //  printpulses();
    //currentpulse=0;
    return;
  }
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;

// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
  // pin is still LOW
   Serial.print(" usec, ");
  lowpulse++;
  delayMicroseconds(RESOLUTION);
  if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
  //  printpulses();
  //  currentpulse=0;
    return;
  }
}
//pulses[currentpulse][1] = lowpulse;

          // we read one high-low pulse successfully, continue!
       currentpulse++;
  }
    }

  void printpulses(void) {
        Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
         for (uint8_t i = 0; i < currentpulse; i++) {
            Serial.print(pulses[i][0] * RESOLUTION, DEC);
            Serial.print(" usec, ");
            Serial.print(pulses[i][1] * RESOLUTION, DEC);
            Serial.println(" usec");
           }

         // print it in a 'array' format
     Serial.println("int IRsignal[] = {");
     Serial.println("// ON, OFF (in 10's of microseconds)");
         for (uint8_t i = 0; i < currentpulse-1; i++) {
             Serial.print("\t"); // tab
             Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
             Serial.print(", ");
            Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
           Serial.println(",");
        }
          Serial.print("\t"); // tab
     Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
      Serial.print(", 0};");
        }

回答by

The uint8_t is a unsigned integer on 8 bits. In Arduino, it's called a "byte", so you can use it like that:

uint8_t 是一个 8 位无符号整数。在 Arduino 中,它被称为“字节”,因此您可以这样使用它:

for (byte i = 0; i < currentpulse; i++) {....

It's far better than using the Arduino's "int" type (== int16_t) or "unsigned int" (== uint16_t) because the ATmega328 is 8-bit. So handling an 8-bit var is faster (a lot).

它比使用 Arduino 的“int”类型(== int16_t)或“unsigned int”(== uint16_t)好得多,因为 ATmega328 是 8 位的。因此处理 8 位 var 更快(很多)。

I hope it can help.

我希望它能有所帮助。