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

datatype problem

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

datatype problem
PostPosted: Fri Dec 06, 2013 10:25 pm     Reply with quote

Hi,
I'm using PIC18F2520, CCS C Compiler, Version: 4.114, Oscillator: 2Mhz

Reading eeprom values, but the values are wrong. Why is it so?

reading a 16 bit number and saving it in float. It creates problem. Why is it?
sample program:
Code:
#include "18F2520.h"
#fuses INTRC_IO
#use delay(clock = 2000000)

#define LOW_CAL_ADC_ADDRESS      20
#define LOW_CAL_DISP_ADDRESS   30
#define HIGH_CAL_ADC_ADDRESS   40
#define HIGH_CAL_DISP_ADDRESS   50

float adc_span;
float disp_span;
float leastcount;

#define INT_EEPROM_ADDRESS  int8

void write_int16_eeprom(INT_EEPROM_ADDRESS address, int16 data);
int16 read_int16_eeprom(INT_EEPROM_ADDRESS address);


// Purpose:    Write a 16 bit number to internal eeprom
// Inputs:     1) An eeprom address
//             2) The 16 bit number to write to internal eeprom
// Outputs:    None
void write_int16_eeprom(INT_EEPROM_ADDRESS address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *((int8 *)(&data) + i));
   }
}


// Purpose:    Read a 16 bit number from internal eeprom
// Inputs:     An eeprom address
// Outputs:    The 16 bit number read from internal eeprom
int16 read_int16_eeprom(INT_EEPROM_ADDRESS address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *((int8 *)(&data) + i) = read_eeprom(address + i);
   }

   return(data);
}

void main()
{
   write_int16_eeprom(LOW_CAL_ADC_ADDRESS, 3000);
   write_int16_eeprom(HIGH_CAL_ADC_ADDRESS, 23000);

   write_int16_eeprom(LOW_CAL_DISP_ADDRESS, 9000);
   write_int16_eeprom(HIGH_CAL_DISP_ADDRESS, 0);

   
   adc_span = read_int16_eeprom(HIGH_CAL_ADC_ADDRESS) - read_int16_eeprom(LOW_CAL_ADC_ADDRESS);

   disp_span = read_int16_eeprom(HIGH_CAL_DISP_ADDRESS) -   read_int16_eeprom(LOW_CAL_DISP_ADDRESS);

   leastcount = disp_span/adc_span;

   while(1)
   {
      
   }
}

Read the values in watch window using mplab,
values are,
adc_span = 20000.0000
disp_span = 56536.0000 // wrong value...
leastcount = 2.82680011
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 07, 2013 12:11 am     Reply with quote

Quote:
Reading eeprom values, but the values are wrong. Why is it so?

Learn to trouble-shoot the problem. Use printf to display intermediate
results in the math formula.

Quote:
reading a 16 bit number and saving it in float. It creates problem.

That's an assumption. Load the result into an int16 value and display
that result with printf. What do you get ?
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Sat Dec 07, 2013 12:21 am     Reply with quote

Quote:
Learn to trouble-shoot the problem

changed the code to
Code:
disp_span = (float)read_int16_eeprom(HIGH_CAL_DISP_ADDRESS) -   (float)read_int16_eeprom(LOW_CAL_DISP_ADDRESS);


value on mplab, disp_span = -9000.
Looks good.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Dec 07, 2013 2:03 am     Reply with quote

It is worth understanding what was wrong.

An 'int' or 'int16', is an _unsigned_ value in CCS.

0-9000 is a _negative_ result. Impossible in an unsigned.
Since integer arithmetic 'wraps', 0-9000 will give 65536-9000 = 56536.

Best Wishes
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