Reading Array values into variables


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125

    Exclamation Reading Array values into variables

    This is probably easy, but I can't seem to figure it out.

    I have an array of 34 bytes collected via Serin2. I can see each of these. What I need to do tho is read several of them and convert them from the hex values they represent now to decimal.

    So imagine:

    SSMAX(23) contains 83
    SSMAX(34) contains 03

    These make up a temperature measurement.

    I need to reverse these so I get 03 83, then convert these to DECIMAL

    03 83 to Decimal should give me 899. There is an implied decimal between the 9's so I'd divide by 10. the result would be 89.9

    BUT.. I can't find a way to get the data out of the array to deal with it.

    Does anyone have any ideas about this?

    I was planning to read each character 0 - 3 - 8 - 3 in sequence via a loop and multiply the 0*4096+3*256+8*16+3*1... But I can't seem to get data ouf of the array..

    I can't seem to get the array values into variables to work on them either.

    Thanks - this is new stuff for me.

    Tom

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,601


    Did you find this post helpful? Yes | No

    Wink

    Did you know the array starts at element 0?

    (suggestion from a newb)

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,601


    Did you find this post helpful? Yes | No

    Smile

    Are you sure you are processing your array in the proper order?

    I am also converting, DATAIN VAR BYTE[5] to one byte.

    RESULT=DATAIN[0]+(DATAIN[1]*10)+(DATAIN[2]*100)+(DATAIN[3]*1000)+(DATAIN[4]*10000)

    I was just about to search for a post. Someone had told me lately how to do the conversion and I was going to doublecheck my formula.

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    SSMAX(23) contains 83
    SSMAX(34) contains 03
    what about the following
    Code:
    DEFINE BOOTLOADER_USED 1
    DEFINE OSC 20
    
        ' Lcd defines
        ' ===========
        '
        define LCD_DREG PORTB     ' Set data pin of LCD to
        define LCD_DBIT 0         ' PORTB.0-PORTB.3
    
        define LCD_RSREG PORTB    ' Set RS bit of LCD to
        define LCD_RSBIT 4        ' PORTB.4
    
        define LCD_EREG PORTB     ' Set E bit of LCD to
        define LCD_EBIT 5         ' PORTB.5
    
        DEFINE LCD_LINES 4        ' 4 Lines LCD
        define LCD_COMMANDUS 2000 ' Command delay time in uSec
        DEFINE LCD_DATAUS 100      ' Data delay time in uSec
    
    WholeDecimalNumber var word
    SSD4        var byte
    SSD3        var byte
    SSD2        var byte
    SSD1        var byte
    SSMAX       var byte [80]
    MSB         var byte
    LSB         var byte
    
    
    ssmax[34]=$03
    SSmax[23]=$83
    SSD4= (ssmax[34]>>4)
    SSD3= (ssmax[34] & $f) 
    SSD2= (ssmax[23]>>4)
    SSD1= (ssmax[23] & $f) 
    
    WholeDecimalNumber=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1
    
    lcdout $fe,1,"myval=",#wholedecimalnumber dig 3,_
           #wholedecimalnumber dig 2,#wholedecimalnumber dig 1,_
           ".",dec1 wholedecimalnumber
    
    here: goto here
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Smile Thanks for the quick replies! Question about the math

    The 4th post includes this:

    SSD4= (ssmax[34]>>4)
    SSD3= (ssmax[34] & $f)
    SSD2= (ssmax[23]>>4)
    SSD1= (ssmax[23] & $f)

    It appears that somehow this is reading the digits out of the bytes, but I have never seen the notation before.

    >>

    & $f

    Do these somehow mean first digit and second digit?

    TG

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    >>4 => shift byte 4 position to the right. so you keep only the 4 high bits
    & $F => Bitwise and with hex F. so you keep only the 4 low bits

    everything is in the PBP manual section 4.17
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  2. Array values changing
    By MyBuddy in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th October 2009, 23:13
  3. Funny PULSIN values: what is going on???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th April 2008, 08:02
  4. Seeting array variables in For-Next loop
    By bcd in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th October 2007, 07:01
  5. Putting values into an array
    By Vince in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd December 2003, 07:22

Members who have read this thread : 1

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