ATmega328 Lookup table

Stuck with a problem in your code? Seek help here.

ATmega328 Lookup table

Postby budy500000 » March 5th, 2014, 12:01 am

i am having a problem in this segment of code i am using avr studio6 and a lcd to debug my code. i am getting an unexpected result does anyone see where i may have gone wrong?

index always =11 when (intuvoltage>=lookuptable[index]) ??????????????????
intuvoltage seems to work and varies between 856 and 1000 biased on temperature and is correctly reported to the lcd
lookuptable[11]=5215 and seems to be correct
lookuptable[index]=39 or 40??????????????????????

the function is called by
Code: Select all
uint16_t x;
x=ADCtoTemp(rawADCtemp1, 1, 0, 10, 111.23, 2.5);



Code: Select all
#ifndef Thermocouple_H
#define Thermocouple_H

#include <avr/pgmspace.h>   //used to store the lookup table in flash instead of ram


uint16_t ADCtoTemp(uint16_t rawADC, uint8_t degrees, double foffset, uint16_t ADCres, double ADCgain, double ADCvref);



//rawADC is the avraged 10bit adc reading, degrees 1==F 0==C, ADCoffset in f due to powersupply and diffrences in thermocouples (gain is currently about av=111.23)
uint16_t ADCtoTemp(uint16_t rawADC, uint8_t degrees, double foffset, uint16_t ADCres, double ADCgain, double ADCvref)
{
   // lookup table for k type thermocouple held in flash memory 250F-910F in 1 degree increments represented in micro volts
   static const PROGMEM uint16_t lookuptable[661] =
   {
      4965, 4988, 5011, 5034, 5056, 5079, 5102, 5124, 5147, 5170, 5192, 5215, 5238, 5260, 5283, 5306, 5328, 5351, 5374, 5396, 5419, 5441, 5464, 5487, 5509, 5532, 5554, 5577, 5599, 5622, 5644, 5667, 5690, 5712, 5735, 5757, 5779, 5802, 5824,

      5847, 5869, 5892, 5914, 5937, 5959, 5982, 6004, 6026, 6049, 6071, 6094, 6116, 6138, 6161, 6183, 6205, 6228, 6250, 6272, 6295, 6317, 6339, 6362, 6384, 6406, 6429, 6451, 6473, 6496, 6518, 6540, 6562, 6585, 6607, 6629, 6652, 6674, 6696,

      6718, 6741, 6763, 6785, 6807, 6829, 6852, 6874, 6896, 6918, 6941, 6963, 6985, 7007, 7029, 7052, 7074, 7096, 7118, 7140, 7163, 7185, 7207, 7229, 7251, 7273, 7296, 7318, 7340, 7362, 7384, 7407, 7429, 7451, 7473, 7495, 7517, 7540, 7562,

      7584, 7606, 7628, 7650, 7673, 7695, 7717, 7739, 7761, 7783, 7806, 7828, 7850, 7872, 7894, 7917, 7939, 7961, 7983, 8005, 8027, 8050, 8072, 8094, 8116, 8138, 8161, 8183, 8205, 8227, 8250, 8272, 8294, 8316, 8338, 8361, 8383, 8405, 8427,

      8450, 8472, 8494, 8516, 8539, 8561, 8583, 8605, 8628, 8650, 8672, 8694, 8717, 8739, 8761, 8784, 8806, 8828, 8851, 8873, 8895, 8918, 8940, 8962, 8985, 9007, 9029, 9052, 9074, 9096, 9119, 9141, 9163, 9186, 9208, 9231, 9253, 9275, 9298,

      9320, 9343, 9365, 9388, 9410, 9432, 9455, 9477, 9500, 9522, 9545, 9567, 9590, 9612, 9635, 9657, 9680, 9702, 9725, 9747, 9770, 9792, 9815, 9837, 9860, 9882, 9905, 9927, 9950, 9973, 9995, 10018, 10040, 10063, 10086, 10108, 10131, 10153,

      10176, 10199, 10221, 10244, 10267, 10289, 10312, 10334, 10357, 10380, 10402, 10425, 10448, 10471, 10493, 10516, 10539, 10561, 10584, 10607, 10629, 10652, 10675, 10698, 10720, 10743, 10766, 10789, 10811, 10834, 10857, 10880, 10903,

      10925, 10948, 10971, 10994, 11017, 11039, 11062, 11085, 11108, 11131, 11154, 11176, 11199, 11222, 11245, 11268, 11291, 11313, 11336, 11359, 11382, 11405, 11428, 11451, 11474, 11497, 11519, 11542, 11565, 11588, 11611, 11634, 11657,

      11680, 11703, 11726, 11749, 11772, 11795, 11818, 11841, 11864, 11887, 11910, 11933, 11956, 11978, 12001, 12024, 12047, 12070, 12093, 12116, 12140, 12163, 12186, 12209, 12232, 12255, 12278, 12301, 12324, 12347, 12370, 12393, 12416,

      12439, 12462, 12485, 12508, 12531, 12554, 12577, 12600, 12624, 12647, 12670, 12693, 12716, 12739, 12762, 12785, 12808, 12831, 12855, 12878, 12901, 12924, 12947, 12970, 12993, 13016, 13040, 13063, 13086, 13109, 13132, 13155, 13179,

      13202, 13225, 13248, 13271, 13294, 13318, 13341, 13364, 13387, 13410, 13433, 13457, 13480, 13503, 13526, 13549, 13573, 13596, 13619, 13642, 13665, 13689, 13712, 13735, 13758, 13782, 13805, 13828, 13851, 13874, 13898, 13921, 13944,

      13967, 13991, 14014, 14037, 14060, 14084, 14107, 14130, 14154, 14177, 14200, 14223, 14247, 14270, 14293, 14316, 14340, 14363, 14386, 14410, 14433, 14456, 14479, 14503, 14526, 14549, 14573, 14596, 14619, 14643, 14666, 14689, 14713,

      14736, 14759, 14783, 14806, 14829, 14853, 14876, 14899, 14923, 14946, 14969, 14993, 15016, 15039, 15063, 15086, 15109, 15133, 15156, 15179, 15203, 15226, 15250, 15273, 15296, 15320, 15343, 15366, 15390, 15413, 15437, 15460, 15483,

      15507, 15530, 15554, 15577, 15600, 15624, 15647, 15671, 15694, 15717, 15741, 15764, 15788, 15811, 15834, 15858, 15881, 15905, 15928, 15952, 15975, 15998, 16022, 16045, 16069, 16092, 16116, 16139, 16163, 16186, 16209, 16233, 16256,

      16280, 16303, 16327, 16350, 16374, 16397, 16421, 16444, 16468, 16491, 16514, 16538, 16561, 16585, 16608, 16632, 16655, 16679, 16702, 16726, 16749, 16773, 16796, 16820, 16843, 16867, 16890, 16914, 16937, 16961, 16984, 17008, 17031,

      17055, 17078, 17102, 17125, 17149, 17173, 17196, 17220, 17243, 17267, 17290, 17314, 17337, 17361, 17384, 17408, 17431, 17455, 17478, 17502, 17526, 17549, 17573, 17596, 17620, 17643, 17667, 17690, 17714, 17738, 17761, 17785, 17808,

      17832, 17855, 17879, 17902, 17926, 17950, 17973, 17997, 18020, 18044, 18068, 18091, 18115, 18138, 18162, 18185, 18209, 18233, 18256, 18280, 18303, 18327, 18351, 18374, 18398, 18421, 18445, 18469, 18492, 18516, 18539, 18563, 18587,

      18610, 18634, 18657, 18681, 18705, 18728, 18752, 18776, 18799, 18823, 18846, 18870, 18894, 18917, 18941, 18965, 18988, 19012, 19035, 19059, 19083, 19106, 19130, 19154, 19177, 19201, 19224, 19248, 19272, 19295, 19319, 19343, 19366,

      19390, 19414, 19437, 19461, 19485, 19508, 19532, 19556, 19579, 19603, 19626, 19650, 19674, 19697, 19721, 19745, 19768, 19792, 19816, 19839, 19863, 19887, 19910, 19934, 19958, 19981, 20005, 20029, 20052, 20076, 20100, 20123
   };
   
   uint16_t result=250;
   double uvoltage =rawADC*((ADCvref / pow(2,ADCres))/ADCgain*1000000);   //uvoltage holds value in micro volts
   uint16_t intuvoltage=(uint16_t)(uvoltage);//truncate into an integer to speed things up a bit
   
   uint16_t index;
   for (index=0;index<660;index++)   //look threw table
   {
      if (intuvoltage>=lookuptable[index])            
      {

         char debug[10];   //initialize string
         dtostrf(index,7,1,debug);
         setCursor(0,0);
         wrightString(debug);   //always prints out 11.0 on the lcd????????????????????????????????????????????????
         
         dtostrf(intuvoltage,7,1,debug);
         setCursor(0,8);
         wrightString(debug);   //prints out tc voltage in micro volts on the lcd seems ok 856.0 at room temp, 1000.0 at hot breath
         
         dtostrf(lookuptable[11],7,1,debug);
         setCursor(1,0);
         wrightString(debug);   //prints out 5215.0 from lookup table on the lcd
         
         dtostrf(lookuptable[index],7,1,debug);
         setCursor(1,8);
         wrightString(debug);   //prints out 39.0 or 40.0 on a whim on the lcd?????????????????????????????????????????????????????????????????
         
         result=index+250;
         break;
      }
   }
   

   
   return result;
}



#endif
budy500000
 
Posts: 4
Joined: January 7th, 2012, 12:30 pm

Re: ATmega328 Lookup table

Postby Adam » March 11th, 2014, 12:13 pm

I haven't been working with AVR for long, but it looks like you're setting your debug variable to the index itself instead of the lookup value AT that index.

Code: Select all
dtostrf(index,7,1,debug);

should probably be

Code: Select all
dtostrf(lookuptable[index],7,1,debug);
Adam
 
Posts: 3
Joined: March 11th, 2014, 12:07 pm


Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 3 guests