首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino uint8_t变量

Arduino uint8_t变量
EN

Stack Overflow用户
提问于 2012-05-05 07:07:01
回答 1查看 22.1K关注 0票数 1

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

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

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

这两个代码本身都工作得很好。

代码可以编译,但是当我调用下面的空IRDetector()时,它不能工作。我已经对它进行了调试,到目前为止,我在使用变量uint8_t或uint16_t (我尝试用it和long替换它们)时发现了这一点。是否必须导入和库才能使用uint8_t?有什么想法吗?

任何帮助都将不胜感激。

代码语言:javascript
复制
 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};");
        }
EN

回答 1

Stack Overflow用户

发布于 2012-05-30 20:06:41

uint8_t是一个8位的无符号整数。在Arduino中,它被称为"byte",所以你可以这样使用它:

代码语言:javascript
复制
for (byte i = 0; i < currentpulse; i++) {....

这比使用Arduino的"int“类型(== int16_t)或"unsigned int”(== uint16_t)要好得多,因为ATmega328是8位的。因此,处理8位变量的速度更快(快得多)。

我希望它能有所帮助。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10457210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档