hello i need a bit of help with this code..
Im trying to make a counter to count up to 900 and im using a 74LS247 driver. now this chip use 4 bit to decimal (like show below). i know there a why to input all the number as a table or something to save me form type the 4 bit ever time i want it to show 1 as number.
You are using a binary coded decimal driver, right.
Do this:
//pins for bit 1 -4
short pins[] = {5,2,3,4}
int i, j;
//display numbers from 0-10
for(i = 0; i< 10; i++) {
// for each pin output the corresponding bits
for(j = 0; j <4; j++) {
digitalWrite(pins[j], (i >> j) & 1);
}
delay(5000);
}
The computer stores the numbers in this format and the LED segment seems to want them in this format.
That makes it very easy to directly output them:
int number = 6; // Number we want to output
digitalWrite(A, number & 1);
digitalWrite(B, (number >> 1) & 1);
digitalWrite(C, (number >> 2) & 1);
digitalWrite(D, (number >> 3) & 1);
Analysis:
number & 1
This calculates the bitwise AND of number and 1
number is 0110 and 1 is 0001
0110 & 0001 = 0000 = LOW
In the arduino library LOW is the same as 0, so we can directly put the result into digitalWrite
digitalWrite(B, (number >> 1) & 1)
This time, before we compare number to 1, we shift it right 1 bit.
0110 becomes
0011
Then we check the last bit again:
0011 & 0001 = 0001 = HIGH
Again we can put the result directly into digitalWrite, because 1 is recognized as HIGH.
For your segment you want to split a number into its individual bits, and put them onto differnt pins.
(number >> bit) & 1
does this, it shifts the number right, so that the bit you are interessted in is the lowest one, and then int sets everything else to 0, so that only the lowest bit determines, if the result is 0 or 1.
Hope this helped, you might also want to look into:
Thank a lot!!! it make some scene to me. i just need to play with this a bit more... any idea if there a chip that can drive 3 or more 7 segment led?? or do i need to mount more 74ls247 together ??
Sorry, i'm still new to the hardware side myself. If you need to drive a couple of 7 segment displays, you might consider shift registers. Those are more complex to use, but you can have as many segments as you want with only 3 or 4 pins.
If you decide to use them, just post again, i have some code ready for that scenario.
There are many dedicated LED driver chips from Maxim et al, but shift registers are very easy to use. Some options are
74xx595 (xx = HC, LS etc depending on technology)
74xx4094
74xx164
UCN5821/5822
all these are normal logic serial in parallel out (SIPO) registers so you will still need current-limiting resitors for every segment.
TPIC68595 same as 74xx595 but sinks 150mA per pin.
NBI5029, 16 constant-current outputs so only one resistor is required to set current for all LEDs.
In all cases you only need two pins to shift the data into the chips and another to latch the data to the outputs for the LEDs (the algorithm is simple). As many as you like can be cacsaded in a row.
I must say I would go with some shift register. After I just started using them a day ago I can't believe how awesome it is. The code is also very simple, just use 8 bit binary. So make a table like
byte zero = B11111100;
byte one = B01100000;
byte two = B11011010;
byte three = B11110010;
byte four = B01100110;
byte five = B10110110;
byte six = B00111110;
byte seven = B11100000;
byte eight = B11111110;
byte nine = B11110110;