How best to interpret a binary input

I would like to be able to read approximately 30 positions of a rotating device but rather than using 30 individual inputs on a PIC device would like to convert these to a binary sequence which would therefore only require 5 inputs.

I can see the means to both detect and convert the positions to binary (probably using hall effect sensors and a little logic) but would appreciate some suggestions as to how best to deal with the resulting inputs.
I presume there will be a better way than writing code to question each possible permutation of the five inputs to find which of the 30 possible states is presently valid.

What I envisage is to get the binary to a decimal variable from which a simple calculation would be made by consideration of two other variables and using this result to output instruction to rotate to a new position.

Thanks
 

nick12ab

Senior Member
Put the binary inputs onto a port's pins 0 to 4 in order and just use a mask to cut off the last three bits, then you have the number.
 

Dippy

Moderator
Be more specific about your rotating device and how electrical contacts are made.
It could be that you could use a string of resistors and use an ADC.

You could use a digital encoder chips using the enable pins if necessary.
Or daisy chain with supplementary logic (or using more PICAXE pins).
I'm not dictating you use this chip but check out this data sheet to see the idea.
http://www.ti.com/lit/ds/symlink/sn54hc148.pdf


What is level of skill/experience with electronics and coding?
 
Thanks you for your responses.

Dippy.

I envisage machining one or more simple disks to create four binary ‘tracks’ that will be read by 4 x hall effect sensors though optical devices are an option. I would use a comparator to achieve high or low digital outputs. The vulnerabilities of the physical environment strongly promote the advantages of absolute digital outputs over a stepped analogue resistance to an ADC input which I have considered.
I have plenty of electronics ability but am very new to using microcontrollers and hence am considering projects like this to learn how to use them.
The chip you suggest is certainly of interest and what I would previously have gone straight towards using but I presume that the real value of using microcontrollers is that such items become redundant.
I am most pleasantly surprised for example, to find that most of my previously used PWM circuits are now redundant as I can achieve the same as would have taken me hours of design and assembly with a few minutes of programming!


Nick.

That sounds like the sort of thing I anticipate might be possible but could you please elaborate on how to utilise this idea.


Cheers….
 

nick12ab

Senior Member
That sounds like the sort of thing I anticipate might be possible but could you please elaborate on how to utilise this idea.
The big question is... do you already have the binary output or do you need to create that? If you currently only have decimal outputs, it would be much easier to use Dippy's resistor suggestion but you'd need low tolerance ones.

If you don't already have electrical connections and you have to make the sensor to, it's easy as you just use the same principal as used in rotary hex switches.
 
As detailed above, I will create the digital inputs which I am very capable of achieving without too much fuss.

This discussion has perhaps inspired a sudden realisation of a very workable solution that is so obvious I am not sure why I didn’t realise sooner:
I individually read the pins that are my binary inputs such as for example if C.1 is high, variable b1 = 16; if C.2 is high b2 =8 etc. I can then have a variable that is the mathematic total of these and presto! I have my decimal output from digital inputs.
I think that makes perfect sense. Doesn’t it…????
 
Ahaha!! So there is a simpler way.
And is that the actual code as you have written it?
And can I still use the remaining C pins in a normal fashion having used that command??

Added...Yes that is the actual code and in simulation it does exactly what I wanted.
Thank you...
 
Last edited:

nick12ab

Senior Member
That is the actual code. You can still use the other C pins as before as the last part ('and %00011111') is a mask which filters out the pins C.5-C.7 and only includes pins C.0 - C.4. To adjust the number of bits you're using, simply change the number of 1s. The only thing is that the least significant bit has to be connected to C.0 rather than the most significant byte as you have shown above.
 

Paix

Senior Member
Back in the day they invented the Gray code, so that only one digit was changing at a time. Not so handy, but indicative of a possible problem where two digits change together where things can get confused. Easily rectified by a clock track so that the data is read in the middle of the shortest element; if that makes sense?
 
Top