View previous topic :: View next topic |
Author |
Message |
Piccolo
Joined: 19 May 2006 Posts: 23 Location: Connecticut
|
PIC24FJ128GB106 int16 to int32 error |
Posted: Thu Sep 09, 2010 1:22 pm |
|
|
Using PCWHD IDE 4.112
Part is PIC24FJ128GB106 w/ Date code 10171R6
Here's my test code:
Code: | MyInt16 = 32768;
MyInt32 = (int32)MyInt16;
Set_LCD_Page(9);
printf(lcd_putc,"MyInt16 %Lu ", MyInt16);
Set_LCD_Page(10);
printf(lcd_putc,"MyInt32 %Lu ", MyInt32);
delay_ms(1500); |
the output on the LCD is:
MyInt16 32768
MyInt32 429493452
Any int16 value below 32768 converts to int32 without error.
Any int16 value >= 32768 is wrong.
Any suggestions greatly appreciated.
Here's the ASM listing:
.................... MyInt16 = 32768;
03BDC: MOV #8000,W4
03BDE: MOV W4,830
.................... MyInt32 = (int32)MyInt16;
03BE0: PUSH 830
03BE2: POP 832
03BE4: CLR W4
03BE6: BTSC.B 833.7
03BE8: SETM W4
03BEA: MOV W4,834
.................... Set_LCD_Page(9); |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Thu Sep 09, 2010 2:41 pm |
|
|
This is what you'd expect....
Reason is that by default, the PCD compiler treats integers as signed, so an int16, can't hold values over 32767. Use the unsigned keyword on the int16 declaration.
One of the little 'caveats' on the switch to the later chips.
Best Wishes |
|
|
Piccolo
Joined: 19 May 2006 Posts: 23 Location: Connecticut
|
|
Posted: Thu Sep 09, 2010 3:05 pm |
|
|
I changed int16 to unsigned int16 and then it worked just fine.
Thank you for your help. |
|
|
|