cancel
Showing results for 
Search instead for 
Did you mean: 

Compiler Errors, what is best practice to write code (when if(1==1) is false) )

Linas L
Senior II

Hello,

Many times i get into problems that I should not get into, like simple logical mistakes while executing code:

examples:

uint32_t a = 1;
if(a==1)
{
    MCU goes here, it's how it should be;
}
else
{
  MCU goes also here , how is this possible?;
}

MY latest problem I was generating string from battery voltage;

bat_level = ADC_GET_VOLTAGE();
  if(bat_level>4200);
  {
    bat_level=4200;
  }
  if(bat_level<3000)
  {
    bat_level=3000;
  }
  bat_level=bat_level-3000;
  bat_level=bat_level*100;
  bat_level=bat_level/1200;

And I always get 100%, even if my voltage is 3300 ( 1bit = 1mV)

And when debugging this part of code showed me why, I always make if compare and set 4200mV !!!

How is this possible? What I am doing wrong. (new iar arm compiler 9.10.2 with optimization on)

0693W00000BawqVQAR.png

9 REPLIES 9
JPeac.1
Senior

The ADc for VBAT is ratiometric, meaning it reports a ratio compared to VREF voltage reference input. You can verify the VREF voltage by sampling the VREFINT internal voltage reference, which normally runs around 1.12 volts (check datasheet for specific part reading). Also, check your board to see what voltage is connected to the VREF+ pin.

You have a semicolon at the end of your if statement. It always forces bat_level to change to 4200.

Jack Peacock

TDK
Guru

The best defense against obviously incorrect but technically compileable code is an IDE that detects these types of mistakes.

Both CLion and STM32CubeIDE detected your syntax was not intended.

0693W00000BawzXQAR.png

If you feel a post has answered your question, please click "Accept as Solution".

It's already converted to correct volatge by DMA_TC interrupt callback. so it is down to 1mV precision as with multi-meter reading

All ADC_GET_VOLTAGE() does is return value of global variable inside adc program that runs on TC IRQ's

Problem is with scaling logic not been corectly executed based on what I write. it works ok with zero optimization.

0693W00000Bax11QAB.png First bat_level was declared inside function, it give same results, when made it global, after that volatile, no results...

Ok, now I see the problem. simple mistype after if() ;

After complete day of coding you start to miss mistakes like that.

But I had problems when compiler simply makes my code non functional, while logic dictates it should work. l

> But I had problems when compiler simply makes my code non functional, while logic dictates it should work. l

There's usually an explanation for compiler errors and extremely rarely is it a compiler bug.

If you feel a post has answered your question, please click "Accept as Solution".
Piranha
Chief II

Take into account the fact that Li-Ion battery is at least somewhat linear in a 4,15..3,45V range. Outside that range it's very non-linear and holds only a tiny amount of energy anyway. Also remenber that LDO also has some voltage drop. Does your device actually work below 3,3V? And even if it does, if the VREF+/VDDA has the same level the VDD has, ADC/DAC will have wrong values.

HI,

Device works on 1.8V, so if I have ~2V, MCU will be able to work. don't worry about this. And yes, I should get discharge curve for my setup to get corret capacity value out of voltage, since right now it's just is indication

Ususally it's optimization problem and inability to see that I use variable that looks to be unused. Last time it happened was with timer, i was unable to read TIM2->CNT register to variable, it always stayed 0. Reason, it was optimized out, while it was used to extend 16b counter overflow for 32b emulation.

With it was working with no optimization.

Linas L
Senior II

This is another example. Even if status is 0, and I can check it with debugger, it always goes inside if true condition, and updates my flash from ram.

0693W00000BbJ3HQAV.pngThis is with high optimization, no matter what I do, it allays does that...

with no optimization works just fine !

0693W00000BbJ3vQAF.pngSo where is my mistake ?