Pages: [1]   Go Down
Author Topic: Convert ring counter to binary  (Read 1066 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everbody!

I just wanted to know if there is a single hardware component or simple hardware solution to convert a ring counter value to a binary value. Here is an example:

Input -> Output
0001 -> 00
0010 -> 01
0100 -> 10
1000 -> 11

My project uses much higher input values (Input is 16bits, therefore Output must be 4bits). OR'ing the right bits and link them together would be a solution in the example (only 2 ORs needed) but not in my project (32 ORs needed = much to connect).

Thanks for helping, Rob!

edit: Corrected thread.
« Last Edit: July 20, 2012, 06:17:57 am by privateRob » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 637
Posts: 34602
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The input is not a binary count, it is called a ring count.
To convert shift the input one place to the right and keep doing it until the number is zero. Then the number of shifts you had to make is the binary value.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

GCC/G++ has a builtin function to count the number of leading zeros of an int (__builtin_clz), and long/long long versions (__builtin_clzl and __builtin_clzll).  In looking at the compiler code, it looks like __builtin_clz and friends calls into the library, and there they have assembly versions of the routines.

So you would want something like:

Code:
n = 16 - __builtin_clz (x);

Now, if you ever plan to move your code to a different machine, such as an ARM, you probably should use this instead:

Code:
n = (sizeof (int) * 8) - __builtin_clz (x);

As I said, AVR doesn't seem to have an instruction built in that does count leading zero in one operation, but other machines do, such as x86, powerpc, and arm5.  Given the library routine is written in assembler, it is probably faster than the shift and loop function.
« Last Edit: July 19, 2012, 05:54:36 pm by MichaelMeissner » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Stupid me!  smiley-red
I forgot to say that I'm looking for a hardware solution. Best without any clock cycles.

... and thanks for the help so far! smiley
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 637
Posts: 34602
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You will ave to post the schematic of what you have before we can consider a hardware solution.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Stupid me!  smiley-red
I forgot to say that I'm looking for a hardware solution. Best without any clock cycles.

... and thanks for the help so far! smiley
My sense is for a lot of the computers that have a count leading zeros instruction, internally the processor does a combination of if and loop type operations.  You can throw more logic at the problem, depending on the usual cost/benefit analysis.  On the server powerpc's that I deal with for work, the CNTLZ instruction is a 2 cycle instruction, while the normal integer operations are 1 cycle.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 637
Posts: 34602
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My sense is for a lot of the computers that have a count leading zeros instruction
No they don't, the instruction you referanced is an increment or decrement with a branch if the result is less than zero.
I know of no instructions that count zeros in a word.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 637
Posts: 34602
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If all you have is the 16 ring counter inputs, you can use a pair of 74LS348 priority Encoders.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If all you have is the 16 ring counter inputs, you can use a pair of 74LS348 priority Encoders.

Yes, yes, YES! Thank you very much! smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...or one 74LS150...?
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 637
Posts: 34602
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
or one 74LS150...
No that is a multiplexer chip not a priority Encoder. The two are totally different things.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

After one more readthrough I see what you mean. Sorry.
Logged

Pages: [1]   Go Up
Jump to: