74hc164 + 2x 4511 bcd decoder

Hi everyone.
i'm trying to use 2 bcd decoder with 1 74hc164.
if i use

shiftOut(data, clock, LSBFIRST,B10011001);

i'll get the number 9 in both displays.

i need some help to make a function that will convert a DEC number i.e 34 into B00110100 so i can send it to the 74hc164.

thanks everyone!

cheers

The value 34 consists of 2 digits. You can separate the two digits into separate variables:

byte num = 34;
byte digit1 = num / 10; // digit1 will be 3
byte digit2 = num % 10; // digit2 will be 4

Now, you just need to convert each digit to a binary representation.

byte b[4];
if(digit >= 8)
{
    b[0] = 1;   // If digit is 8 or 9, first position is 1
    digit -= 8;
}
else
   b[0] = 0; // Otherwise, it's 0

// digit is now 7 or less
if(digit >= 4)
{
    b[1] = 1; // if digit is 4, 5, 6, or 7, 2nd position is 1
    digit -= 4;
}
else
    b[1] = 0; // Otherwise, it's 0

// digit is now 3 or less
if(digit >= 2)
{
    b[2] = 1; // if digit is 2 or 3, 3rd position is 1
    digit -= 2;
}
else
   b[2] = 0;  // Otherwise, it's 0

// digit is now 0 or 1
b[3] = digit;

There are much shorter ways of doing this, but this works, and is easy to understand.

So, i should use this for digit1(byte a) and digit2(byte b)

and then use

shiftOut(data, clock, LSBFIRST,a|b);

?

I think you'd need to output (a << 4)|b, not a|b. But, I could be wrong. I always have to experiment to get the right results.

Thanks for your help dude..
i´ll try that as soon as i get home.

cheers :smiley:

Does anybody knows if this would work?

byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

It should, but I don't think you'll remember in 6 months why you are multiplying by 16 in the first part. Left-shifting by 4 accomplishes the same goal, and is easier to remember the goal later.

Just my opinion...

Hey guys,

Actually i'm doing this:

#define data 2
#define clock 3 

byte n0 = B00000000;
byte n1 = B00000001;
byte n2 = B00000010;
byte n3 = B00000011;
byte n4 = B00000100;
byte n5 = B00000101;
byte n6 = B00000110;
byte n7 = B00000111;
byte n8 = B00001000;
byte n9 = B00001001;
byte d0 = B00000000;
byte d1 = B00010000;
byte d2 = B00100000;
byte d3 = B00110000;
byte d4 = B01000000;
byte d5 = B01010000;
byte d6 = B01100000;
byte d7 = B01110000;
byte d8 = B10000000;
byte d9 = B10010000;

void setup()
{
  pinMode(clock, OUTPUT); // make the clock pin an output
  pinMode(data , OUTPUT); // make the data pin an output
   // send this binary value to the shift register
   Serial.begin(9600);
} 

void loop()
{
  shiftOut(data, clock, MSBFIRST,n3|d7);
  Serial.println(n3|d7,BIN);//for debuggin purpose only
  delay(5000);
}

the problem is that the left 0's are not sent. so instead of the 74hc164 receiving 01110011 it is receiving 1110011

any help? :smiley:

anyone??? :o

Hi
Try reading from the "arduino.cc/en" the "Tutorial/ShftOut13" page
It may help you :wink:

(Sorry, links not allowed!!)

::slight_smile:
errr... hmmm the last code a posted was working.. but i was so stupid that i inverted the 4 bcd pins in one of the 7-segments.

thanks for your support :smiley: