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 support@ccsinfo.com

Variable initialization error
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ozturkhuseyin



Joined: 26 Jan 2024
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 2:00 am     Reply with quote

Ttelmah wrote:
First, this:
Code:

read_adc();
DEBUG("|V1: %.3lf |C1: %.3lf |APP1: %.3lf |VAR1: %.3lf |PHI1: %.3lf |EN1: %.8f |ACTIVE1: %.3lf\n", vL1 / 11558.0 , cL1 / 112388.0, appL1 / 151.50181, varL1 / 151.50181, pfL1 / 1.0, energyL1 / 1.0, activeL1 / 151.50181);


Makes no sense at all. You are calling the read_adc function, and not storing
the result from this.

Then do what has already been suggested. Change your debug after the
EEPROM read, and display both the en1 value, and the energyL1 value.

Then post your ReadFloatEepromBackup function.

You also have not answered the question about whether you are using
multiple compilation units?.

Do you have access to a slightly older compiler?. 5.104, was one where
there was a maths problem.


I am storing values in read_sensors() function. Here my ReadFloatEepromBackup function:
Code:

float ReadFloatEepromBackup(__EE_ADDR__ EE_Adr)
{
   long i;
   float data;
   for(i=0;i<4;i++)
      *((int8*)&data + i) = ReadEepromBackup(i + EE_Adr);
   
   return(data);
}


What do you mean by saying "multiple compilation units" ? I have libraries that i wrote in my code with including them.

I am going to change my debug printfs and test again today.
ozturkhuseyin



Joined: 26 Jan 2024
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 4:50 am     Reply with quote

Tested values with changed printf statements, its same.

Code:

    float en1 = 0.0;
    float en2 = 0.0;
    float en3 = 0.0;
   
    //en1 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY1_0);
    en2 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY2_0);
    en3 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY3_0);
   
    energyL1 = en1;
    energyL2 = en2;
    energyL3 = en3;


I commented en1 initialization with eeprom read, i can read energyL1 correct but i have to read last values from eeprom to system work correct.
ozturkhuseyin



Joined: 26 Jan 2024
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 6:36 am     Reply with quote

I solved this issue by doing this but i dont know how it solved

Code:

    float en1 = 0.0;
    float en2 = 0.0;
    float en3 = 0.0;
    float temp = 0.0;
   
    en1 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY1_0);
    en2 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY2_0);
    en3 = ReadFloatEepromBackup(EE_ADDR_INDEX_ENERGY3_0);

    // if i dont multiply by 2.0 and div by 2.0 it doesnt work
    temp = en1 * 2.0;
    energyL1 = temp / 2.0;
   
    temp = en2 * 2.0;
    energyL2 = temp / 2.0;
   
    temp = en3 * 2.0;
    energyL3 = temp / 2.0;


also if i multiply and div by 1.0 its doesnt work.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 7:39 am     Reply with quote

On multiple compilation units, this is when you use the compiler with
separate compilation units, so have a compiled 'library' that is linked in.
Problem is that if the referencing between the compilation units is not
done correctly, you can end up with the same variable in two different
units, not being actually the same variable in memory.
Glad you are not using these, but it is wrong really to refer to your
include files as 'libraries', if you are not.

Now, my suspicion is that you have some hidden defect in one of your
routines that is giving the problem. Either a pointer that is resulting
in a memory corruption somewhere or a routine accessing the wrong
variable or variable type.
You need to be doing genuine debugging to narrow down what/where
the problem actually is. For example, where you currently read the
EEPROM, write a fixed value into each variable instead. What happens?.
Your posting 'bits', thinking these may be where the problem is, does not
allow us to really help.
temtronic



Joined: 01 Jul 2010
Posts: 9116
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 9:57 am     Reply with quote

We really need to see a complete, small program.Something that will compile on our computers.

I'd get rid of the 'groups of three', only use ONE channel ,ONE set of 'math',print ONE result. I'm assuming this is a 3 phase data collection/display program but for testing purposes use only one phase and concentrate on getting it right.
Whenever there is a calculation, print/display the result not just the final number. Use the raw ADC value, play 'computer' and see IF each of the steps of the 'math' IS the correct value.

Also , WHAT ADC are you using to capture the original raw data ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Thu Feb 01, 2024 10:43 am     Reply with quote

Also, you have still not updated any of your %f formats. You seem to be
misunderstanding how C %f formats work, and I know that having a leading
blank did cause issues with some compilers.

You have also not answered why you are calling read_adc, and throwing
the value this returns away.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Fri Feb 02, 2024 7:34 am     Reply with quote

He is reading from an I2C power monitor chip.
However there is a glaring potential problem. I have a nasty suspicion
that he may be calling the function to talk to this, 'read_adc'. Ignoring
that the system has a function with the same name to read the processor
adc. This may be causing disaster.
temtronic



Joined: 01 Jul 2010
Posts: 9116
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Feb 02, 2024 8:07 am     Reply with quote

I'm just really curious, as I've never used an external ADC that reported back data in floating point format. It infers it's a bipolar ADC, and the ones I used( AD ) didn't return a fp number.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Fri Feb 02, 2024 8:11 am     Reply with quote

Have to agree wholheartedly. If it does, it won't be in Microchip
FP format, but IEEE.
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Fri Feb 02, 2024 10:28 am     Reply with quote

I haven't used this particular 'mains power' IC but I have used similar units. The ones I've been exposed to will often call what a register holds a 'float' value but it's actually a scaled integer. No sign bit, mantissa, nor exponent.

Indeed a big potential for disaster if that's what is going on here.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Fri Feb 02, 2024 12:40 pm     Reply with quote

That makes total sense. If so a huge potential error area. Handling this
needs very careful coding because of the size differences of the PIC
variables.
He needs to post how he reads this. Though he says this works, I can
see potential problems when dealing with the arithmetic to combine it
with an existing value.
temtronic



Joined: 01 Jul 2010
Posts: 9116
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 03, 2024 8:18 am     Reply with quote

The sad thing is ,everyone here is trying to help the guy and if he'd post the what the external 'ADC device' actually is, I'm sure someone would cut basic test code that would read one channel of the chip,do the 'math', and properly display it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Sat Feb 03, 2024 12:23 pm     Reply with quote

The big problem is that the poster has seen that changing the load of the
data apparently makes the problem appear, so is concentrating on this,
without realising that changing this alters dozens of other things.
Alters the size of the code, moves variables, changes the range of numbers
that are actually present in the variables involved, etc. etc..

It is giving him a blindness to the possibility that other parts are involved.
This is why I suggested loading some different values at this point (would
allow seeing if the range of values is what triggers the issue, which is a
potential first step).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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