LUT for 10bit ADC??

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

LUT for 10bit ADC??

Post by jack26 »

After writing flowchart on Flowcode 4 to solve the following problem using Pic 16F88:-

Read a voltage between 0 and 5V with ADC channel
Display Error on LCD if voltage is below 1.3V or above 4V.
If no error display a numeric value relative to the analogue input voltage.

There are 15 defined points the first few being:-

ADC Numeric value

267 0
285 1
304 2
322 3
etc to numeric value 14.

I also need to interpolate numeric values between 1 and 2 etc from the intermediate ADC values.

My first attempt involved creating an excel spreadsheet of ADC versus numeric values, graphing it and then creating a formula from trendline. The result was a curve which required at least a 3rd order polynomial equation. So dutifully put that into flowcode and Pic ran out of memory! Persisted and got it to fit in memory eventually but solution is really grim.

After looking at some of the posts and examples I think that the way forward is a LUT. I have the idea but would appreciate some advice as to the best approach.

Q) The examples I have seen show ADC values scaled down from 255 to 30 so its count is the same as LUT values present. In my case the ADC is 1023 and the values to be detected range from 267 (=0) to 729 (=14). Does this mean I would have to create a LUT (generated from excel) with a unique numeric value for each of the ADC values?

jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: LUT for 10bit ADC??

Post by jack26 »

Had an attempt at constructing a LUT using the following thoughts;

Because the valid ADC range started at 266 and ended at 818 I subtracted 266 from ADC value to start with giving me a new range of 0 to 552. Any value outside of this range is displayed as a 0 for ADC value and ERROR for Index value. Then I created a table using Excel to generate intermediate values for the Index value versus ADC value. This Index value was then multiplied by 10 to give whole number for LUT. Now this is where things went a bit strange and looking at other posts I think the problem is that the LUT is limited to 255 Bytes? so when I put the full table in an error resulted when it was compiled. I reduced the table to 255 terms and that compiled and loaded to PIC ok. Problem now was that the LUT did not match the ADC value range so strange things happened to the LCD display when LUT locations were exceeded.

Would the solution to this problem be to create a further 2 LUT with other remaining INDEX values in them and then select them as required when the ADC value exceeds the range of say LUT 1 and then moves onto LUT 2 ??

Also noticed that the LUT seems to have to be entered in reverse order ??

Its a bit messy but I have attached flowchart developed so far,
Attachments
ADC_LUT_VER1.fcf
(10.5 KiB) Downloaded 355 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LUT for 10bit ADC??

Post by medelec35 »

Hi jack26,
You can only have a maximum of 256 different variables (index of 0 to 255)
If you have one LUT then Max value retrieved is 255.
You mentioned about numbers being in the wrong order.
if you have got
10,9,8,7,6,5,4 etc.
Then INDEX(0)=10
INDEX(1)=9
INDEX(2)=8 etc
If you only ever index range between 240 and 250, then you must have 251 lots of numbers in your look up table, so you do need to subtract 240 from your ADC for example. so you only require 10 lots of numbers instead of 251.
I have also posted a LUT tutorial here:
http://www.matrixmultimedia.com/mmforum ... 26&t=10066
Maybe worth a look if you have not seen it already?

There are posts covering retrieving numbers for example 0 to 32767 using two LUT's.

But that's not really what your after.
Here is what I would do. You correct, subtract to give a range of 0 to 552!

Use two lots LUT.
then use a decision branch.
IF newrangeADC>255
then INDEX2= newrangeADC-256

So if newrangeADC=258 then INDEX2 = 2
Therefore the 3rd number of second LUT is retrieved.

