CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Simple thing but problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Requan



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

Simple thing but problem
PostPosted: Fri Jan 16, 2015 11:36 am     Reply with quote

Dear CCS Member,
I wrote program to read temperature from PT1000 via external ADC.
Code:
const float ResistanceTable[]=
{
   803.06,
   842.71,
   882.22,
   921.6,
   960.86,
   1000,
   1039.02,
   1077.92,
   1116.71,
   1155.39,
   1193.95,
   1232.39,
   1270.71,
   1308.92,
   1347.02,
   1385,
   1422.86,
   1460.6,
   1498.23,
   1535.75,
   1573.1
};


float get_res_x()
{
   int16 upper, lower, config, ans;
   i2c_start();     
   i2c_write (0xD0);  // 1101 0000 -address byte write  (SELECT ADDRESS  A0  AND SET IN WRITE MODE) FOR MCP3421A0           
   i2c_write (0x88);  // ADC Gain 1
   i2c_stop();     
   i2c_start ();
   i2c_write (0xD1);  // 1101 0001 - CHANGE TO READ MODE  (ADDRESS A0)   FOR MCP3421A0     
   upper = i2c_read (1); // read slave data
   lower = i2c_read (1); // read slave data     
   config = i2c_read (0); // read slave data     
   i2c_stop ();
   ans =( upper * 256) + lower;
   
   if(ans>32767 )
   {
      ans=0;
   }   
   
   return ans;
}


float CalcPT1000(int32 AdcRes)
{
   float PtR, HiR, LoR;
   float LoTemp;
   int8 i;
   PtR = (2.048f / 32768.0f) * (float)AdcRes;
   PtR *= 1000;
   
   for(i=0; i<22; i++)
   {
      if(ResistanceTable[i]>PtR)
      {
         HiR = ResistanceTable[i];
         LoR = ResistanceTable[i-1];
         LoTemp = (i-6) * 10;
         break;
      }
   }
   PtR -= LoR;
   PtR /= (HiR - LoR);
   PtR *= 10;
   PtR += LoTemp;
   
   return (float)(PtR*10);
}

It worked fine, but when i put data to int16 table index 1, example:
Code:

 adc_val = get_res_x();
 valuemodbus[1] = (int16)CalcPT1000(adc_val);

For first measure data is in index1, for second is on index 2, so in valuemodbus[2].
I think i made forbidden think, cause to weird table behavior.
Could You look at code and write me any suggestion?
Martin
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jan 16, 2015 2:22 pm     Reply with quote

Your two line sample code at the end does not provide enough information to see what is going wrong and I could not understand your description of the problem.

For example, where is the declaration of the array valuemodbus[] ?

In C arrays start at offset 0 therefore the first array element of your array would be valuemodbus[0]
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Jan 16, 2015 2:24 pm     Reply with quote

I may be missing it (it has been one of those days today), but where is the array "valuemodbus[]" defined??

mikey

[edit] I see Andrew posted while I was posting :-)
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Requan



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

PostPosted: Sat Jan 17, 2015 4:37 am     Reply with quote

sorry, i put piece of code i have problem and forgot to paste declaration, here it is:
Code:
int16 valuemodbus[10];
int32 adc_val;


i tried also as int32 but no help.

Now, i am wondering if compiler can do this conversion in proper way:
Quote:
(int16)CalcPT1000(adc_val);

maybe i should to do it by union?
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Sat Jan 17, 2015 4:47 am     Reply with quote

A union can't help with this, and 'yes', the compiler will convert a float to an integer for you.
I'd suspect the value you are getting from your function is not what you think it is. Step back, and get rid of all the maths, and display the number you are actually reading from the chip.
If this isn't what you expect, your maths will then not work...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group