# programming suggestions: software analog to binary converter?

Discussion in 'Embedded Systems and Microcontrollers' started by Steve C, Nov 23, 2009.

1. ### Steve C Thread Starter Active Member

Nov 29, 2008
86
0
Hi everyone,

I will be experimenting soon with a 1-wire interface to an analog input pin of a microcontroller. The multi-button input MUST sense multiple presses, so I will be using a circuit based on the R / 2R chapter of the guides written on this website.

Now, for THIS circuit, I will be using only three inputs. So there will only be seven voltages to detect. So I can easily write code to interpret each voltage individually.

But future projects may require many more inputs, so I want to learn a more efficient way to code input detection.

------

The design will likely work some way like this:
output 1 will raise the input pin by .5V
output 2 will raise the input pin by 1V
output 3 will raise the input pin by 2V

So There's gotta be a really easy way to code sensing a voltage between .5 and 3.5 volts in .5 volt increments and translate that to a three digit binary code representing which outputs have been activated.

I just can't visualize the code.

------

Can anyone recommend the high level direction I can go when writing the code that will accomplish this?

2. ### BMorse AAC Fanatic!

Sep 26, 2009
2,675
237
So you actually don't want an Analog Value to Binary conversion, you just want to set 3 bits to something depending on what the voltage level is at the input pin... Right?

Since converting a 8-10 bit ADC reading into 3 bit binary is useless.....

Basically (depending on what language you are using to write the code....) you just need to use select case statement.....

My .02

3. ### rjenkins AAC Fanatic!

Nov 6, 2005
1,015
69
I believe you are trying to implement an analog to digital conversion by using a comparator plus a resistor ladder D to A?

The simplest way is to use a byte as a counter, feeding the 'high' bits to your resistor ladder outputs.

If the comparator input is off (unknown voltage greater than ladder output), and counter < 255, increment the counter.

If the comparator input is on (unknown voltage less than ladder output), and counter > 0, decrement the counter.

When the comparator changes from off to on, copy the counter value to the result register.

The counter will 'follow' the input, once it's as near as it can be it will hunt up and down by one increment (of the output bits), which will be some number of passes through the code depending on the unused bits.

To get an accurate reading without excessive jitter, add one or two more stages to the ladder then mask the result to the higher bits, so the jitter is only affecting the masked-off bits of the result.

4. ### Steve C Thread Starter Active Member

Nov 29, 2008
86
0
Yes, this is nearly exactly right. The plan is to define behavior based on the 3 bits. Which has now been changed to 5 or 6 bits (probably 5)

Meaning the case statement would be a little unwieldy, wouldnt it?

5. ### AlexR Well-Known Member

Jan 16, 2008
735
55
Whatever way you do it its going turn out unwieldy. You would be much better off using 3 or 5 (or however number of bits you end up needing) lines running to dedicated microcontroller pins. At least that way you will be able use the interrupt on change function on the processor leaving it free to do useful work between button presses. The way you propose to do it you will need to be constantly polling of the line line and doing endless AtoD conversions to see if anything has changed.

If running multiple lines is not practical think about sending the switch status as a serial data stream. Again this would save you from having to constantly poll the line.

6. ### Steve C Thread Starter Active Member

Nov 29, 2008
86
0
I agree. But my micro doesnt have enough inputs for that, and the cost jumps 6x to get the IO needed. meanwhile, I'm using no AIO. Seemed like a logical design choice.