Created
October 31, 2014 15:32
-
-
Save joaquimpedro/02967290cd6ded8da9f5 to your computer and use it in GitHub Desktop.
Contagem binaria usando deslocamento de bits, 0 .. 255
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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