NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Project Help and Ideas » Binary Counter Using Shift Register
January 24, 2012 by SpaceGhost |
Hello all, I recently got a few 74HC595 shift registers that I have been playing with. I am trying to put together an 8 bit, 0 - 255 binary counter, with the eight shift register outputs representing the binary coded output. (Hope I'm explaining that right!) I am working off of a modified version of the demo code Rick posted (the really cool blinky LED thingy) in the Photography club rating kit thread. What I have going almost works... When I tie PB1 low, the counter definitely starts to count - LEDs 1, 2, 4, 8, fire off pretty quickly, then the time increases of course as LEDs 16, 32, 64, and 128 begin to fire off. The most significant digit LEDs light brightly as the count progresses; however, the lesser significant LED's only dimly flicker. I would like for the lesser significant LED's to become fully lit also, as they are being used. The flickers do seem to be flickering in their proper order. This leads me to believe that I must be close. But I've been playing with the code for a couple evenings now and whatever it is, it is still eluding me. I think it may be how I'm trying to use
Here is the code that I am working with -
As always, I appreciate any suggestions, hints or comments that anyone can offer. Dave |
---|---|
January 24, 2012 by SpaceGhost |
I just tried something, and got an interesting result - I commented out lines 103 through 115, and put in -
then loaded the code and ran the program again. Now when I tie PB1 low, all of the proper binary output LEDs light for the number 54. When I remove PB1 from gnd., the most significant LEDs for 54 remains lit, but the other three turn off.. Hmmm... |
January 25, 2012 by 6ofhalfdozen |
SpaceGhost, I am not the most familiar with shift registers, but when I read your two posts with their changes a couple things stand out. a. is it possible that in the original version the "less significant" LEDs are cycling through/counting up so fast that you can not properly see them and they appear dim(ie they act like PWM due to cycling speeds and appear dim due to low current)?? If that is the case, try slowing it down some more, those commented out delays might let you see them better, albeit slower counts. either that or when up in the higher counts (say >64) you only change the LEDs/shift registers for every 6 counts or so which would leave the lower value LEDs on longer between changes. b. I am thinking that in the second post when you set i to 54, it is all lit because line 101 is true, but when you disconnect PB1, it becomes false and somehow the less significant digits drop out, dunno if thats a code or shift register issue, but I think thats where it is. hopefully that helps a little |
January 25, 2012 by SpaceGhost |
I had considered the same thing - that perhaps the count was cycling too fast. I put the delays back and increased the duration. The count is considerably slower now, of course. I can definitely see that it the LEDs are lighting/counting in their proper order. The problem seems to be with the "on" time of the lesser significant LEDs. Either the LED "on" time is still too short, or for some reason I am not getting full current to the LEDs. I still am not grasping how to set the durations of the output pulses themselves, just the duration between the pulses (off times). The duration is long, but the "count" is just a quick, dim "blip".
In all probability I will probably be able to use what I have (tweeked up a bit) for my intended purpose anyway - what I'm wanting to do is have the code display the binary equivalent of an integer (0 - 255) that I can set with some keypad code that I have written. But since I started out trying to build a counter, I am of course still trying to figure this out. |
January 25, 2012 by Rick_S |
Ok, I'm assuming by the toggling of bits that you are attempting to invert the byte going to the shift register.... Why not just reverse your LED's and then just write the byte to the shift register? Or, if you need to invert the byte because your LED's are an array with a common anode/cathode, why not just invert the whole byte then send it once. To invert the whole byte just xor it.
What you are probably seeing is the issue of writing the byte multiple times. Rick |
January 27, 2012 by SpaceGhost |
Hi Rick, sorry it took me a couple days to answer back. I do not quite understand your answer though.. My LEDs are on a common connection - the eight anodes are connected together to the positive rail. I think I understand what you are saying about sending the "whole byte", all at once. That would be what happened when I changed the code to display the fixed value integer, right? How would I go about XOR-ing the whole byte to send it all at once? I am not sure how to use the code snippet that you suggested. By the way, I have managed to combine the shift register code that I had posted above with my one-wire ADC keypad code - and it's working pretty well! I can enter a number from 000 - 255 with the keypad and store it, then display that number's binary output by pressing another button. Pretty nerdy I know, but I actually have a practical application in mind for the project. |
January 27, 2012 by SpaceGhost |
Oh yeah, I meant to say something about "What you are probably seeing is the issue of writing the byte multiple times." That seems plausible... What I'm seeing the lesser significant bits doing does appear as though they are rapidly turning on then back off - like the byte is being written "multiple times." The most significant bit is the only one that does not appear to be changing because it's in use more than the others? |
January 28, 2012 by Rick_S |
I meant replace all this:
with this:
The Caret symbol (^) represents XOR (Exclusive OR). An XOR will only show true IF ONLY one of the bits being compared is true NOT both. So if you XOR 0xFF (0b11111111) with any byte, it will invert it. For instance 0x03 ^ 0xFF = 0xFC
Rick |
January 28, 2012 by SpaceGhost |
Wow, that works great Rick. Thanks again for your help! I appreciate your explanation on using the boolean operator. "So if you XOR 0xFF (0b11111111) with any byte, it will invert it" .. Okay, I think I'm starting to get it now. Hmmm, and it's got me thinking even more. I don't know why I waited so long to delve into shift registers, these things are great! I'll be applying this to my keypad project too, sure simplifies the code. |
January 28, 2012 by Rick_S |
No problem. Did your ghosting issue go away with that as well? Rick |
January 28, 2012 by SpaceGhost |
Yep. I can slow down the count or speed it up and still get a good solid read out. I left the delay (2000 ms)out. Works perfect, in my opinion. Neat little program. |
Please log in to post a reply.
Did you know that you can impress a loved one with a digitally-controlled Valentine's Day Card with randomly twinkling LEDs? Learn more...
|