In this lesson we explore how to create a Binary or Hexadecimal Bit Flipper. From our earlier lessons you see we can visually represent Hexadecimal or Binary numbers with a series of LED, with an on LED representing a “1” and an off LED representing a “0”. In programming and circuit applications, it is sometimes useful to “flip” or invert the bits. For an 8 bit number, one could do this in a program with 255 IF statements, but there is a simpler way. If you think about it, you can get the flippedByte by simple subtracting the byte from 0xFF, or 0b11111111. If you try some test cases, you can see that this will always work.
Simply stated, flippedByte=0xFF-Byte,
or if you prefer thinking in binary,
flippedByte=0b11111111-Byte
This is the circuit we are using to drive the 8 LED with the 74HX595 chip, and all this was explained in Tutorial 42.
This is the code which we developed in the video above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | int latchPin=11; int clockPin=9; int dataPin=12; int dt=1000; byte myByte=0x18; byte myByteFlipped; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(latchPin,OUTPUT); pinMode(dataPin,OUTPUT); pinMode(clockPin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(latchPin,LOW); shiftOut(dataPin,clockPin,LSBFIRST,myByte); digitalWrite(latchPin,HIGH); delay(dt); myByteFlipped=255-myByte; digitalWrite(latchPin,LOW); shiftOut(dataPin,clockPin,LSBFIRST,myByteFlipped); digitalWrite(latchPin,HIGH); delay(dt); } |
In all these lessons we are using the Arduino Super Starter Kit, which you can pick up HERE.