Multi-digit word variable to string


+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2011
    Location
    Portland, Oregon
    Posts
    195

    Default Multi-digit word variable to string

    Good morning.

    I have recently resurrected an old project as a means of introducing myself to PBP3 and the PIC16F88. It is a scrolling marquee on a single 5x7 matrix LED; the characters are 5x5 and with some missteps, I am now able to display a static message quite satisfactorily. Yaye me!

    To extend my knowledge, I determined to interface an LM34 temperature sensor as demonstrated here: http://www.rentron.com/PicBasic/LM34.htm. The project calls for a PIC16F677--the ADC setup is different, but therein lays the learning. Armed with the datasheet, I went to work… a reasonable period later, I felt I had adequate understanding to program some code.

    While I may (or may not) have gotten the ADC configured properly, the real frustration is displaying the result! I had intended to use the [DEC] statement, [DIG], and some division to insert each digit into the string array for display, but [DEC RawT (the word variable holding the ADC result)] results in a “Bad expression” error. I had thought perhaps that my ADC result was indecipherable, but even setting RawT to a likely value does not change the result.

    TL;DR:
    Can someone with a better understanding demonstrate how a multi-digit word variable be converted to a single digit string format; i.e. RawT=754 --> MSG[x]= “7”, MSG[x+1]= “5”, MSG[x+2]= “4”? Also, seeing that I’ve impinged on your good nature already, can you verify my ADC configuration to read from AN0 on pin17, using a +Vref on AN3 (pin 2)?

    Many thanks...

    CONFIG:
    #CONFIG
    __config _CONFIG1, _INTRC_IO & _WDT_OFF & _PWRTE_OFF & _MCLR_ON & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_PROTECT_OFF & _DEBUG_OFF
    __config _CONFIG2, _FCMEN_OFF & _IESO_OFF
    #ENDCONFIG

    DEFINE OSC 8
    OSCCON = %01110000
    ANSEL = %00001001 'ANALOG OFF, EXCEPT +Vref AND AN3
    TRISA = %0001001 'PORTS TO MATCH ANSEL
    ADCON1.7 = 0 : ADCON1.6 = 0 : ADCON1.5 = 1 : ADCON1.4 = 0 'LEFT JUSTIFIED : ADCS2 DISABLED : VCFG Vref+ AVSS
    ADCON0 = %01011001 'Fosc/8 : AN3 ACTIVE : ADON (BIT0) ADC ON
    TRISB=0 'PORTB DIGITAL



  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,283

    Default Re: Multi-digit word variable to string

    Hi,
    One way could be to use the ArrayWrite command, something like:
    Code:
    MSG VAR BYTE[4]
    RawT VAR BYTE
    
    RawT = 754
    
    ARRAYWRITE MSG, [DEC RawT]
    /Henrik.

  3. #3
    Join Date
    Apr 2011
    Location
    Portland, Oregon
    Posts
    195

    Default Re: Multi-digit word variable to string

    Thank you, Henrik.
    I love when the answer is shorter than the question!

    To be clear then, it appears that DEC is not a command of itself, but a parameter of some other display functions; it is only valid in an output string to an LCD or other output device (ie serial port) or construct (array-obviously).

    I hope it is not too much trouble for any who follow this thread to correct my interpretation as well as to clarify one more point? Can an entry point be specified, as ARRAYWRITE MSG[14], [DEC RawT] so that the variable may be positioned in a larger array? I saw little on this when searching

    It is possible to specify an entrypoint, so that (for example) the string "Temperature = XXX degrees" can be dynamically updated with Arraywrite ARRAYNAME[entrypoint] = [DEC variable]
    Last edited by Amoque; - 25th March 2012 at 19:44.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,283

    Default Re: Multi-digit word variable to string

    Hi,
    That's correct. DEC is not a command by itself, it's what the manual calls a modifier among others (BIN, HEX etc).

    I've never tried it but I'm pretty sure you can start anywhere in the array in the way you show.

    /Henrik.

  5. #5

    Default Re: Multi-digit word variable to string

    Hello all,

    this is an old post. I'm not sure if my question fits right to here.

    I have a temperature values and i would like to send them to a display from 4D Systems.

    At the moment i was using the serial SPE commands as 4D Systems calls it, and they have exactly the command to send those variables.

    Then i tried their "new" Visi Genie enviroment, which allows you to create "forms" and add any image, button etc.

    I have fniished the easy task inside the Visi Genie and i'm in front of a problem that i need to transform the variable to string.

    As from 4D Systems:

    Visie-Genie writes to pre-defined objects so in this case it is a pre-defined string object so all we have to do is convert what we need to send to a string, calculate a checksum and send. This is where Genie differs from Serial as in the Serial mode you are sending a serial command to the display which is translated to a command to do a supported function on the display.
    It is the first time i had to do a task with Checksum and found it interesting way by XORing all the bytes. In a very small code i tryied it worked, as i sent based on 4D Systems the command to change the constrast and it worked.

    Code:
    command var byte
    constrast var byte
    CS var byte
    
    ' -----------------------------------------------------------------------------|
    ' [ LCD Initialization ] |
    '------------------------------------------------------------------------------|
    ' FIRST TIME BOOTUP: We give plenty of time for tbe LCD '
    lcdinit:
    pause 2000
    command = $04
    contrast = $06
    gosub checksum
    hserout [command,contrast,CS]
    hserin [wait(6)]
    pause 100
    
    
    high green
    return
    
    '------------------------------------------------------------------------------|
    ' CHECKSUM |
    '------------------------------------------------------------------------------|
    checksum:
    cs = 0
    cs = cs ^command
    cs = cs ^ contrast
    return
    Now i'm trying to see the way to convert var to string. I have read the manual written in 2013

    "A numeric value preceded by a pound sign ( # ) will send the ASCII representation of its decimal value, up to 65535. For example, if W0 = 123, then #W0 (or #123) will send "1", "2", "3"."
    Is that the right way? May i have your help please?

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,974

    Default Re: Multi-digit word variable to string

    Or use DEC modifier like DEC k. If k=5, DEC k will send out the "5".

    With DEC you can also specify how many digits you will send. E.g. DEC3 will send 3 digit of the variable with the appropriate leading zeros in this case to fit the number of digits requested.

    If k=15 then DEC3 k will send "0", "1", "5"

    Ioannis
    Last edited by Ioannis; - 28th September 2020 at 09:57.

  7. #7

    Default Re: Multi-digit word variable to string

    Thanks so much Ioanni,

    will try it out.

Posting Permissions

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