On Sun, 21 Mar 2010, yamanoor sairam wrote: > Hello Sergio, > Thanks for your mail. > > Should the above read > > Could this be a problem with my code? > > I did the following steps in my code using MikroC complier: > > adc_rd = adc_read(0); // read voltage from AN0 > > t1 = 900-(float)(1.0625*adc_rd);//t1 is declared as float. I am doing this subtraction because my opamp gives an output of 0V for 900mV and 4.139V for 0mV. > > t3long = (long)t1long; > > //I am proceeding to LCD display section thereafter. > > Should the above read > > t1 = 900 - (float)(adc_rd) * 1.0625; > t3long = (long)t1; > > > The second line was a typo. Am I wrong in the first line? Think about it. If you NEED to convert "1.0625*adc_rd" to a float AFTER the multiplication, then you are going to have problems. You shouldn't "need" to convert adc_rd to a float because the compiler should do that automatically for you before the multiplication because you are multiplying an integer with a float. The problem is I don't know how well behaved the MikroC complier is (I have not used it) when it comes to promoting integers to floats in conjunction with floating point constants. I've seen lots of issues in the past with other compilers so I would play safe and rewrite it as I've shown (and even then I wouldn't like to guarentee it). I haven't done the math but you should check yours (that adc_rd * 1.0625 cannot produce a result greater than 900) otherwise you'll get negative numbers. Also something else that comes to mind, is adc_rd a 16 bit integer variable or an 8 bit integer variable and does adc_read(0) return a 16 or 8 bit value? If adc_read(0) returns a 16 bit value and adc_rd is an 8 bit integer, you could be seeing a high value (above 255) that is dropping to zero and you are seeing it jump about because you are losing the high bits. e.g. 900 = 0x384 -> 0x84 = 132 800 = 0x320 -> 0x20 = 32 700 = 0x2bc -> 0xbc = 188 600 = 0x258 -> 0x58 = 88 500 = 0x1f4 -> 0xf4 = 244 400 = 0x190 -> 0x90 = 144 300 = 0x12c -> 0x2c = 44 200 = 0xc8 -> 0xc8 = 200 Regards Sergio Masci -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist