LCD prints erroneously

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Contact:

LCD prints erroneously

Post by fotios »

Hi everyone

I have a Uint variable: A_UINT
I have a multiplier that gets values from 1000d - 65535d: GAIN
I have a floating point variable: A_FLP
I have a string type variable: A_STR
I use the following calculation:

Code: Select all

A_FLP = (A_UINT * GAIN * 0.001) / 32768
Then the A_FLP is converted to string for printing on LCD display:

Code: Select all

A_STR = FloatToString$ (A_FLP,3)
In FC7 simulation is calculated and printed correctly for any value of GAIN.
On the actual LCD of application is printed correctly for GAIN values from 32768 and above.
For GAIN values from 32767 and below the LCD prints 0.000.
To simplify the calculation:

Code: Select all

A_FLP = A_UINT * 0.001 * (GAIN / 32768)
If "GAIN / 32768" is greater than 1, it prints correctly.
If "GAIN / 32768" is less than 1 (e.g. 0.999) it prints "0.000"
Why this?
The application micro is P16F18877.

Thanks
Last edited by fotios on Fri Apr 05, 2019 7:49 am, edited 1 time in total.
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 795
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: LCD prints erroneously

Post by mnf »

Hi Fotis,

I'd suspect some over flow errors are occurring. Try changing your variables to 32 Bit - 16 bit only allow 0...65535' but in think that the simulation works with 32 bit values.

Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Contact:

Re: LCD prints erroneously

Post by fotios »

mnf wrote:Hi Fotis,

I'd suspect some over flow errors are occurring. Try changing your variables to 32 Bit - 16 bit only allow 0...65535' but in think that the simulation works with 32 bit values.

Martin
Hi Martin and thanks for the reply.
How I could do that?
The floating point variable is standard and the "A_FLP" is defined within FC as floating point type: "R"
The A_STR is defined within FC as String type A_STR[20]: "S"

Thanks

Fotis
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Contact:

Re: LCD prints erroneously

Post by fotios »

Just now I realized that the problem arises from the division by 32768.
To explain:
Let's suppose:

Code: Select all

A_FLP = (A_UINT * 32767 * 0.001) / 32768
Which is the same with:

Code: Select all

A_FLP = (A_UINT * 0.001) * (32767 / 32768)
If I place:

Code: Select all

A_FLP = A_UINT * 0.001 * 0.9999
Then the actual display prints correctly.

:? :roll:
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Contact:

Re: LCD prints erroneously

Post by fotios »

I finally solved the problem by removing the division. I make the division with the calculator and then put the result to be multiplied on the formula of Gain calculation. It is not a problem, because is calculated one time and then is stored permanently.
However, it remains a problem with the ATM90E32 registers.
From the datasheet, I have understood that are 16-bit for UINT type variables.
Nevertheless, within the application document is referred to as an example of Ugain variable calculation resulting in 58395d:
"So the register can be set to E41Bh"
If the register is 16-bit wide, that makes E41Bh = -7141d and not 58395d.
Within datasheet is also referred that all Gain registers after reset are by default = 8000h and that makes -32768d.
I'm really confused by the Windows calculator, which I use for the conversions of decimals to HEX and vice versa. In Word format, it prints the negative complements of decimals. In DWord format, it prints correctly.
Best Regards FOTIS ANAGNOSTOU

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8925
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: LCD prints erroneously

Post by medelec35 »

Hi fotios one important thing to remember is :
A value in a calculations involving floats e.g 32768 are treated as integers.
You need to cast it to a float by adding a real portion i.e always as .0 at the end.

So I would do

Code: Select all

A_FLP = (A_UINT * 32767.0 * 0.001) / 32768.0
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Contact:

Re: LCD prints erroneously

Post by fotios »

medelec35 wrote:Hi fotios one important thing to remember is :
A value in a calculations involving floats e.g 32768 are treated as integers.
You need to cast it to a float by adding a real portion i.e always as .0 at the end.

So I would do

Code: Select all

A_FLP = (A_UINT * 32767.0 * 0.001) / 32768.0
Hi Martin
Thanks a lot, you saved my life :D
Indeed, the small detail it was this ".0" at the end of 32767.
I had no idea that both literals should be taken as floating point in the division.
Now the formula works very nice.
Thanks again
Best Regards FOTIS ANAGNOSTOU

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8925
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: LCD prints erroneously

Post by medelec35 »

You're welcome.
Just glad you have it working now.
Thanks for the update.
Martin

Post Reply