String formatting
+ Reply to Thread
Results 1 to 11 of 11
  1. #1

    Default String formatting

    two closely related questions:
    I have a serial interface 16x2 character LCD display, using DEBUG to talk to it, is working fine

    I want to format a label and a number like this (2 lines shown)
    -----------------
    Pot
    20
    -----------------
    Pot
    2144
    -----------------

    notice the right hand justification of the number
    I know I could use some IF's and move the cursor prior to outputting the number but I would also need to clear the leading blank area which slows things down and is a pain

    My question is whether one of the String modifiers can do anything for me here ?
    something like "right justify" so it is padded with leading spaces, or leading 0's could be OK too

    and does DEBUG even support the BIN HEX etc string modifiers ?
    I think the manual says it does but I kept getting compile errors

    I should say though that I am quite rusty on this stuff but I can't make the syntax work, probably doing something wrong

    any examples would be appreciated even if they don't directly help this question posed,
    am referring to Debug command usage examples of section 2.11 in the manual
    thanks!

  2. #2
    Join Date
    Sep 2009
    Posts
    703

    Default Re: String formatting

    Tried DEC4?

  3. #3

    Default Re: String formatting

    the post did not show the "20" shifted right by 2 characters I guess the forum ate the leading spaces

    I did but it wouldn't compile, maybe I had the syntax wrong or maybe DEBUG does not support that ?
    with the brackets or without ?
    can you give a full line example please ?

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    1,603

    Default Re: String formatting

    if you are using pbp3 and a enhanced core pic16 or any pic18 chip, i created this user command
    http://support.melabs.com/forum/picb...strtok-usercmd
    that right justifies a buffer amongst other things

    eg
    include " strtok.pbpmod"
    buff var byte[6]
    data var word

    data = 20

    arraywrite buff,[#data,0]
    RJUSTBUFF buff,5

    debug str buff
    This is more entertaining than Free to Air TV

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    1,603

    Default Re: String formatting

    I did but it wouldn't compile, maybe I had the syntax wrong or maybe DEBUG does not support that ?
    it would help if you showed what you did

    debug dec4 data

    is the way but it has leading 0's that are pretty ugly
    This is more entertaining than Free to Air TV

  6. #6
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    271

    Default Re: String formatting

    When you "print" something to an LCD screen, you know you have 16 character spaces. In your Post #1 example, you listed 2 different examples; presumably the same display with 2 different values to display. A tactic I use to eliminate preceding "0" characters is by using IF/THEN statements;
    Code:
    VarChar VAR BYTE[4]
    ARRAYWRITE VarChar, [#VAR]
    IF Var < 1000 THEN
      VarChar[3] = VarChar[2]
      VarChar[2] = VarChar[1]
      VarChar[1] = VarChar[0]  
      VarChar[0] = " "
    ENDIF
    This could be extended for more characters;
    Code:
    VarChar VAR BYTE[4]
    ARRAYWRITE VarChar, [#VAR]
    IF Var < 100 THEN
      VarChar[3] = VarChar[2]
      VarChar[2] = VarChar[1]
      VarChar[1] = " "
      VarChar[0] = " "
    ENDIF
    ...and so on. Then to send to the LCD,
    DEBUG VarChar

    ...or something like that. This may actually place spaces at the front of your second line. You may need to modify it like;
    Code:
    IF Var < 1000 THEN
      VarChar[3] = " "

  7. #7

    Default Re: String formatting

    thanks all
    I was just doing stupid things as I had never used these modifiers before, e.g. I had
    DEC 4
    instead of DEC4
    I had brackets instead of no brackets
    I had #myvar instead of myvar
    and so on
    the leading 0's might be fine, I will dig through the rest of your suggestions as I move along, am stuck on 12b ADC at the moment in 16F1789, I masked out the top 4 bits but no joy, I will post a new thread on that later
    thanks again for all your help

  8. #8
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,155

    Default Re: String formatting

    If you have an 12 bit A/D and only want the result to be 8 bits then just do a right shift four places like this:
    a_d = a_d >> 4 now you have the upper 8 bits of the result.
    Dave Purola,
    N8NTA
    EN82fn

  9. #9
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    271

    Default Re: String formatting

    You want to Left Justify (typically found in one of the ADCONx SFRs) so the upper 8 bits are in ADRESH, then you read ADRESH for your 8-bit ADC result. Using DEFINE ADC_BITS 8 instructs PBP to Left Justify. The BYTE sized result will be from ADRESH. Your method adds complication, and you must have a WORD sized variable to retrieve your ADC result.

  10. #10

    Default Re: String formatting

    No I want a 12b result, not sure where the 8b impression came from, maybe I did not say something clearly earlier, anyway it is all good now.
    I have a 4.096 voltage reference and a pot across it to GND is reading 0-4095.

    Actually it is reading 4 at the bottom but I have not chased that down yet, I have VREFN enabled and Kelvin connected to GND near the VREFP just to avoid this but we will see ....

  11. #11
    Join Date
    Sep 2009
    Posts
    703

    Default Re: String formatting

    Probably ADCOffset. Try to measure VREFN if PIC have it on multiplexer. Then subtract that from actual measurement.
    Also, that is really low. It's 4mV. That is less then 0.01% of FSO.
    I wouldn't worry about that.

Similar Threads

  1. LCD Display Formatting
    By jimtreg in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 9th October 2015, 15:40
  2. Replies: 3
    Last Post: - 3rd December 2011, 23:48
  3. serial formatting
    By barkerben in forum General
    Replies: 4
    Last Post: - 7th January 2005, 18:54

Members who have read this thread : 15

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts