Skip to content

Instantly share code, notes, and snippets.

@joaquimpedro
Created October 31, 2014 15:32
Show Gist options
  • Save joaquimpedro/02967290cd6ded8da9f5 to your computer and use it in GitHub Desktop.
Save joaquimpedro/02967290cd6ded8da9f5 to your computer and use it in GitHub Desktop.
Contagem binaria usando deslocamento de bits, 0 .. 255
int const clock = 2;
int const latch = 3;
int const data = 4;
void setup() {
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(data, OUTPUT);
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < 255; i++) {
digitalWrite(latch, LOW); //abre fluxo de bits
for(int j = 0; j < 8; j++) {
digitalWrite(clock, LOW); //abre para receber o bit
Serial.print("i - ");
Serial.print(i);
Serial.print("j - ");
Serial.print(j);
Serial.print(" i & j -> ");
Serial.print(i & (1 << j));
Serial.print("\n");
if(j == 7) {
Serial.print("Fim byte ");
Serial.print(i);
Serial.print("\n");
}
if(i & (1 << j)) { //Escreve o bit
digitalWrite(data, HIGH);
} else {
digitalWrite(data, LOW);
}
digitalWrite(clock, HIGH); //Grava o bit 0 ou 1
}
digitalWrite(latch, HIGH); //Envia os bits
delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment