Question about lookup tables

Thread Starter

hunterage2000

Joined May 2, 2010
487
I am new to programming in C and want to know how to:

convert an analogue voltage from 0 to 5V to display in 0.1 steps to give 50 possible outputs to two 7seg displays.

The A/D converter is 10 bit so the analogue value of:

0V would be from 0 to 20.47
0.1 = 20.48 to 40.96 etc

I guess it would be possible to have 50 if statements to output the 7seg values but I something about a lookup table is quicker but I'm sure how this is done.

Can someone explain this to me and maybe give some code?
 

MrChips

Joined Oct 2, 2009
30,824
It is not quite clear what you are asking.

A 10-bit ADC will output a binary value from o to 1023.
If this corresponds to analog voltages from 0 to 5V, then 1 step = 5V/1024 = 4.88mV

Ignore the desire for 50 steps for the moment. (There is possibly no need to worry about the number of steps).

What do you want to be displayed on a 2-digit display with full scale 5V input?
 

Thread Starter

hunterage2000

Joined May 2, 2010
487
Basically to have a 0 to 5V input coming in and to display 0.0 to 5.0 on the 7segs.

Eventually this will be for a display on a DC power supply
 

pwdixon

Joined Oct 11, 2012
488
It could as simple as read the value then use multiple if statements to test if the reading is greater than the lower band value and less than the upper band value then display appropriate value. Of course each step requires knowledge of C and the hardware involved.
 

Thread Starter

hunterage2000

Joined May 2, 2010
487
So this would be 50 if statements that would be scanned 1 by 1? Would it be possible to use 50 interrupts or is this not typical? I heard a lookup table would be used if there is lots of possible outputs.
 

pwdixon

Joined Oct 11, 2012
488
Alternatively, read the value and then write a function that translates the value into characters for the display, this will require coding specific to the hardware involved. If you have a problem with coding the display interface you will need to provide a schematic for the electronics. Start by using fixed test inputs to the display function to make sure you have the display coding worked out then worry about getting the input reading to map onto the display value.
 

pwdixon

Joined Oct 11, 2012
488
So this would be 50 if statements that would be scanned 1 by 1? Would it be possible to use 50 interrupts or is this not typical? I heard a lookup table would be used if there is lots of possible outputs.
I would not expect that you won't have 50 interrupts available and in any case how would you trigger them if you did?

Doing 50 if's is just an extremely simplified way of viewing a first stab at the application, once you have learnt what the pitfalls are with that method you will realise you need to look deeper into C and find a different way. I would say get something to work and then polish it up and reduce the code after.
 

ErnieM

Joined Apr 24, 2011
8,377
That's not how you want to drive an analog to digital converter.

You start with your input: 0 to 5 volts. Either that directly or a divided down version gets applied to the A2D, which compares it to some reference voltage. Typically you get to pick that reference, either using the supply to the chip (guessing that is 5V) (which is somewhat innacurate as that voltage can vary), or a nice fixed reference voltage chip; these typically have an output of 2.048 or 4.096 volts (nice even numbers in binary).

For the moment assume you use a 4.096 V reference. I'll call this 4V as we go on.

Since 5V is greater than 4V you need some divider. Also keep in mind if your power supply goes a little above your 5V target you do want to be able to read that and display the overage. So let's make 6V that max input.

A divider is thus needed to convert 6V down to 4V, or a ratio of 4/6=2/3: 2K and 4K will get this for you, these need to be stable resistors that do not change. The actual tollerance is not very importaint as you can add calibrations later.

So now your A2D sees Vin * 2/3. The output count of the converter is approximately the ratio of the input to the reference times the max bit count, or:

Counts = (Vin * 2/3)/(Vref) * 1023

Let's see what a count of 1 gets us:

1 = (Vin * 2/3)/(4.096) * 1023 = Vin * 166.5

Solve for Vin:

Vin = 0.006V

So the step size or resolution is 6 mV per count. If you multiply the count you get from the A2D by 0.006 you get the voltage in volts. But ouch, that needs a floating point number to capture the decimal part.

So... let's make this as useful as can be, as you want to display X.X volts. There is nothing making you keep volts as the ultimate unit. In fact, using "volts times 10" makes a better unit for you as that is what you want to send to the display. That is a factor of 0.006 * 10 or 0.06. Here's how to do that in plain integer math (no floating point needed):

Note that 0.06 = 6 / 100. So:

Volts = (Counts * 6) / 100

By arranging the calculation this way you get the desired voltage out into a variable.

Now all you need do is pull out the pieces:

Units = Volts % 10; // modulus division (ie the remainder)
Tens = Volts / 10; // standard division

Having these two variables it should be easy to convert to the 7-segment code (which is best done with that look up table).

The ultimate code is up to you. We are quite willing to help you with each and every step. If we know some helpful example we will point to it, but we are not a code writing supply.
 

MrChips

Joined Oct 2, 2009
30,824
I'll assume that a full scale input of 5V gives ADC_Count = 1023.

Forget about the 50 steps. The computer code is straight forward:

Code:
unsigned int ADC_Count;
unsigned int result;

result = ((ADC_Count * 50) + 511)/1024;
 

pwdixon

Joined Oct 11, 2012
488
I'll assume that a full scale input of 5V gives ADC_Count = 1023.

Forget about the 50 steps. The computer code is straight forward:

Code:
unsigned int ADC_Count;
unsigned int result;

result = ((ADC_Count * 50) + 511)/1024;
What's the extra 511 for? Wouldn't that mean you couldn't read less than 0.5V?
 
Top