2021-06-14 10:04 AM
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)
2021-06-14 10:26 AM
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
2021-06-14 10:31 AM
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.
2021-06-14 10:41 AM
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.
First bat_level was declared inside function, it give same results, when made it global, after that volatile, no results...
2021-06-14 10:46 AM
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
2021-06-14 11:08 AM
> 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.
2021-06-14 12:20 PM
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.
2021-06-15 08:55 PM
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
2021-06-15 08:57 PM
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.
2021-06-17 12:13 PM
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.
This is with high optimization, no matter what I do, it allays does that...
with no optimization works just fine !
So where is my mistake ?