Low Cost PCB's Low Cost Components

Author Topic: PIC XC8 signed char literal constants  (Read 2129 times)

0 Members and 1 Guest are viewing this topic.

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
PIC XC8 signed char literal constants
« on: May 19, 2017, 01:01:17 AM »
It's only an annoyance but when using XC8...

signed char myThing;

myThing = -1;

The assignment gives a warning due to shortening of the right hand side.

How do I declare an 8-bit -1 value to avoid the warning?

Thanks in advance

« Last Edit: May 19, 2017, 01:02:59 AM by NivagSwerdna »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 1441
  • Country: fi
  • Embedded SW/HW.
Re: PIC XC8 signed char literal constants
« Reply #1 on: May 19, 2017, 01:27:40 AM »
-1 is of type signed integer, so assigning it to signed char will truncate is, thus the warning.

You can cast is explicitly to a signed char:

Code: [Select]
myThing = (signed char) -1;

Yes, this is very annoying, and it will break the code should you change the type of the myThing.

Some compilers support typeof() function, but unfortunately the XC8 doesn't support that.

However, there is a better way to do it:

Code: [Select]

typedef signed char myThing_t;

myThing_t myThing;

myThing = (myThing_t) -1;


 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #2 on: May 19, 2017, 01:38:31 AM »
But the cast doesn't make the warning go away?
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 1441
  • Country: fi
  • Embedded SW/HW.
Re: PIC XC8 signed char literal constants
« Reply #3 on: May 19, 2017, 01:43:38 AM »
Have you tried it?
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 1766
  • Country: it
Re: PIC XC8 signed char literal constants
« Reply #4 on: May 19, 2017, 02:50:51 AM »
compiler version?
i recall having problems with signed chars not being handled as signed, i solved by using a typedef. crazy, whatever.
so, why don't you include <stdint.h> and use the int8_t type? (yeah it just typedef signed char int8_t but you have a standard implementation across compilers and project)

or, assuming you don't run out of memory screw it and use an int
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #5 on: May 19, 2017, 06:33:40 AM »
Well I tried a few things... cast to (signed char) doesn't work...

main.c:299: warning: (752) conversion to shorter data type

and also...

    typedef volatile signed char myType;
    myType upBtnCount = 0;
    ...
    upBtnCount = (myType)-1;


gives...

main.c:299: warning: (752) conversion to shorter data type

but a slightly more creative...  ((signed char)0)-1 gets rid of the warning and generates the correct code (same as a line with a warning)

!    upBtnCount = (signed char)-1;
0x3E2: SETF upBtnCount, ACCESS
!    downBtnCount = ((signed char)0)-1;
0x3E4: SETF downBtnCount, ACCESS


i.e.  XC 1.36

WARNING          upBtnCount = (signed char)-1;
WARNING          upBtnCount = -1;
NO WARNING    upBtnCount = ((signed char)0)-1;

So I can ignore the warnings or I can do some jiggery pokery.  Very odd but harmless.
« Last Edit: May 19, 2017, 07:10:09 AM by NivagSwerdna »
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #6 on: May 19, 2017, 07:23:24 AM »
I found http://www.microchip.com/forums/m815346.aspx

Seems to be a feature.  I'll add a #pragma
 

Offline cv007

  • Regular Contributor
  • *
  • Posts: 98
Re: PIC XC8 signed char literal constants
« Reply #7 on: May 19, 2017, 07:34:01 AM »
Tried it, no warnings for me- XC8 version 1.41
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #8 on: May 19, 2017, 07:57:10 AM »
Tried it, no warnings for me- XC8 version 1.41
Just tried v1.42 and the 752 warnings are gone.... but now I get lots of...

main.c:140: warning: (373) implicit signed to unsigned conversion

#define UP_LED          LATAbits.LA7

static bit shortFlasher;
...

            UP_LED = shortFlasher;

 :-DD

 

Offline cv007

  • Regular Contributor
  • *
  • Posts: 98
Re: PIC XC8 signed char literal constants
« Reply #9 on: May 19, 2017, 08:54:57 AM »
Tried it, no warnings for me- XC8 version 1.41

LATAbits.LA7- shouldn't that be LATAbits.LATA7 ?
 

Offline rcbuck

  • Regular Contributor
  • *
  • Posts: 52
  • Country: us
Re: PIC XC8 signed char literal constants
« Reply #10 on: May 19, 2017, 09:54:25 AM »
MPLAB X v3.45
XC8 (v1.42)

This code:

main()
{
    signed char myThing;

    myThing = -1;
   
    while(1)  // main loop
    {
        CLRWDT();

        while(myThing < 125)
        {
            myThing++;
        }
       
        // more code below here

    }  // end while(1)
}  // end main

produces no warning.
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #11 on: May 19, 2017, 09:55:54 AM »
LATAbits.LA7- shouldn't that be LATAbits.LATA7 ?
I'm not sure what the difference between LATAbits.LA7 and LATAbits.LATA7 is!??!  They look equivalent in struct/bitfield terms?
 

Offline NivagSwerdna

  • Frequent Contributor
  • **
  • Posts: 932
  • Country: gb
Re: PIC XC8 signed char literal constants
« Reply #12 on: May 19, 2017, 10:00:43 AM »
MPLAB X v3.45
XC8 (v1.42)

This code:

