Arduino 空循环
Arduino Void Loop
为了class,我开发了如下代码。另一方面,我的老师要我浓缩它。如果有人有任何建议,我将不胜感激。谢谢
void setup()
{
DDRD = 0b11111111;
DDRB = 0b00000001;
}
void loop()
{
PORTD = 0b00000001;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00000010;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00000100;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00001000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00010000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00100000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b01000000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b10000000;
PORTB = 0b00000000;
delay(;
PORTD = 0b00000000;
PORTB = 0b00000001;
delay(t);
}
这应该遍历您要将 PORTD 设置为的值。它为每个循环移动一次“1”位,因为 bitval 只能容纳一个字节(8 位),第八次移位将使 bitval 为零,结束循环。
void loop()
{
for (byte bitval = 1; bitval != 0; bitval <<= 1)
{
PORTD = bitval;
PORTB = 0;
delay(t);
}
PORTD = 0;
PORTB = 1;
delay(t);
}
为了class,我开发了如下代码。另一方面,我的老师要我浓缩它。如果有人有任何建议,我将不胜感激。谢谢
void setup()
{
DDRD = 0b11111111;
DDRB = 0b00000001;
}
void loop()
{
PORTD = 0b00000001;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00000010;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00000100;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00001000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00010000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b00100000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b01000000;
PORTB = 0b00000000;
delay(t);
PORTD = 0b10000000;
PORTB = 0b00000000;
delay(;
PORTD = 0b00000000;
PORTB = 0b00000001;
delay(t);
}
这应该遍历您要将 PORTD 设置为的值。它为每个循环移动一次“1”位,因为 bitval 只能容纳一个字节(8 位),第八次移位将使 bitval 为零,结束循环。
void loop()
{
for (byte bitval = 1; bitval != 0; bitval <<= 1)
{
PORTD = bitval;
PORTB = 0;
delay(t);
}
PORTD = 0;
PORTB = 1;
delay(t);
}