CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC24FJ128GB106 int16 to int32 error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Piccolo



Joined: 19 May 2006
Posts: 23
Location: Connecticut

View user's profile Send private message

PIC24FJ128GB106 int16 to int32 error
PostPosted: Thu Sep 09, 2010 1:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 09, 2010 2:41 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 09, 2010 3:05 pm     Reply with quote

I changed int16 to unsigned int16 and then it worked just fine.
Thank you for your help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group