gpt4 book ai didi

c++ - Arduino uint8_t 变量

转载 作者:行者123 更新时间:2023-11-30 02:04:34 24 4
gpt4 key购买 nike

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

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

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

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

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

如有任何帮助,我们将不胜感激。

 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};");
}

最佳答案

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

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

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

希望对您有所帮助。

关于c++ - Arduino uint8_t 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com