Announcement

Collapse
No announcement yet.

Need help with python sign extension

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with python sign extension

    I am working on a project that has a computer (32 bit x86 machine) reading data (numbers) over the serial port from a 16 bit micro controller. My software on the computer is written in python.

    Here is the problem. A 16 bit microcontroller is represents all numbers with only 16 bits. When I read the data with the 32 bit computer it misses the sign bit like this:

    What the microcontroller sent: 1000010001000000 (MSB is set so the # is negative)
    What the computer puts into the memory: 00000000000000001000010001000000 (MSB is not set so the number is wrong).

    I'm a hardware guys so for the life of me I can't figure this one out. I was thinking to AND the data with 0x8000 to look for the MSB and if it is set then do an inversion (~number + 1) but no dice so far. Anyone have any ideas?
    Retired 6th DB

  • #2
    Re: Need help with python sign extension

    You don't want to invert or take the negative of your number, just set the upper 16 bits appropriately. A C compiler would handle this for you if you used the correct types, I'm not sure about python.

    Code:
    if (data & 0x8000)
       data |= 0xFFFF0000;
    that's c syntax, btw

    Comment


    • #3
      Re: Need help with python sign extension

      i'm lost o.0

      Comment


      • #4
        Re: Need help with python sign extension

        Shift your number 16 bits to the left, do your math, then shift it back 16 bits to the right when you're done ?

        Can you post the piece of code that processes that number ?


        DB
        Last edited by Dick Blonov; 06-17-2008, 10:10 PM.

        «That looks like a really nice house except for that horrible bathroom.» Donrhos

        | |





        Comment


        • #5
          Re: Need help with python sign extension

          Well I think I figured it out. If the read number is negative then I invert it. I then AND it with 0xffff to chop off the extra ones (greater than 16 bits). Inverting one more time seems to give me the correct number. I was also misunderstanding how they were representing negatives. They are using twos complement.

          The data is read over the serial port and I load it into a list for easy manipulation. I look at the MSB of the high byte and if it is set then I know that the number is negative. That is when I do the inversion stuff. Here is a code snippet for reference.

          Code:
          if (lst[4] & 128)  :
          	Xaccel = Xaccel * -1
          	Xaccel = (Xaccel & 0xffff) * -1
          Well anyway it works now. Thanks for the advice guys.
          Retired 6th DB

          Comment


          • #6
            Re: Need help with python sign extension

            Cool!

            (that reminds me why I love assembler, one instruction: cwd, 2 bytes of code, done :icon19:)

            DB

            «That looks like a really nice house except for that horrible bathroom.» Donrhos

            | |





            Comment

            Connect

            Collapse

            TeamSpeak 3 Server

            Collapse

            Advertisement

            Collapse

            Twitter Feed

            Collapse

            Working...
            X