5 posts / 0 new
Author
Message

Hi guys,
I'm having trouble with a project that is supposed to read a voltage thanks to ADC1 (this part is fine,I got it) and compare this value with a lookup table to find what to display on my 3 seven segment display. I have to use assembly and I know that to define a lookup table I have to use .db.

For example: If the voltage is 5V the displays have to show 1,2,3.

My understanding is that the lookup table should be something more like:

.db 5,1,2,3

But how can I go on the lookup table finding the closest value to what the ADC1 got and save the next 3 cells on 3 register?

Thanks for help

Well the real issue here is how you convert the 0..1023 ADC reading into an index into the table. One possibility would simply be to have a 1024  entry table but with 3 bytes per entry that's a 3K table. If you don't have the room for that you might decide to just divide the value by /2 or /3 or /4 or something. In binary obviously /2, /4, /8 are the "easy ones" as this is just a right shift of 1, 2 or 3 places. If you /2 then the table has 512 entries. If you /4 then it has 256 entries and so on.

While a reading of 731 might mean something like 3.57V it doesn't really matter about converting from the raw 0..1023 to Volts and then use that as an index. All you really need to know is that a raw reading of 731 means you need to display "088" on your three digits (can you guess how I worked that out?? ;-)

clawson wrote:

While a reading of 731 might mean something like 3.57V it doesn't really matter about converting from the raw 0..1023 to Volts and then use that as an index. All you really need to know is that a raw reading of 731 means you need to display "088" on your three digits (can you guess how I worked that out?? ;-)

Unfortunately I don't get it, but this is exactly what I need to do

Well I was simply taking the fact that you said "5V should display 123" to assume that a maxinum readaing of 1023 on the ADC meant 5V and then 123. So if you got a reading of 731 then this would be 731/1024 of "full scale" so 0.713. If you multiply 5V by 0.713 then it is 3.57 so that's why I said 731 might be 3.57V and if you multiply 123 (max display value) by 0.713 it is 88 so on 3 digits you might display "088".

HOWEVER if life is really this simple (what is known as a linear relation) then you wouldn't need the lookup table at all. While it's true it saves you doing some maths the fact is tat if you can do the sums you could always convert the 0..1023 direct to display value anyway. If 1023 is the max (5V) reading from the ADC and it then display the max "123" on the digits then for any other reading you would simply multiply by 123/1024 that is 0.12. For example 731 * 0.12 is 88.

Only thing is that multiplying by 0.12 is "tricky" in Asm. Another approach is to recoginse the reciprocal of 0.12 and that multiplying by something is the same as dividing by its reciprocal. So the reciprocal is 0.12 is 8.3. Therefore 731 / 8.3 is also a route to (rounded up) 88. Now it could be that you don't need the total accuracy implied by the additional 0.3 in this. So it might be that 731 / 8 = 91 is accurate enough? If it were the case then life just got easy as /8 is the same as shift right 3 places.

However to avoid the (potentially complex) maths perhaps it is better to precalculate all the results and build a table of display values which is where you came in ;-)

Which AVR is this - can it dedicate 3K of flash to a complete lookup table?