####LINE 67    main()
{
    signed char myThing;

####LINE 70    myThing = -1;
   
    while(1)  // main loop
    {
        CLRWDT();

        while(myThing < 125)
        {
            myThing++;
        }
       
        // more code below here

    }  // end while(1)
}  // end main

produces no warning.

Produces 2 warnings with 1.36...

main.c:67: warning: (371) missing basic type; int assumed
main.c:70: warning: (752) conversion to shorter data type

Produces 1 warning with 1.42...

main.c:67: warning: (371) missing basic type; int assumed

The 371 warning seems very sensible.
 

Offline cv007

  • Regular Contributor
  • *
  • Posts: 98
Re: PIC XC8 signed char literal constants
« Reply #13 on: May 19, 2017, 10:56:39 AM »
Quote
I'm not sure what the difference between LATAbits.LA7 and LATAbits.LATA7 is!??!  They look equivalent in struct/bitfield terms?
The header for the chip I was using defines all bits in the LATXbits struct as LATXN, not LXN, yours must be different- disregard.
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 1073
  • Country: ca
Re: PIC XC8 signed char literal constants
« Reply #14 on: May 19, 2017, 11:28:55 AM »
I'm curious, I thought that chars & ints were by default signed, and if you didn't want signage, you would use the term unsigned.
Have I been doing thing backwards in XC8?
Or, have I just been lucky that my code always worked using negative numbers just using ints and chars?

__________
BrianHG.
 

Offline rcbuck

  • Regular Contributor
  • *
  • Posts: 52
  • Country: us
Re: PIC XC8 signed char literal constants
« Reply #15 on: May 19, 2017, 12:56:47 PM »
Quote
Produces 1 warning with 1.42...

main.c:67: warning: (371) missing basic type; int assumed
Sorry, my main() is actually void main(void) - I just manually typed main in the reply.

Quote
I'm curious, I thought that chars & ints were by default signed
From the XC8 user's manual page 143:
The MPLAB XC8 compiler supports integer data types with 1, 2, 3 and 4 byte sizes as well as a single bit type. Table 5-1 shows the data types and their corresponding size and arithmetic type. The default type for each type is underlined.

TABLE 5-1: INTEGER DATA TYPES

    Type               Size (bits)        Arithmetic Type
unsigned char          8              Unsigned integer
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 1073
  • Country: ca
Re: PIC XC8 signed char literal constants
« Reply #16 on: May 19, 2017, 02:44:56 PM »
Quote
Produces 1 warning with 1.42...

main.c:67: warning: (371) missing basic type; int assumed
Sorry, my main() is actually void main(void) - I just manually typed main in the reply.

Quote
I'm curious, I thought that chars & ints were by default signed
From the XC8 user's manual page 143:
The MPLAB XC8 compiler supports integer data types with 1, 2, 3 and 4 byte sizes as well as a single bit type. Table 5-1 shows the data types and their corresponding size and arithmetic type. The default type for each type is underlined.

TABLE 5-1: INTEGER DATA TYPES

    Type               Size (bits)        Arithmetic Type
unsigned char          8              Unsigned integer
See attached image:
After reading the finer text 3 paragraphs after the table, signed is the default except for 'char', so, you are correct, though there is no underlining...
« Last Edit: May 19, 2017, 02:57:28 PM by BrianHG »
__________
BrianHG.
 

Offline cv007

  • Regular Contributor
  • *
  • Posts: 98
Re: PIC XC8 signed char literal constants
« Reply #17 on: May 20, 2017, 02:20:21 AM »
That's why stdint.h was invented, so you can just tell the compiler what size you want and whether its signed or not- no table lookup required. When you use a uint16_t, int8_t, uint8_t - its obvious to you, the compiler, and anybody reading your code what that means.
 
The following users thanked this post: JPortici

Offline rcbuck

  • Regular Contributor
  • *
  • Posts: 52
  • Country: us
Re: PIC XC8 signed char literal constants
« Reply #18 on: May 20, 2017, 07:16:10 AM »
Quote
though there is no underlining...
No underlining must be your PDF viewer. The screen shot seems to indicate you are running Linux. I see the underlining with Adobe Acrobat.

As you say, the 3rd paragraph down does clarify the data types.

As cv007 suggests, I always use uint8_t, etc... as it makes it clear. I wonder if Microchip is defaulting char to unsigned as a carry over from the ASCII table?
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 1073
  • Country: ca
Re: PIC XC8 signed char literal constants
« Reply #19 on: May 20, 2017, 07:20:51 AM »
No underlining must be your PDF viewer. The screen shot seems to indicate you are running Linux. I see the underlining with Adobe Acrobat.

Sorry, but I am running windows 7.
And I am using Adobe Acrobat Reader version 2017.009.20044, Copyright 1984-2017 Adobe Systems Incorporated.

__________
BrianHG.
 

Offline rcbuck

  • Regular Contributor
  • *
  • Posts: 52
  • Country: us
Re: PIC XC8 signed char literal constants
« Reply #20 on: May 20, 2017, 08:57:51 AM »
Brian, I was using the XC8 manual from 2012. I just downloaded the XC8 2012-2016 version. I do not see the underlining in that version even though they state default types are underlined. You gotta love Microchip.

I am running the same version Acrobat that you are. However, I am using Windows 10, which sucks compared to Windows 7.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf