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

User avatar
fotios
Posts: 428
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 98 times
Been thanked: 101 times
Contact:

LCD prints erroneously

Postby fotios » Fri Apr 05, 2019 7:11 am

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: 602
Joined: Wed May 31, 2017 11:57 am
Has thanked: 56 times
Been thanked: 334 times
Contact:

Re: LCD prints erroneously

Postby mnf » Fri Apr 05, 2019 7:44 am

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
These users thanked the author mnf for the post:
fotios (Sat Apr 06, 2019 9:30 pm)
Rating: 5.26%
 

User avatar
fotios
Posts: 428
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 98 times
Been thanked: 101 times
Contact:

Re: LCD prints erroneously

Postby fotios » Fri Apr 05, 2019 8:00 am

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: 428
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 98 times
Been thanked: 101 times
Contact:

Re: LCD prints erroneously

Postby fotios » Fri Apr 05, 2019 8:33 am

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:
These users thanked the author fotios for the post:
Benj (Fri Apr 05, 2019 9:45 am)
Rating: 5.26%
 
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 428
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 98 times
Been thanked: 101 times
Contact:

Re: LCD prints erroneously

Postby fotios » Sat Apr 06, 2019 8:40 am

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: 8479
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2435 times
Been thanked: 3491 times
Contact:

Re: LCD prints erroneously

Postby medelec35 » Sat Apr 06, 2019 11:25 am

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
These users thanked the author medelec35 for the post:
fotios (Sat Apr 06, 2019 4:09 pm)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
fotios
Posts: 428
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 98 times
Been thanked: 101 times
Contact:

Re: LCD prints erroneously

Postby fotios » Sat Apr 06, 2019 8:19 pm

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: 8479
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2435 times
Been thanked: 3491 times
Contact:

Re: LCD prints erroneously

Postby medelec35 » Sat Apr 06, 2019 8:34 pm

You're welcome.
Just glad you have it working now.
Thanks for the update.
These users thanked the author medelec35 for the post:
fotios (Sat Apr 06, 2019 9:29 pm)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.