News:

Let's find out together what makes a PIC Tick!

Main Menu

Printing unknown float

Started by shantanu@india, Aug 23, 2022, 01:33 PM

Previous topic - Next topic

shantanu@india

I'm reading values from a multifunction meter through modbus & displaying them in 16X2 LCD...

                       
                 
If disp_page=0 Then
                            Print At 1,1,"WIFI_OK  PARAM1 "
                            Print At 2,1,"                " 
                            Print At 2,1,Dec2 param1                             
                        ElseIf disp_page=1 Then
                            Print At 1,1,"WIFI_OK  PARAM2 "
                            Print At 2,1,"                "
                            Print At 2,1,Dec2 param2
There are seven display pages in all displaying seven float valuesover which I have no control.
The reason for the middle print command that prints sixteen blanks is to ensure that the second line is cleared & does not have any remnant of the previous page.But this is causing the second line to flicker.
An example...If parameter1=26587.68 & parameter2=50.01 then without the middle blanking print command the second page would show "50.01.68"
Maybe I can check the parameter values(& width) before printing by a series of if statements but that doesn't seem very efficient.
How to left justify my second line & fill all the leftover spaces after the second decimal space with zero's(or blanks)?
Regards
Shantanu

shantanu@india

Solved it by adding a conditional flag

If disp_page=0 Then
                            Print At 1,1,"WIFI_OK  PARAM1 "
                            If disp_blank=1 Then
                                Print At 2,1,"                "
                                disp_blank=0
                            EndIf
                            Print At 2,1,Dec2 param1                             
                        ElseIf disp_page=1 Then
                            Print At 1,1,"WIFI_OK  PARAM2 "
                            If disp_blank=1 Then
                                Print At 2,1,"                "
                                disp_blank=0
                            EndIf
                            Print At 2,1,Dec2 param2
The disp_blank is set by the page changeover timer.
Flickering gone!!
Regards
Shantanu

Pepe

Remove
Print At 2,1,"                "

I use this

Print At 2,1,Dec2 param1,Rep " "\20

where 20 is the number of spaces that I print after the variable to be shown that can be modified according to the maximum number of characters that the screen has

shantanu@india

#3
Thanks Pepe.
I didn't know the existence of 'Rep'
I should update myself once in a while.
But the number of 'Rep' s still need to be calculated from the width to be occupied by the parameter, isn't it?
Regards
Shantanu

Pepe

If the spaces are not printed in the next line, you can put a value similar to the maximum number of characters on the display or more according to the type of font.

shantanu@india

Regards
Shantanu

top204

#6
The procedure listed below will display a string's contents and fill the remainder of the line on an alphanumeric LCD with blanks. It first gets the length of the string passed to it, then subtracts that from 16 to get the remaining amount of characters on the LCD's line and fills them with spaces using the Rep modifier.

The demo listing below fills line 1 of the LCD with text, then waits for 1 second and displays new text with blanks, in order to remove the original text's remnants:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a procedure to display on an alphanumeric LCD, and add blank spaces following any text to remove any remnant characters.
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                                   ' Tell the compiler what device to compile for
    Declare Xtal = 16                                   ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup the Alphanumeric LCD
'
    Declare LCD_DTPin = PORTA.0                         ' The LCD's Dt4 to Dt7 lines connect to PORTA.0 to PORTA.3
    Declare LCD_ENPin = PORTA.4                         ' Connects to the LCD's EN line
    Declare LCD_RSPin = PORTA.5                         ' Connects to the LCD's RS line
    Declare LCD_Interface = 4                           ' The LCD is going to use a 4-bit interface
    Declare LCD_Lines = 2                               ' The LCD has 2 lines
    Declare LCD_Type = Alphanumeric                     ' The LCD is an Hitachi alphanumeric type
'
' Create some variables for the demo
'  
    Dim MyWord  As Word = 12
    Dim MyFloat As Float = 3.14

'-------------------------------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Cls                                                 ' Clear the LCD's display
   
    Print At 1, 1, "FILLED WITH TEXT"                   ' Fill line 1 of the LCD with text
    DelayMS 1000                                        ' Wait for 1 second
    PrintWithBlanks(1, "Hello " + Str$(Dec, MyWord) + ":" + Str$(Dec2, MyFloat))' Display text on line 1 of the LCD and blank the rest of the line

'-------------------------------------------------------------------------------------------------------
' Display a string on the LCD with spaces after it to fill the line
' Input     : pLine holds the line on the LCD to display the string's contents (1 or 2)
'           : pText holds the text to display on the line. Maximum 16 characters.
' Output    : None
' Notes     : None
'
Proc PrintWithBlanks(pLine As Byte, pText As String * 16)
    Print At pLine, 1, pText, Rep " "\(16 - Len(pText)) ' Place spaces after the string's text, in order to fill the line
EndProc 

To display variable values as ASCII, use the Str$ modifier, as shown in the above listing, because, remember, it is a String variable that is being assigned too, not a Print command.

The Rep modifier has been with the compiler from day one, because it was a BASIC Stamp II module modifier, so I brought it over to the compilers for compatability. It is also, sometimes, quite useful and quite efficient to use.

shantanu@india

Lovely coding indeed.
Thanks.
Regards
Shantanu

top204

#8
Many thanks for your kind words.

