Weird code problem


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29

    Default Weird code problem

    OK... I'm about to give birth to a cow here. I've been at this for a few hours now and still can't find where the problem lies. Call it a brain fart or a senior moment or....

    This is a simple transmit routine that takes an array of letters, converts them to Manchester code and the spits them out a serial port.

    Code:
    COUNTERA var Byte
    COUNTERB var byte
    
    TRANSMIT var Byte 
    MYDATA var byte [9]
    MYDATACTR var byte
    MANCHESTERWORD var Word 
    Encoded var word
    BYTECTR var byte
    DataPort var PortB.0
    EncodedData var word [4]
    
    
    START:
        
        mydata [0]= "T"
        mydata [1]= "E"
        mydata [2]= "S"
        mydata [3]= "T"
        mydata [4]= "."
        
        
    for mydatactr=0 to 4
    
            transmit = mydata [MYdatactr]    
            gosub encodemanchester                 ' take the transmit variable and convert it to Manchester (EncodedData word)
            serout2 dataport,17197, [encodeddata.highbyte[mydatactr]]
            serout2 dataport,17197, [encodeddata.lowbyte[mydatactr]]
        
    So far so good. With the array "TEST." the serial port is spitting out 
    99 9A 9A 99 99 A5 99 9A A6 56 in HEX, which is a correct Manchester
    conversion.
    
    next mydatactr
    
    
    FOR COUNTERB=0 TO 1
         serout2 dataport,17197, [$55,$55,$55,$55,$55,"STA"]   ' send preamble
         for mydatactr=0 to 4
              serout2 dataport,17197, [encodeddata.highbyte[mydatactr]]
              serout2 dataport,17197, [encodeddata.lowbyte[mydatactr]]
    
    This is where it gets weird. The EncodedData array has not been tinkered in
    any way (I don't see where) yet the serial port is now spitting out
    99 9A A5 99 9A A5 56 9A A6 56 .... WTH?????? 
    
    Notice the first two and last two bytes did not change but the stuff in the middle has....   what am I missing here?
    
         next MYDATACTR
         pause 100
    NEXT COUNTERB
                
    pause 3000
    goto start
    
    EncodeManchester:
        manchesterword=0
        
        For BYTECTR=0 TO 7
        If transmit.0=1 then
            ManchesterWord.14=1
        else
            ManchesterWord.15=1
        endif
        
        If bytectr<7 then ManchesterWord=ManchesterWord>>2
        transmit=transmit>>1
        Next BYTECTR
        
        encodeddata.lowbyte[mydatactr]=manchesterword.lowbyte
        encodeddata.highbyte[mydatactr]=manchesterword.highbyte
        
    Return
    The above Manchester conversion routine and the one supplied by Ioannis produce the same result.

    Any help would be greatly appreciated.... BTW, I'm using MCS 2.1.0.6 with PBPPro 2.46.
    Thanks!!!
    Last edited by Navaidstech; - 15th March 2009 at 13:44.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    EncodedData var word [4] should be EncodedData var word [5]

    2 bytes are needed for each byte conversion to manchester.

    What's happening is you're corrupting RAM by going out-of-bounds
    in your EncodedData array.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29


    Did you find this post helpful? Yes | No

    Default

    Thank you for the reply Bruce...

    Code:
    EncodedData var word [4] should be EncodedData var word [5]
    You mean you can't use a zero for an array pointer?

    Code:
    2 bytes are needed for each byte conversion to manchester.
    Right... and I thought the conversion routine did just that. Am I missing anything besides the improper definition of the EncodedData array?

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    With EncodedData var word [4] you have 4 words total in the array.

    EncodedData[0]
    EncodedData[1]
    EncodedData[2]
    EncodedData[3]

    Now with;

    mydata [0]= "T"
    mydata [1]= "E"
    mydata [2]= "S"
    mydata [3]= "T"
    mydata [4]= "."

    If each byte in mydata gets encoded as 2 bytes, you're going to need more
    than 4 words or 8 bytes to store your encoded data in.

    So, when mydatactr is 4, and you jump to your encode routine;

    encodeddata.lowbyte[mydatactr]=manchesterword.lowbyte
    encodeddata.highbyte[mydatactr]=manchesterword.highbyte

    It's placing the encoded value outside your EncodedData word array. I.E. you
    now have two bytes of data in RAM out-of-bounds of your array.

    You mean you can't use a zero for an array pointer?
    You can, but you need to make sure you don't exceed the array boundary.

    A 4 word array only goes from 0 to 3. Not 0 to 4.
    Last edited by Bruce; - 15th March 2009 at 18:13.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29


    Did you find this post helpful? Yes | No

    Default

    Code:
    You can, but you need to make sure you don't exceed the array boundary.
    
    A 4 word array only goes from 0 to 3. Not 0 to 4.
    I see... I had incorrectly assumed it goes from 0 to 4. Duly noted.
    I will give it a try and report back...

    thank you for the pointers...

    Alex

  6. #6
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29


    Did you find this post helpful? Yes | No

    Talking

    Bruce...

    that did it! The transmitter now spits out good data.
    In addition to the increase in the array size, I also had to make the following change from:

    Code:
    EncodedData var word [5]

    To:
    Code:
    EncodedDataLow var Byte [5]
    EncodedDataHigh var byte [5]
    For some reason the splitting the manchester word into EcodedData.LowByte and EncodedData.Highbyte didn't quite work, and so I ended up creating sepatate bytes for the high and low ends of the manchester word.
    Not sure why it didn't work in the first place but it doesn't matter now. I might revisit this issue at a later date.

    Thank you for steering me in the right direction. This would have never crossed my mind - must be a habit from my old QBasic programming days.



    Alex

Similar Threads

  1. Problem compiling EasyHID code
    By mindthomas in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th August 2010, 21:46
  2. MN1307 sample code problem
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 20th November 2008, 21:41
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 09:26
  4. PIC18F got some weird problem?
    By NatureTech in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 19th February 2007, 17:18
  5. Servo Code problem
    By ALFRED in forum General
    Replies: 1
    Last Post: - 2nd March 2006, 04:30

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