Go Back   Tactical Gamer > General Forums > Hardware & Software Discussion


Hardware & Software Discussion Hardware and Software discussion and troubleshooting. Tweakers and Overclockers welcome!

Reply
 
Thread Tools
Old 06-17-2008, 10:23 AM   #1 (permalink)
 
Wimpinator's Avatar
 
Join Date: Jan 2006
Location: United states, TN
Age: 36
Posts: 2,982
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?
__________________
|TG-6th|Wimpinator



Wimpinator is offline   Reply With Quote
Old 06-17-2008, 07:19 PM   #2 (permalink)

 
RandomGuy's Avatar
 
Join Date: May 2003
Location: K-W, Ont.
Age: 27
Posts: 1,740
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
RandomGuy is offline   Reply With Quote
Sponsored links
Old 06-17-2008, 09:05 PM   #3 (permalink)
 
Steamers's Avatar
 
Join Date: May 2008
Posts: 158
Re: Need help with python sign extension

i'm lost o.0
Steamers is offline   Reply With Quote
Old 06-17-2008, 10:11 PM   #4 (permalink)
 
Dick Blonov's Avatar
 
Join Date: Feb 2006
Location: In a Vortex!
Age: 51
Posts: 3,398
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
__________________
|TG-6th|Blonov

«Any situation that involves the line "I was ran over by a dumptruck..." can't be good.» eGoatBoy

BattleField2 SOPs | Teamspeak | Server Rules and SOPs | The 6th Devil's Brigade





Last edited by Dick Blonov; 06-17-2008 at 11:10 PM.
Dick Blonov is offline   Reply With Quote
Old 06-18-2008, 10:59 AM   #5 (permalink)
 
Wimpinator's Avatar
 
Join Date: Jan 2006
Location: United states, TN
Age: 36
Posts: 2,982
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.
__________________
|TG-6th|Wimpinator



Wimpinator is offline   Reply With Quote
Old 06-18-2008, 09:50 PM   #6 (permalink)
 
Dick Blonov's Avatar
 
Join Date: Feb 2006
Location: In a Vortex!
Age: 51
Posts: 3,398
Re: Need help with python sign extension

Cool!

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

DB
__________________
|TG-6th|Blonov

«Any situation that involves the line "I was ran over by a dumptruck..." can't be good.» eGoatBoy

BattleField2 SOPs | Teamspeak | Server Rules and SOPs | The 6th Devil's Brigade




Dick Blonov is offline   Reply With Quote
Sponsored links
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT -4. The time now is 06:48 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
©2004-2008 - Tactical Gamer - All Rights Reserved