To make things even easier to use and understand, you can use the compiler's preprocessor and create meta-macros to replace the Str$ functions. For example, add these to the program's listing, before they are used in the program itself. Or place them inside an include file that is loaded in to the program at its start:

'
' Decimal meta-macros to make the Str$ function easier to use
'
$define StrDec(pValue) Str$(Dec,pValue) 
$define StrDec0(pValue) Str$(Dec0,pValue)
$define StrDec1(pValue) Str$(Dec1,pValue)
$define StrDec2(pValue) Str$(Dec2,pValue)
$define StrDec3(pValue) Str$(Dec3,pValue)
$define StrDec4(pValue) Str$(Dec4,pValue)
$define StrDec5(pValue) Str$(Dec5,pValue)
$define StrDec6(pValue) Str$(Dec6,pValue)
$define StrDec7(pValue) Str$(Dec7,pValue)
$define StrDec8(pValue) Str$(Dec8,pValue)
$define StrDec9(pValue) Str$(Dec9,pValue)
$define StrDec10(pValue) Str$(Dec10,pValue)
'
' Hexadecimal meta-macros to make the Str$ function easier to use
'
$define StrHex(pValue) Str$(Hex,pValue) 
$define StrHex1(pValue) Str$(Hex1,pValue)
$define StrHex2(pValue) Str$(Hex2,pValue)
$define StrHex3(pValue) Str$(Hex3,pValue)
$define StrHex4(pValue) Str$(Hex4,pValue)
$define StrHex5(pValue) Str$(Hex5,pValue)
$define StrHex6(pValue) Str$(Hex6,pValue)
$define StrHex7(pValue) Str$(Hex7,pValue)
$define StrHex8(pValue) Str$(Hex8,pValue)
'
' Binary meta-macros to make the Str$ function easier to use
'
$define StrBin(pValue) Str$(Bin,pValue) 
$define StrBin1(pValue) Str$(Bin1,pValue)
$define StrBin2(pValue) Str$(Bin2,pValue)
$define StrBin3(pValue) Str$(Bin3,pValue)
$define StrBin4(pValue) Str$(Bin4,pValue)
$define StrBin5(pValue) Str$(Bin5,pValue)
$define StrBin6(pValue) Str$(Bin6,pValue)
$define StrBin7(pValue) Str$(Bin7,pValue)
$define StrBin8(pValue) Str$(Bin8,pValue)
$define StrBin9(pValue) Str$(Bin9,pValue)
$define StrBin10(pValue) Str$(Bin10,pValue)
$define StrBin11(pValue) Str$(Bin11,pValue)
$define StrBin12(pValue) Str$(Bin12,pValue)
$define StrBin13(pValue) Str$(Bin13,pValue)
$define StrBin14(pValue) Str$(Bin14,pValue)
$define StrBin15(pValue) Str$(Bin15,pValue)
$define StrBin16(pValue) Str$(Bin16,pValue)


Then you can use the line:

PrintWithBlanks(1, "Hello " + StrHex2(MyWord) + ":" + StrDec2(MyFloat))    ' Display text on line 1 of the LCD and blank the rest of the line

It is easier to read, and easier to type in. The preprocessor is such a useful and powerful program and can make things so much easier to write and understand. It took me an awful lot of time and work to find out how preprocessors worked and write it and add it to the compilers, but it was time well spent. :-)

shantanu@india

#9
Les,
 I'll turn 58 by the turn of the year and I strongly disbelieve in pessimistic people who say grey cells never regenerate, and once you start losing your mind you've had it....
My passion to learn is what sustains me through financial and personal hardships.
My daughter is graduating in psychology and I find study of the mind a fascinating subject. I'm recently reading a book on developmental psychology which is fantastic study of the mind from the newborn to the dying patient. I feel basic psychology is a must-read subject for all.
Have you read Daniel Kahneman's "Think fast and slow"? He is a psychologist who won a nobel in economics!!
I really don't know why I spoke about psychology when you were trying to teach me how to use the preprocessor... another psychic riddle!!
Regards
Shantanu

charliecoutas

Hi Shantanu

Having somehow reached the age of 76, I must argue that the little-grey-cells don't degenerate. I now have trouble finding electronic components in my workshop, and remembering what I last did to a design. I just went looking for something and found a boxfull of 7-segment displays that I had forgotten I had.

I do Sudokuos every day and Lynda and I attempt the Times T2 crossword most days. I have been doing this for at least 20 years to try to stop brain-fade.

But I understand what you mean, it's just a question of starting early enough. Say 30?

Best
Charlie

shantanu@india

Dear Charlie,
In India I would have touched your feet and asked for blessings and you would have placed your palm's on my lowered head and murmured something behind your breath. This practice of touching the feet of the elders cuts across all caste, creed and religion can be considered a backbone of our culture.
I would love to be exactly like you when I'm 76...hope I'm still there with a crop of white hair on the top of my head and swirling nostalgia inside it!
Regards
Shantanu

charliecoutas

Dear Shantanu
I would love to come to India, place my palms gently on your head, and bless you and all the Good People who make this forum what it is. You and I would then jointly, equally, praise Les for his endless efforts to support this forum.

Bless you. (Sorry for going off-topic a little)

Charlie

shantanu@india

Dear Charlie,
I'm sure I can see your moist eyes.
Let's close this topic unless Les has anything to add.
Regards
Shantanu

charliecoutas