Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Conversion from 2s complement to decimal

Status
Not open for further replies.

north2012

Member level 3
Joined
Apr 1, 2013
Messages
63
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,813
hello,

Does anybody can help me with writing function for conversion from 2scomplement (16-bit) to decimal value? I am using micro C compiler, and here is my code which does not works... I received wrong results (-30 dec) on LCD for all calculations.
Code:
// LCD module connections
sbit LCD_RS at PORTC3_bit;
sbit LCD_EN at PORTC2_bit;
sbit LCD_D4 at PORTC4_bit;
sbit LCD_D5 at PORTC5_bit;
sbit LCD_D6 at PORTC6_bit;
sbit LCD_D7 at PORTC7_bit;

sbit LCD_RS_Direction at DDC3_bit;
sbit LCD_EN_Direction at DDC2_bit;
sbit LCD_D4_Direction at DDC4_bit;
sbit LCD_D5_Direction at DDC5_bit;
sbit LCD_D6_Direction at DDC6_bit;
sbit LCD_D7_Direction at DDC7_bit;
// End LCD module connections

void main(void)
{
signed short int p;
unsigned short mask;
char *txt;
char txtp[6];
//start of program
Lcd_Init();                        // Initialize LCD
delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
lcd_out(4,1,"INIT DONE...");
delay_ms(1000);
lcd_out(4,1,"CALCULATION 1");
  p = 0x038B;
   mask = 0x8000;
   while(mask)
   {
   if(p & mask)
       *txt = '1';
   else
       *txt = '0';
   txt++;
   mask >>= 1;
   }
  *txt = 0;
   ShortToStr(txt, txtp);
   Lcd_out(1,2,txtp);
  delay_ms(3000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  lcd_out(4,1,"CALCULATION 2");
   p = 0x0204;
   mask = 0x8000;
   while(mask)
   {
   if(p & mask)
       *txt = '1';
   else
       *txt = '0';
   txt++;
   mask >>= 1;
   }
  *txt = 0;
   ShortToStr(txt, txtp);
   Lcd_out(1,2,txtp);
 
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  delay_ms(1000);
  lcd_out(4,1,"CALCULATIONS DONE");

  while(1)
  {
  }
}
 
Last edited:

Hello,

I am trying to connect AD5933 with ATmega128. AD5933 is impedance converter and measurement results are stored in 4 8-bit registers (Real high&low byte and Imag high&low byte). Results are in 2s complement data format and for impedance calculation Z=sqrt(R*R+I*I) I need to convert 2s complement to decimal value (see example from data sheet in attachment). Because of that I want to first test function for conversion, that is code above. I want to convert three different numbers (0x038b, 0x0204 and 0xfa3f) to decimal to verify algorithm for conversion. example.jpg

- - - Updated - - -
 

The datasheet doesn't mention that the data is in 2's Complement and why would they design a device which will give result in 2's Complement. The datasheet tells that the data in the registers at address 0x94 and 0x95 will be 0x038B (say) = decimal 907 and value at addresses 0x96 and 0x97 will be 0x0204 (say) = decimal 516. You have to take the squares of the 2 numbers and add them and then take the square root of the result. Where is the 2's Complement?

unsigned int R, I;
float Z;


Code C - [expand]
1
2
3
4
R = 0x038B;
I = 0x0204
 
Z = sqrt(R^2 + I^2);

 

its already given in the correct format...... (they shown in the Hex)
 
Last edited:

Many thanks for your replies. In data sheet page 26 of 40 there is explanation (I attached picture). Untitled.jpgUntitled.jpg

- - - Updated - - -

here is one solution... :)

Code:
signed int convert (unsigned int p)
{
   unsigned short mask;
   signed int res = 0x0000;
   mask = 0x8000;
   if((p & mask) == 0x0000) //positive
   {
   res = p;
   }
   else //negative
   {
   res = p;
   res = ~res;
   res = res+1;
   res = 0-res;
   }
   return res;
}
 

Ok. What is the problem? 2's complement is nothing but 1's Complement + 1. To reverse subtract 1 from 2's complement and then invert the bits of the result.
 

Hai all,
I am trying to interface AD5933 with PIC16F886. Does anyone got a working proto board (only for AD5933)?
I am from Bangalore, India.
Regards,
Jayaraj.A
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top