![]() |


|
|||||||
| Hardware & Software Discussion Hardware and Software discussion and troubleshooting. Tweakers and Overclockers welcome! |
![]() |
|
|
Thread Tools |
|
|
#1 (permalink) |
![]() 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? |
|
|
|
|
|
#2 (permalink) |
![]() ![]() 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; |
|
|
|
| Sponsored links | |
|
|
|
|
|
#4 (permalink) |
![]() 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. |
|
|
|
|
|
#5 (permalink) |
![]() 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 |
|
|
|
|
|
#6 (permalink) |
![]() 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 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|
|
|
| Sponsored links | |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|

