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.
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: