Page 1

Rev: 1.0

C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com 7/6/2014


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

Table of Contents Objective of This Document ...............................................................................................................1 Why I Decided to Switch From C18 to XC8? .........................................................................................1 Introduction ......................................................................................................................................1 1.

Operating Modes (Extended or Non-Extended) ...........................................................................1

2.

Memory Models .........................................................................................................................1

3.

Integer Promotions .....................................................................................................................2

4.

Headers ......................................................................................................................................2

5.

Built-in Routines and Macros ......................................................................................................3

6.

Data Types and Limits .................................................................................................................3

7.

Size Limitations ..........................................................................................................................3

8.

Storage Classes ...........................................................................................................................3

9.

Pointer Storage Qualifiers ...........................................................................................................4

10. Function Variants .......................................................................................................................4 11. Structures and Unions ................................................................................................................4 12. Interrupts ...................................................................................................................................5 13. Locating Objects .........................................................................................................................5 14. Predefined Macro Names ...........................................................................................................7 15. In-Line Assembly ........................................................................................................................7 16. Linking .......................................................................................................................................7 17. References .................................................................................................................................8

Revision: 1.0 [07/2014]


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

Objective of This Document This document aims to provide a quick reference on the changes that must be considered or performed in the source code when migrating from C18 to XC8. This information is based on the Microchip C18 to XC8 Migration Guide (See references).

Why I Decided to Switch From C18 to XC8 ? 1. 2. 3.

4. 5. 6. 7. 8.

XC8 is the successor of C18; therefore late or early microchip will stop giving support to C18. XC8 can be used for all 8-bit families (PIC10, PIC12 and PIC16 and PIC18), while C18 only works for PIC18 family. XC8 is a C compiler which allows us to use the C language to develop programs for small microcontrollers (PIC10, PIC12 and PIC16). Previously we had to develop the program in assembler or find another C compiler that supported our microcontroller. XC8 allows you to declare local variables just before use, while in C18 you had to declare them always at the beginning of the block where it were used. XC8 fulfills more of the ANSI C standard which makes the code more portable. XC8 does not add code of functions that are not used in the program, which C18 did not do. XC8 is based on the compilers HI-TECH, which is very good. XC8 uses "xc8.exe" for compiling, linking, and creating libraries, while C18 used an application for each task (mcc18.exe, mplink.exe and mplib.exe).

Introduction The following sections describe the changes that should be considered when you want to migrate source code of C18 to XC8. Most of the changes that should be made are to not standard ANSI C code. If you are familiar with C18 there will be examples of equivalent updates that you can use with XC8.

1. Operating Modes (Extended or Non-Extended) XC8 does not support the PIC18 extended instruction set; code is always compiled for the standard instruction set.   

Only for PIC18 family. The “extended instruction set” configuration bit should be disabled. Used assemble code in C must only use non-extended instructions.

2. Memory Models XC8 does not use memory models since the size of the pointer for each variable is designated independently and automatically, therefore models are ignored.

Revision: 1.0 [07/2014]

1


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

3. Integer Promotions To perform arithmetic operations C18 uses the size of the largest data type, but when the data type is smaller than int (short, char and structures with 1-bit fields) will be promoted to either signed integer or unsigned integer, which may produce a different result than expected. Example 1: unsigned char count, a=0, b=50; if(a – b < 10) count++;

C18: The result of a-b = 206 which is not less than 10, so the code inside the if will not run. XC8: As both operands are converted to signed int the result of a-b = -50 which is less than 10, so the code inside the if will run. If you want the result of the subtraction be unsigned int (to get the same result as C18) a cast can be applied to the result of the subtraction. Example 1 with cast: unsigned char count, a=0, b=50; if((unsigned int)(a – b) < 10) count++;

Another problem arises when bit operators are used for example “~”. Example 2: unsigned char count, c; c = 0x55; if(~c == 0xAA) count++;

C18: It is assumed that ~c = 0xAA, hence the code inside the if will run. XC8: Because the variable c is changed to int the result of ~c = 0xFFAA, therefore is not equal to 0xAA and the code inside the if will not run. The above can be solved making a cast like the previous example. Example 2 with cast: unsigned char count, c; c = 0x55; if((unsigned char)(~c) == 0xAA) count++;

NOTE: Each case must be analyzed to determine if it is the expected behavior or if it requires a cast.

4. Headers XC8 uses the header <xc.h> for all devices; therefore we must delete all device specific headers as <p18cxxx.h>.

Revision: 1.0 [07/2014]

2


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

5. Built-in Routines and Macros The following table shows the equivalence of the built-in routines and macros: C18: Nop() ClrWdt() Sleep() Reset() Rlcf(), Rlncf(), Rrcf(), Rrncf() Swap()

XC8: NOP() CLRWDT() SLEEP() RESET() Use << and >> (c >> 4) | (c << 4)

NOTE: XC8 can also use _delay(), _delaywdt(), __delay_us(), __EEPROM_DATA(), etc.

6. Data Types and Limits  

char: Must be specified if it is signed or unsigned since C18 default handles it with sign, but XC8 unsigned. float y double: By default for C18 are 32-bit wide, but for XC8 are 24-bit wide and can be configured independently one of other with 32-bit if required (--FLOAT or --DOUBLE).

NOTE: XC8 allows you to use a new type called “bit” (it is not part of the standard) that can handle a bit (using an int of space).

7. Size Limitations The size of an object auto (array or structure) may not exceed 100h bytes, which is the same as a bank of PIC18.

8. Storage Classes C18 can define in which memory zone/type you want to store an object independently with the ram and rom classes (No standard classes for ANSI C). XC8 does not use them. On C18 the const class can be used independently or together to ram or rom classes. But XC8 use const to indicate that a global object will be read-only and will be stored in the program memory (Code area), which will be equivalent to const rom in C18.  

Delete all ram, near and far classes. Replace all rom and const rom classes with const class.

Revision: 1.0 [07/2014]

3


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

9. Pointer Storage Qualifiers XC8 ignores the qualifiers near and far since the compiler determines the size of the pointers automatically based on it use and these can be of 8/16/24-bit. In XC8 const y volatile can be used to define if the pointer is read only or if it should not be optimized, the rest of non-standard qualifiers should not be used and are ignored. ď&#x201A;ˇ

Delete all near y far qualified used in pointers.

10. Function Variants In C18 is necessary to perform variations of functions for different types of pointers (to ram or rom). While XC8 only requires one function (based on the ANSI C standard) regardless if the data is stored in program memory or data memory. ď&#x201A;ˇ

Change all function variants by the main function.

Example C18: char *strcpy (auto char *s1, auto const char *s2); char *strcpypgm2ram (auto char *s1, auto const rom char *s2); rom char *strcpyram2pgm (auto rom char *s1, auto const char *s2); rom char *strcpypgm2pgm (auto rom char *s1, auto const rom char *s2); Change in XC8: char *strcpy (char *s1, const char *s2);

11. Structures and Unions Anonymous structures and unions are still being supported by XC8, but they are not part of the ANSI C standard so is better if you do not use them. ď&#x201A;ˇ

Change 1-bit signed field inside a structure to unsigned since XC8 does not support them.

Revision: 1.0 [07/2014]

4


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

12. Interrupts Define interrupts in XC8 is easier; you only need to define one function per priority (one high and one low). Use the indicator interrupt for high priority and interrupt low_priority for low priority interrupts, the rest of the code necessary is completed by the compiler. In XC8 there is no restriction on the amount of code that can go into the body of the interrupt, except for the size of program memory in the device. Interrupt declaration in C18: #pragma code high_vector=0x08 void interrupt_at_high_vector(void) { _asm GOTO high_isr _endasm } #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm GOTO low_isr _endasm } #pragma code /* return to the default code section */ #pragma interrupthigh high_isr void high_isr (void) { /* service routine body goes here */ } #pragma interruptlow low_isr void low_isr (void) { /* service routine body goes here */ } Interrupt declaration in XC8: void interrupt low_priority low_isr (void) { /* service routine body goes here */ } void interrupt high_isr (void) { /* service routine body goes here */ }

13. Locating Objects XC8 does not handle the location of objects in the same way as C18, consider the following examples: If you want to assign a variable or function at a specific address the easiest way is to make them absolute using: @ address Absolute variables in data memory (RAM) cannot be initialized when you declare them. Absolute assignment in XC8: int voltage_var @ 0x100;

// This variable is allocated to the address 100h in data memory.

const int array[] @ 0x200 = {1,2,3,4,5}; // This constant is allocated to the address 200h in program memory. int calc_temp(paramsâ&#x20AC;Ś) @ 0x1500 {â&#x20AC;Ś}

Revision: 1.0 [07/2014]

// This function is allocated to the address 1500h in program memory.

5


Omar Gurrola http://www.proprojects.wordpress.com

C18 to XC8 Migration Reference

If the variable is required to be allocated within a specific bank bankX is used, but before the option –ADDRQUAL must be changed to request, otherwise it is ignored. XC8 allows allocating variables in the first four banks only. Allocation to a bank in XC8: bank1 int speed; // This variable is allocated in the bank 1 of program memory.

If the variable is required to be allocated within a specific section __section(“MySection”) should be used. The address and name of the section cannot be defined in the code; this must be specified with the option: -L-pMySection=0100h. Also a linker class can be defined to specify the name and address range: -L-AMYSPACE=50h-0ffh,100h-1ffh -L-pMySection= MYSPACE

Section allocation in XC8: -L-pMyData=0100h -L-AMYCODE=1000h-1500h -L-pMyCode=MYCODE int __section(“MyData”) power_var;

// This variable is allocated in the section MyData in data memory.

int __section(“MyCode”) read_temp(params…){…}

    

// This function is allocated in the section MyCode in program memory.

Replace all variables defined with sectiontype pragma to absolute variables, bank allocation or in a specific section using __section(). Replace all functions defined with sectiontype pragma with absolute functions or to a specific section using __section(). If defined sections are used add the required options to the compiler. Confirm that the data or code are placed in the desired areas Delete all the #pragma tmpdata directives.

Revision: 1.0 [07/2014]

6


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

14. Predefined Macro Names Macros predefined for C18 are shown in the following table and its equivalent in XC8: C18: __18CXX __PROCESSOR __SMALL__ __LARGE__ __TRADITIONAL18__ __EXTENDED18__

XC8: __XC, __XC8 __PROCESSOR N/A N/A __TRADITIONAL18__ N/A

Description: It define the used compiler Device specific: __18F4550 No extended mode is used -

XC8 does not support macros with a variable list of arguments, so it must be changed a function or implement a macro for each required combination of arguments.

15. In-Line Assembly In XC8 there are two additional ways to insert assembly instructions in C code: 1. 2.

The first way is to use the function asm(â&#x20AC;&#x153;instructionâ&#x20AC;?) for each instruction. The second way is to use #asm and #endasm to start and finish.

16. Linking XC8 does not use any linker file; if it is required to specify a section or define the allocation address of a variable you can implement it following the instructions of this section: Locating Objects.

Revision: 1.0 [07/2014]

7


C18 to XC8 Migration Reference

Omar Gurrola http://www.proprojects.wordpress.com

17. References 

Microchip, “MPLAB C18 to XC8 C Compiler Migration Guide”, 2013 http://ww1.microchip.com/downloads/en/DeviceDoc/50002184A.pdf

Microchip, “MPLAB XC8 C Compiler User’s Guide”, 2013 http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_XC8_C_Compiler_User_Guide.pdf

Revision: 1.0 [07/2014]

8


http://www.proprojects.wordpress.com

C18 to XC8 Migration Reference  

This document aims to provide a quick reference on the changes that must be considered or performed in the source code when migrating from C...

Read more
Read more
Similar to
Popular now
Just for you