If I have confused you a bit as its difficult to explain, I will post a LUT with my suggested mods a bit later tonight, as im still at work :(

Martin
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LUT for 10bit ADC??

Post by medelec35 »

I have alterted your flowchart and just added two extra LUT's.
So now you 552 different LUT values depending on the pot setting!
This can be expanded to have a total of 1023 LUT values.

Hope you find this useful.

Martin.
Attachments
ADC_LUT_VER2.fcf
(14.62 KiB) Downloaded 432 times
Martin

jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: LUT for 10bit ADC??

Post by jack26 »

Thanks for input Martin, apprecated.

I can follow your answer and it make sense. I have had a bash and attached my efforts, which I know are not as neat as your solution and will tidy up when I get it to work!

Just tried flowchart (spent most of the last few hours sorting the LUT values out on excel,arrgh) and it does not work quite right crashing the pic, so think that's a potential problem! I will have a look and see if I can sort out errors and let you know outcome,

Cheers

John
Attachments
ADC_LUT_VER1.fcf
(13.53 KiB) Downloaded 360 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LUT for 10bit ADC??

Post by medelec35 »

I have looked at your flowchart, and it does look like you do understand the principle.
however It is not quiet right, but nearly!

The problem is if you set the pot to ADC 316 for example
ADC=319-266 = 53

With 53 all the LUT's will be accesed, but its the last one that's accessed:INDEX3 that will cause a problem and will cause your microcontroller to corrupt.
It will read the 54th number which don't exist.
So the program counter will be push to a part of memory it should not be in.

The way round is embed each LUT so when one is read the others are bypassed.

That's how I did it in the modified flowchart I posted.

Martin
Martin

jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: LUT for 10bit ADC??

Post by jack26 »

I think our posts crossed and only just picked up your attachment, which leaves me something to dwell over!

I have tried to download your code to dev board but its playing up and display is 'locked up' , so will try it again in the morning, think possible problem with laptop link. I checked my Excel file earlier and have duplicated some values, so also need to sort that out.

Seeing your code makes things look simple, tidy things like setting of Error like a flag? (I think) its been a while since I programmed and a bit slow, but your examples are a great help and really appreciated by an old (ish) hardware engineer brought up on Pascal and Z80, 8086 stuff!

Thanks Martin, will post results tomorrow,

John

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LUT for 10bit ADC??

Post by medelec35 »

Hi John,
hope it does work for you. I have not tried on hardware, but what you could try is place a delay e.g. 100ms I'm the main loop. That may stop lcd from locking up. Reason for using. Flag is because there will be a point when adc - 266 = 0 so 'ERROR' will be wrongly displayed.

Martin
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: LUT for 10bit ADC??

Post by Benj »

Hello,

I have experienced LCD lockups before when working on firmware involving interrupts. Basically the interrupt had the possibility of kicking in while talking to the LCD and this was messing up the timing and causing the LCD to hang. I found that if I replaced one of my clear commands with a start command then this seemed to allow the LCD to come back and work correctly as the start command should allow the display to re-sync with the data being sent to it.

jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: LUT for 10bit ADC??

Post by jack26 »

Hi Martin,

Loaded code again this morning with both a delay in and start command in place of one of the clears as suggested by Ben and everything now works fine. The table has been redone and values for index now agree with the ADC values so really chuffed! I should have known better than to originally try this with a formula using floating point notation but I guess never learn!

Thanks again Martin for your help appreciated, thanks Ben,

Cheers

John

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LUT for 10bit ADC??

Post by medelec35 »

Hi John,
Your welcome.
I'm so pleased your hardware is working the way you intended it to (and after only posing your 1st post on Wednesday) :)
Creating the excel sheet must have been a bit of a challenge and headache!

Bet you find Flowcode so much easier than Pascal and Z80 etc.
I use to program spectrum and vic20 in basic....wow! remembering goto, gosub, data, peek and poke :lol:

Thanks for the updated post.

Martin
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: LUT for 10bit ADC??

Post by Benj »

Great glad the workaround is working for you.

I remember those commands, So glad that C is pretty much everywhere now :D

I started out on a Motorola 68000 setup writing assembler and a BBC writing Basic.

jack26
Posts: 23
Joined: Sun Oct 09, 2011 4:38 pm
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: LUT for 10bit ADC??

Post by jack26 »

I 've still got my Z80 and 8086 assembler programming manuals, no idea why but it makes me 'feel safe'! Unfortunatly I never used C in anger and only picked bits and bobs up as I went along. The uni that I went to did my foundation course using Pascal and then at some later point in the degree our control lecturer decided that we would be given example code to mod in C. Control theory was bad enough but solving problems using C which I had never used was interesting to say the least and I think you would have been horrified at my attempts! Most of my work time now is using Labview (sorry) for UAV instrumentation but it has lots of limitations doing PID unless you go for the real time pakage and hardware. Because I have not done firmware for such a long time Flowcode looks to me like an ideal way to gently get me back into programming and I can try out my attempts at C code seeing instant results. Having done several years in the past lecturing in both College and Uni I can honestly say that I wished Flowcode had been around then, which would have given students a rapid result but letting them expand their skills as problems arose, i.e like mine running out of memory!!

All I can say guys is its a cracking program and support is excellent,

thanks again,

John

Post Reply