ビット演算子

スポンサーリンク

Arduinoにおける 文法は標準C言語 と特に変わりはありません。

●Example from Arduino Web Site

[c]
int i; // counter variable
int j;

void setup(){
DDRD = DDRD | B11111100; // set direction bits for pins 2 to 7, leave 0 and 1 untouched (xx | 00 == xx)
// same as pinMode(pin, OUTPUT) for pins 2 to 7
Serial.begin(9600);
}

void loop(){
for (i=0; i<64; i++){

PORTD = PORTD & B00000011; // clear out bits 2 – 7, leave pins 0 and 1 untouched (xx & 11 == xx)
j = (i << 2); // shift variable up to pins 2 – 7 – to avoid pins 0 and 1
PORTD = PORTD | j; // combine the port information with the new information for LED pins
Serial.println(PORTD, BIN); // debug to show masking
delay(100);
}
}
[/c]

[c]
// Blink_Pin_5
// demo for Exclusive OR
void setup(){
DDRD = DDRD | B00100000; // set digital pin five as OUTPUT
Serial.begin(9600);
}

void loop(){
PORTD = PORTD ^ B00100000; // invert bit 5 (digital pin 5), leave others untouched
delay(100);
}

[/c]

Arduino 原文

タイトルとURLをコピーしました