Home

C Compiler for the PICmicro Devices User's Manual

image

Contents

1. LIBPATH CODEPAGE NAME vectors START 0x0 END 0x3 PROTECTED CODEPAGE NAME page0 START 0x4 END 0x7FF INCLUDE modulel lkr CODEPAGE NAME pagel START 0x800 END OxFFF CODEPAGE NAME idlocs START 0x2000 END 0x2003 PROTECTED CODEPAGE NAME config START 0x2007 END 0x2007 PROTECTED DATABANK NAME sfrO0 START 0x0 END 0x1F PROTECTED DATABANK NAME sfrl START 0x80 END 0x9F PROTECTED DATABANK NAME gprO START 0x21 END 0x7F DATABANK NAME gprl START 0xA1 END 0xFF SECTION NAME STARTUP ROM vectors Reset vector SECTION NAME ISERVER ROM intserv Interrupt SECTION NAME PROG1 ROM page0 ROM code Gage SECTION NAME PROG2 ROM pagel ROM code pagel SECTION NAME IDLOCS ROM idlocs ID locations SECTION NAME CONFIG ROM config Configuration SECTION NAME BANKO RAM gpr0 RAM bank 0 SECTION NAME BANK1 RAM gpri RAM bank 1 79 CC5X C Compiler B Knudsen Data J RORCKCKCECKCk KCkCk kCkCK kCKCKCkCKCkCk KCKCk KCKCKCKCKCKCKCKCKCk k kkk kkk k kkk ck ck ko k k File modulel lkr generated by CC5X when using the r2 option CODEPAGE NAME intserv START 0x4 END 0x1C CODEPAGE NAME page0 START 0x1D END 0x7FF 6 9 The cdata Statement The cdata statement stores 14 bit data in program memory NOTE 1 cdata can currently not be used with relocatable assembly When using MPLINK such data statements can be put i
2. pragma cdata IDENTIFIER lt VXS gt lt VXS gt ADDRESS 0 Ox7FFE VXS VALUE EXPRESSION STRING VALUE O Ox3FFF EXPRESSION any valid C constant expression i e 0x1000 3 1234 STRING Valid C String r n 0 x24 x8 xe xFF xff 42 CC5X C Compiler B Knudsen Data pragma char name 9 constant or variable Defines the global variable name Useful for assigning a variable to a certain address Only valid addresses are allowed fpragma char i 0x20 pragma char PORTX PORTC NOTE If the compiler detects double assignments to the same RAM location this will cause a warning to be printed The warning can be avoided if the second assignment use the variable name from the first assignment instead of the address Zpragma char var2 varl pragma chip device Defines the chip type This allows the compiler to select the right boundaries for code and memory size variable names etc Note that the chip type can also be defined as a command line option pragma chip PIC16C55 This statement have to proceed any normal C statements but some preprocessor statements like if and define can be compiled first The supported devices are either known internally 16C54 55 56 57 58 61 64 65 71 73 74 84 620 621 622 or defined in a PICmicro header file i e 16F877 h It is also possible to make new header files Refer to file chip txt for details
3. 51 6 1 PROGRAM CODE E 51 Another way of locating functions eee eene nennen een eene netter nene teee trennen teens 51 The pagetype modifier eei te EEeEd Eed 52 Page selection bits si eio tee e e HU Bee aree ee e dee deed 52 6 2 SUBROUTINE CALL LEVEL CHECKING eee eee n nennen nennen nnns 52 Stack level checking when using interrupt eese eene nennen eene eene ener ene 52 PCIE REPRE 53 6 3 INTERRUPTS Ee OU ede bee AR A A 53 6 4 STARTUP AND TERMINATION CODE eeseeeeeeeeee eene enne enne nre nre nn innen enne innen 54 Clearing ALL RAM locations a e ed ee e fere iate ei de e OW vere E Yen 54 6 5 LIBRARY SUPPORT rierien are eso Rp OI eee Una eee eee 35 Math ID FOLIOS sisse ee re Gata ee eet eei aR Scab eege ebay en dote de Ee 55 Integer TE 56 Ia 57 Floating point libraries i socio ave sea a e Save vtr dre ege a ee coe ere EAS 58 Floating point library functions saos ee Gee ale d Ode p fete Brevet 59 Fastand compact inline operations css dei civ er e etre t teet sa rove Been 61 Using prototypes and multiple code pages essent eene eene 61 Kix d point example PER aee iere dires 62 Floating point example edat aepo aUe ies cheba ws eevee meae qtd 62 HOW t0 3QV6 C0d8 eeh ER tree epe a rig eee sete apte ege NEE 63 6 6 INEINE ASSEMBLY oie e Io n eie RR E Ne qe e IR E I e eire whee 63 Generating Single Instructions e UR rra dames 67 6 7 OPTIMIZING THE CODE etd ero dea eire e E
4. Case4 nop Case5 nop Case6 nop Case7 nop Case8 nop Case9 nop pragma compute END NOTE the function Gl useful in thi instruc nop nop dGoto 0 goto END is multiply by 2 multiply by 2 tions at eac nop retur nop retur nop retur nop retur nop retur nop goto nop goto nop goto nop goto nop goto end of necessary fo h selection n n n n n END END END END END region r ALL cases if is called from another codepage NOTE pragma optimize can be S situa tion If the call 1 evel is too deep n ote that the compiler can only replace CALL by GOTO if there are few function retur 9 3 The switch statement char select char W switch W n Constant inside the case 1 XORLW 1 Rae E break case 2 XORLW 3 break case 3 XORLW 1 case 4 XORLW 7 return 4 case 5 XORLW 1 return 5 return 0 default The compiler performs a sequence of XORLW lt const gt These constants are NOT the same as the constants written in the C code However the produced code is correct If more compact code is required then consider rewriting the switch statement as a computed goto This is very efficient if the cases are close to each other i e 2 3 4 5 96 CC5X APPENDI
5. GW dynamic selected skip format warning on long format default 48 CC5X C Compiler B Knudsen Data GD dynamic selected skip format GS always short skip format error if boundary is crossed GL always long skip format I lt directory gt include files directory Up to 5 library directories can be supplied by using separate I lt dir gt options When using include test h the current directory is first searched If the file is not found there then the library directories are searched in the same order as supplied in the command line option list I dir The current directory is skipped when using include lt test h gt j do not update page selection bits 12 bit core STATUS PAO and DAT 14 bit core PCLATH 3 and 4 L lt col gt lt lin gt produce list file src Ist The maximun number of columns per line col and lines per page lin can be changed The default setting is L80 60 mc1 default const pointer size is 1 byte 8 bits mc2 default const pointer size is 2 bytes 16 bits mr1 default RAM pointer size is 1 byte mr2 default RAM pointer size is 2 bytes mml default pointer size is 1 byte all pointer types mm2 default pointer size is 2 bytes all pointer types o lt name gt write hex file to name O lt directory gt output files directory Files generated by the compiler are put on this directory except when a full path name is supplied p lt device gt def
6. amp var name Global bit variables is a challenge It is recommended to first define a char variable and then use bur bx ch 0 Otherwise CC5X will defines a global char variable with random name This name have the format Gbit X X where X is a more or less random selected letter This variable is reserved by a RES statement and used in the assembly file when generating relocatable assembly bat pily blk 10 BCF _GbitQB 0 0 The variable file var is slightly modified when generating relocatable assembly Note that most addresses stated in the variable file are reallocated by MPLINK Local variables CC5X uses a different naming strategy on local variables when generating relocatable assembly CC5X reserves a continuous block in each ram bank or shared bank and use this name when accessing local variables IMPORTANT RESTRICTION The main routine interrupt service routines and all extern functions are defined as independent call trees or paths A function called from two independent call paths can not contain local variables or parameters because address sharing can not be computed in advance CC5X detects this and generates an error message The name of the local RAM blocks are LcRA LcRB etc The last letter is related to the RAM bank and the second last to the module name Adding option ro1 will for example change name _LcAA to _LcBA This can be used if there is a collision between local variable block
7. pragma codepage lt 0 1 2 3 gt 12 bit core 14 bit core 0x000 Ox1FF 0x0000 0x07FF 0x200 Ox3FF 0x0800 OxOFFF 0x400 Ox5FF Ox1000 Ox17FF 0x600 Ox7FF 0x1800 Ox1FFF WN L CH Defines the codepage to be used Code is located at the start of the active codepage or from the current active location on that page The codepage can not be changed inside a function Non existing pages for a specific controller are mapped into existing ones pragma codepage 3 following functions are located on codepage 3 pragma computedGoto 0 1 2 This statement can be used when constructing complicated computed goto s Refer to Section 9 2 Computed Goto for details pragma computedGoto 1 start region pragma computedGoto 0 end of region pragma computedGoto 2 start large region pragma config id state id lt state gt id PWRTE WDTE FOSC BODEN ID state on off LP HS XT RC number number pragma config WDTE off FOSC HS pragma config WDTE 0 FOSC 2 PWRTE 1 fpragma config 0x100 set bit 8 43 CC5X C Compiler B Knudsen Data fpragma config amp OxFFFC clear bit 0 and 1 pragma config amp 3 clear bit 0 and 1 Refer to Section 4 2 PICmicro Configuration for more details pragma config_def lt value gt Defines the position and size of the supported config identifiers and is normally found in PIC
8. pragma update IRP 0 OFF pragma update PAGE 0 OFF pragma update_RP 0 OFF pragma wideConstData 70 pragma asm2var 1 Enable equ to variable transformation Defined in file Section 6 6 Inline Assembly pragma assert type text field Assert statements allows messages to be passed to the simulator emulator etc Refer to Section 7 3 Assert Statements for details pragma assume pointer in rambank n The Zpragma assume statement tells the compiler that a pointer operates in a limited address range Refer to Section 2 4 Pointers for details pragma bit name N B or variable B gt Defines the global bit variable name Useful for assigning a bit variable to a certain address Only valid addresses are allowed pragma bit bitxx 0x20 7 pragma bit ready STATUS 7 pragma bit ready PA2 NOTE If the compiler detects double assignments to the same RAM location this will cause a warning to be printed The warning can be avoided if the second assignment use the variable name from the first assignment instead of the address Zpragma bit var2 9 var1 pragma cdata ADDRESS VXS lt VXS gt The cdata statement can store 14 bit data in program memory at fixed addresses It can also be used to store data in EEPROM memory Refer to Section 6 9 The cdata Statement for details pragma cdata ADDRESS VXS lt VXS gt pragma cdata lt VXS gt lt VXS gt
9. this will not work p 15C64 a not this either Note that the file path if the file does not reside on the current directory 50 CC5X C Compiler B Knudsen Data 6 PROGRAM CODE 6 1 Program Code Pages Many of the PICmicro devices have more than one code page A code page contains 512 words on the 12 bit core and 2048 words on the 14 bit core Using more than one code page requires pragma statements All functions following a pragma codepage statement are put on the page specified Codepage 0 is used as default functions proceeding the first codepage statement are placed on codepage 0 pragma codepage 2 char fx void this function is placed on codepage 2 pragma codepage 1 following functions are placed on codepage 1 When switching between codepages the compiler will keep track on the next free location on each codepage Use of codepages is just a matter of optimization as long as the compiler accepts the selection The optimal combination requires least code or has a speed advantage The optimizer removes unnecessary setting and clearing of the page selection bits Some of the PICmicro devices have 4 code pages Note that calls which requires switching between page 0 and 3 or page and 2 requires more instructions than the other combinations The compiler produces an error message when page limits are exceeded Invalid code pages are mapped to valid ones Another way of locating fun
10. Fixed point math use formats where the decimal point is permanently set at byte boundaries For example fixed8 8 use one byte for the integer part and one byte for the decimal part Fixed point operations maps nicely to integer operations except for multiplication and division which are supported by library functions Information on fixed point libraries is found in Section 6 5 Library Support fixed8 8 fx fx low8 Least significant byte decimal part fx high8 Most significant byte integer part MSB LSB 1 256 0 00390625 07 01 7 0x01 0 00390625 7 0039625 07 80 7 0x80 0 00390625 7 5 07 FF 7 OxFF 0 00390625 7 99609375 00 00 0 FF 00 1 FF FF 1 OxFF 0 00390625 0 0039625 TE 00 127 TE FF 127 OxFF 0 00390625 127 99609375 80 00 128 Convention fixed lt S gt lt I gt _ lt D gt S U unsigned none signed lt I gt number of integer bits D number of decimal bits Thus fixed16 8 uses 16 bits for the integer part plus 8 bits for the decimals a total of 24 bits The resolution for fixed16 8 is 1 256 0 0039 which is the lowest possible increment This is equivalent to 2 decimal digits actually 2 4 decimal digits Built in fixed point types Type fbytes Range Resolution fixed8 8 2 1 1 128 127 996 0 00390625 fixed8 16 3 1 2 128 127 99998 0 000015259 fixed8 24 4 1 3 128 127 99999994 0 000000059605 fixedl16 8 3 2 1 32768 32767 996 0 0039
11. W k ove literal to W IORLW k Z w WwW k Incl OR literal and W ANDLW k Z W W kk AND literal and W XORLW k Z W W k Excl OR literal and W Addition for the 14 bit core ADDLW k C DC Z W k W Add literal to W SUBLW k C DC Z W k W Subtract W from literal RETURN 7 Return from subroutine RETFIE iud Return from interrupt Note d 1 destination f DECF f Zeck d 0 destination W DECF f W W f 1 f file register 0 31 Z Zero bit Z 1 if result is 0 C Carry bit ADDWF C 1 indicates overflow SUBWF C 0 indicates overflow RRF C bit 0 of file register f RLF C bit 7 of file register f DC Digit Carry bit ADDWF DC 1 indicates digit overflow SUBWF DC 0 indicates digit overflow TO Timeout bit PD Power down bit Instruction execution time B Knudsen Data Most instructions execute in 4 clock cycles The exceptions are instructions that modify the program counter These execute in 8 clock cycles e GOTO and CALL e skip instructions when next instruction is skipped e instructions that modify the program counter i e ADDWF PCL 99
12. amp lw d 40 CC5X C Compiler B Knudsen Data ifdef ifdef SYMBOL Statements compiled if SYMBOL is defined Conditional compilation can be nested SYMBOL should not be a variable or a function name endif ifndef ifndef SYMBOL statements compiled if SYMBOL is not defined endif elif ifdef AX elif defined BX defined CX statements compiled if AX is not defined and BX or CX is defined endif else ifdef SYMBOL else endif endif ifdef SYMBOL endif error error This is a custom defined error message The compiler generates an error message using the text found behind error pragma The pragma statement is used for processor specific implementations 4 1 The pragma Statement Available pragma statements pragma asm2var 1 pragma assert e text passed to the debugger pragma assume p in rambank 3 pragma bit ready STATUS 7 pragma cdata 1 0 0x2000 pragma char varX 7 pragma chip PIC16C55 pragma codepage 0 41 CC5X C Compiler B Knudsen Data pragma computedGoto 0 pragma config WDTE off FOSC HS pragma library 1 on pragma location 2 pragma optimize 0 ALL off pragma origin 0x200 pragma rambank 1 pragma rambase 48 bank 1 pragma ramdef 0x190 O0Ox19F pragma resetVector 0 at location 0 pragma return Hello 0 1 H pragma stackLevels 4 max 64 pragma update FSR 1 ON
13. asm b o pt define local settings pragma asm o change setting in assembly mode endasm end current local settings Interpretation o current optimization is performed in assembly mode o no optimization in assembly mode b current bank bit updating is performed in assembly mode b no bank bit update in assembly mode pt current page bit updating is performed in assembly mode p no page bit update in assembly mode Note that b o p means that updating is performed if the current setting in C mode is on Updating is NOT performed if it is switched off in the C code when assembly mode starts The command line options b u j will switch updating off globally The corresponding source code settings are then ignored Generating Single Instructions The file INLINE H describes how to emulate inline assembly using C code This allows single instructions to be generated Intended usage is mainly for code with critical timing The compiler will normally generate single instructions if the C statements are simple Remember to inspect the generated assembly file if the application algorithm depends upon a precisely defined instruction sequence The following example show how to generate single instructions from C code nop NOP f W MOVWF f W 0 CLRW f 0 CLRF f W 2f W SUBWF W f f W SUBWF f W 2f 1h DECF W f f 1 DECF f W f W IORWF W Es E WwW IORWF f W f amp
14. fp underflow fp divide by zero domain error fp rounding bit FpOverflow FpFlags bit FpUnderFlow FpFlags bit FpDivO H FpFlags bit FpDomainError 8 FpFlags bit FpRounding H FpFlags FpRounding 0 truncation FpRounding 1 unbiased rounding to nearest LSB K ae K On OD GA ho r2 IEEE754 interoperability The floating point format used is not equivalent to the IEEE754 standard but the difference is very small The reason for using a different format is code efficiency IEEE compatibility is needed when floating point values are exchanged with the outside world It may also happen that inspecting variables during debugging requires the IEEE754 format on some emulators debuggers Macros for converting to and from IEEE754 is available math32f h before sending a floating point value float32ToIEEE754 floatVar change to IEEE754 3 instr I I before using a floating point value received IEEE754ToFloat32 floatVar change from IEEE754 3 instr I I math24f h float24ToIEEE754 floatVar change to IEEE754 3 instr I 15 CC5X C Compiler B Knudsen Data H Lr I jR754ToFloat24 floatVar change from IEEE754 3 instr Fixed point variables Fixed point can be used instead of floating point mainly to save program space
15. pragma update IRP 1 is processed IRP RAM 1 if IRP is active when accessing RAM registers using INDF 0 otherwise Using dpragma update IRP Di will set this macro to 0 until pragma update IRP 1 16CXX always defined 12 and 14 bit cores 16C5X when the 12 bit core is selected _16C54 when the 16C54 is selected similar for the other devices 3 9 Upward Compatibility The aim is to provide best possible upward compatibility from version to version Sometimes the generated code is improved If the application programs contain timing critical parts depends on an exact instruction count then these parts should be verified again for example by using the MSDOS program fc file compare on the generated assembly files Evaluation of constant expression is slightly changed from version 2 x in order to adapt to standard C An error message is printed if significant bits are lost The cure is to use type conversion a uns16 10 100 Alternatively will the command line option cu force 32 bit evaluation of constant expressions The option wS changes the error message to a warning 39 CC5X C Compiler B Knudsen Data 4 PREPROCESSOR DIRECTIVES The preprocessor recognizes the following keywords define undef include if ifdef ifndef elif else endif error pragma A preprocessor line can be extended by putting a V at the end of the line This requires that there are no space characters beh
16. 0 4294967295 Floating point The compiler supports 16 24 and 32 bit floating point The 32 bit floating point can be converted to and from IEEE754 by 3 instructions macro in math32f h 14 CC5X C Compiler B Knudsen Data Supported floating point types floati16 16 bit floating point float float24 24 bit floating point double float32 32 bit floating point Format Resolution Range 16 bit 2 4 digits t 3 4e38 1 1e 38 24 bit 4 8 digits 3 4e38 1 1e 38 32 bat 7 2 digits t 3 4e38 1 1e 38 Note that 16 bit floating point is intended for special use where accuracy is less important More details on the floating point formats is found in math txt Information on floating point libraries is found in Section 6 5 Library Support Floating point exception flags The floating point flags are accessible in the application program At program startup the flags should be initialized FpFlags 0 reset all flags disable rounding FpRounding 1 enable rounding Also after an exception is detected and handled in the application the exception bit should be cleared so that new exceptions can be detected Exceptions can be ignored if this is most convenient New operations are not affected by old exceptions This also enables delayed handling of exceptions Only the application program can clear exception flags char FpFlags contains the floating point flags fp overflow
17. 74 147 158 B 24 24 8 32 37 124 162 166 A 32 32 8 43 37 178 23 2 222 Fixed point libraries math16x h 16 bit fixed point 8 8 signed and unsigned math24x h 24 bit fixed point 8 16 16 8 signed and unsigned math32x h 32 bit fixed point 8 24 16 16 24 8 signed and unsigned The libraries can be used separately or combined The timing stated is measured in instruction cycles 4 clock and includes parameter transfer call return and assignment of the return value The timing values are found by executing a large number of iterations using selected argument values Sign unsigned S signed Sign Res argl op arg2 Program Approx CYCLES mathl6x h Code min aver max S 8 8 8 8 8_8 47 226 263 339 z 8_8 8_8 8_8 23 214 252 326 S 88 88 88 5T 497 518 584 88 88 88 35 528 558 680 57 CC5X C Compiler B Knudsen Data math24x h Code min aver max S 168 2168 16 8 60 376 450 577 168 2168 16 8 ER 364 437 580 S 168 2168 16 8 68 850 893 1093 168 168 16 8 46 894 944 1222 S 816 28 16 8 16 60 354 428 555 8_16 8_16 8_16 28 342 415 558 S 8_16 8_16 8_16 68 1050 1116 1349 8_16 8_16 8_16 46 1104 1188 1520 math32x h Code min aver max S 24 8 24 8 24 8 77 558 T22 983 24_8 24_8 24 8 35 546 709 1026 S 24 8 248 24 8 85 1298 1366 1761 24 82248 24 8 57 1361 1432 1929 S 16_16 16 16 16 16 78 561 704 930 16_16 16_16 16_16 36 549 690 965 S 16_16
18. 8 32 8 24 16 32 36 24 24 32 c 132 16 8 24 8 32 8 16 16 24 16 32 4 16 signed Program Code 13 18 15 16 16 17 26 32 25 31 18 19 20 2 5 31 32 Approx CYCLES min aver max 83 83 83 197 222 271 198 198 198 1529 179 179 247 247 247 356 356 356 217 263 361 239 310 447 337 410 553 5413 654 929 235 235 235 368 368 368 517 517 5437 287 29T 33 5 481 542 633 665 718 873 xecution speed is critical before math h 56 CC5X C Compiler B Knudsen Data SBa s 24 24 24 36 564 576 732 Au 32 32 32 47 943 966 1295 ABC S 16 16 8 33 196 201 2d AB S 24 24 8 37 305 310 326 A S 32 32 8 41 430 436 457 ABC S 16 16 16 49 296 309 361 B S 24 24 16 53 450 473 543 A S 32 32 16 57 626 660 747 B S 24 24 24 66 573 597 762 A S 32 32 32 83 952 990 1329 ABC 8 16 8 18 226 226 226 ABI e 8 24 8 19 354 354 354 Ase 8 32 8 20 502 502 502 ABC 16 16 16 23 280 283 312 B 16 24 16 29 463 497 599 A 16 32 16 30 636 698 828 B 24 24 24 34 556 567 700 Ara 32 32 32 45 934 955 1254 ABC S 8 16 8 30 189 190 195 ABC S 16 16 16 46 290 297 332 Be 38 16 24 16 50 442 455 501 A S 16 32 16 54 614 634 692 nBu IS 24 24 24 66 567 584 125 A S 32 32 32 86 944 974 1284 A math32m h B math24m h C mathl6m h Code min aver max ABC 16 8 8 37 50 50 50 ABC S 16 16 16 23 437
19. 8 no free stack entry for interrupt Using q2 means an additional warning at stack level 7 if the interrupt routine requires 2 levels i e contains function calls It is NOT recommended to use the q N as a default option Recursive functions Recursive functions are possible Please note that the termination condition have to be defined in the application code and therefore the call level checking can not be done by the compiler Also note that the compiler does not allow any local variables in recursive functions Function parameters and local variables can be handled by writing code that emulates a stack A warning is printed when the compiler detects a function which call itself directly or through another function This warning can be switched off with the wr command line option 6 3 Interrupts The 14 bit core allows interrupts e external interrupt on RBO INT pin e external interrupt on port pins change RB4 7 e internal interrupt on TMRO overflow e and other controller specific interrupts The structure of the interrupt service routine is as follows include intl16CXX h fpragma origin 4 interrupt serverX void W and STATUS are saved by the next macro PCLATH is also saved if necessary The code produced is strongly CPU dependent int save registers W STATUS and PCLATH char sv FSR sv FSR FSR if required handle the interrupt FSR sv FSR if required int restore registe
20. 851 words float24 sin float24 sine input in radians float24 cos float24 cosine input in radians Input range 512 0 512 0 Accuracy error 3 10 5 The relative error can be larger when the output is near 0 for example near sin 2 PI but the absolute error is lower than the stated value Timing min aver max 396 2492 2746 Size 215 words basic 24 bit math library Minimum complete program example 595 words float32 sin float32 sine input in radians float32 cos float32 cosine input in radians Input range 512 0 512 0 Can be used over a much wider range if lower accuracy degrades gradually to 1 significa at input value 10 6 is accepted nt decimal digit Accuracy error 1 2 10 7 The relative error can be larger when the output is near 0 for example near sin 2 PI but the absolute error is lower than the stated value Timing min aver max 943 5220 35855 FK Size 357 words basic 32 bit math library Minimum complete program example 818 words 60 CC5X C Compiler B Knudsen Data The accuracy of the math functions have been checked using many thousands of calculations ME 1 means that the mantissa value can be wrong by 1 i e 1 bit The relative error is then 1 5 10 for 24 bit floating point and 6 10 for 32 bit floating point Only a small fraction of the calculations may have the stated error The min and max timing stated have
21. Function return VALUES RE 35 Parameters in function calls eee esses eese enne teee tnnt en nnne tnnt reete trennen tene en nennen 35 Internal functions REESEN 35 JI TYPE CAS T once Regn e R E eB P RAN ER R R ER ON 36 3 6 ACCESSING PARTS OF A VARIABLE eene e eee e n nnn n AAEN AE EE i rina 37 3 7 G EXTENSI NS ke tette aru ep to te EHE NH E RET INEE A 37 3 8 PREDEFINED S YMBOLS 4 52 3 rere hene teen dn 38 Automatically defined macros and symbols eese ener 39 28 UPWARD COMPATIBILITY NEEN EEN 39 4 PREPROCESSOR DIRECTIV ES eere eee entes tn nonna sins tn tasas totu sons ta tn sine a senta 40 Ln C 40 TEE 40 E E 40 E 40 LI 41 LUI TM PTT E REER 41 Eege Eeer 41 IJ EE 4 EE 4I 12100 TTE 4I LOT To 41 4 1 THEPRAGMA STATEMENT iecit e PHREPRR E REIR ERE ENREDR EI EHE PERSA EARE PERERE TR ERR eee eire E Sgen 41 pr agmdasm2var E tee 42 pragma assert type text Held 42 pragma assume pointer in rambank n eese teet nennen eene en rennen 42 pragma bit lt name gt 9 N B or variable BI 42 Zpragma cdatalADDRESS VXS e MER 42 pragma char name constant or Voirie 43 Zpraegma EE 43 Zpraegma codepage E EE 43 Hpragma computedGoto lt O L 2 gt
22. MPLAB The occ file can then be opened and inspected ed do not print error details disable ew do not print warning details disable eL list error and warning details at the end Some common compilation problems e not enough variable space Solution Some redesign is required The scope of local variables can be made more narrow A better overlapping strategy for global variables can be tried e the compiler is unable to generate code Solution Some of the C statements have to be rewritten possibly using simpler statements e too much code generated Solution rewrite parts of the code By checking the assembly file it may be possible to detect inefficient code fragments Rewriting by using the W register directly may sometimes reduce the code size Experience has shown that around 10 of the hex code can be removed by hand optimizing the C code Optimal usage of the code pages and RAM banks is important Note that the code reduction estimate is compared to the initial code written e codepage limits are exceeded Solution move functions to another codepage by using the pragma codepage or location statements It is sometimes necessary to split a function into two separate functions e too deep call level Solution rewrite the code Remember that the compiler handles most cases where functions are called once only If there is a return array at the deepest call level this code can be moved to the calling function void s
23. above fixed point example to enable code size comparison include 16C73 h include math24f h unsi6 data float tx av mg a vx prev kp void main void InitFpFlags enable rounding as default vx 3 127 tx data automatic type cast data kp assign integer part if tx 0 tx tx make positive av tx 20 0 mg av 1 25 a mg 0 98 prev vx 62 CC5X C Compiler B Knudsen Data vx a 5 0 prev kp vx 0 036 kp vx 1 0 0 036 How to save code Choices that influence code size 1 What libraries to include 24 32 bit float or fixed point 2 Rounding can be disabled permanently Note that this will reduce the accuracy of the math functions slightly define DISABLE ROUNDING include math32f h 3 Optimization currently available on division only Note that speed is default Also note that the saving is only 5 7 instructions Timing difference is up to 15 20 percent define FP_OPTIM_SIZE optimize for SIZE define FP OPTIM SPEED optimize for SPEED default The recommended strategy is to select a main library for the demanding math operations Different floating and fixed point operations should only be mixed if there is a good reason for it Mixing different data types is possible to save code and RAM space For example using a small type in an array and a larger type for the math operations So first decide what
24. bank selection bits Some advise when locating variables 1 Try to locate variables which are close related to each other in the same bank Try to locate all variables accessed in the same function in the same bank 3 Switching between bank 0 and 3 or bank and 2 require more instructions than the other combinations 4 Use as few banks as possible Fill bank 0 first then bank 1 etc 5 Remember that local variables and function parameters also may require updating of the bank selection bits RAM Bank Selection Bits RAM and special purpose registers can be located in up to 4 banks The 12 bit core uses bit 5 and 6 in FSR to select the right bank In the 14 bit core RPO and RP1 in the STATUS register are used for this purpose The bank selection bits are automatically checked and updated by the compiler and attempts to set or clear these bits in the source code are removed by the compiler This feature can be switched off which means that correct updating has to be done in the source code The compiler uses global optimizing techniques to minimize the extra code needed to update the bank selection bits Removing all unnecessary updating is difficult However there should be few redundant instructions The compiler inserts the following instructions BCF 04h FSR 5 12 bit core 16C57 58 BSF 04h FSR 5 12 bit core 16C57 58 BCF 04h FSR 6 12 bit core 16C57 58 BSF 04h FSR 6 12 bit core 16C57 58 CLRF F
25. been found by simulating many thousands calculations The min and max limits are approximate only All timing is measured in instruction cycles When using a 4 MHz oscillator one instruction cycle is 1 microsecond Fast and compact inline operations The compiler will use inline code for efficiency at some important operations Integer converting to left and right shifts a 8 a 2 selecting high low bytes words a 256 a96256 b 0x 10000 replacing remainder by AND operation a2 64 a 0x80 Fixed Point converting to left and right shifts a 8 a 2 all operations except multiplication and division are implemented inline Floating point add sub incr decr of exponent a 128 0 a 2 operations and a b a 0 0 comparing with constants a gt 0 a lt 10 0 inverting the sign bit a a b a Using prototypes and multiple code pages Using floating point on the 12 bit core where each codepage is 512 words will be challenging It is normally not required to define prototypes even when using many code pages If you want to place the library functions to a certain codepage this is easiest done by pragma codepage 1 include math24f h pragma codepage 0 Prototypes can be used when functions are called before they are defined Note that operator functions need a function name to be defined as prototypes Also note that the compiler use a linear search from the beginning of the operator table until a match fo
26. define D28 x x 0x4000 x 0x4000 pragma cdata D7 10 20 D28 10234543 pragma cdata IDO 0x10 200 3000 pragma cdata ID1 Hello world 0 pragma cdata ID2 Another string r n merged pragma cdata ID TABLE IDO ID1 ID2 store addresses E ND end of cdata block pragma cdata CDATA pragma origin CDATA END program code follow here void write unsl6 strID write ID1 write ID2 The cdata statements should normally be stored after the program code AII cdata start addresses have to be decided manually The setup could be as follows cdata definitions C functions at addresses lower than CDATA START 81 CC5X C Compiler B Knudsen Data pragma origin CDATA START pragma origin CDATA END C functions at addresses higher than CDATA END If the address defined by CDATA START overlaps with function code then an error will occur when pragma origin CDATA START is used Statement pragma origin CDATA END checks that no invalid program code addresses is used It is recommended to specify the address only at the beginning of a continuous cdata block as shown Preprocessor statements can be used for checking size during compilation dif CDATA END CDATA START gt 20 derror This is too much endif 82 CC5X C Compiler B Knudsen Data 7 DEBUGGING Removing compilation errors is a simple task The real challenge is to reveal the many application bugs ALW A
27. defined in separate C modules MPLINK detects such collisions ro N add offset lt N gt on local variable block name Local variables for external available functions are allocated separately One block for each extern function This often means inefficiently use of RAM It is therefore recommended to use extern only on those functions that have to be extern and use few local variables in the extern functions Also consider using global variables Header files It is recommended to make common header files that contains global definitions that are included in all C modules Such files can contain definitions define IO variable names etc Using RAM banks RAM bank definitions only applies to devices with RAM located in more than one bank Note that the RAM bank of ALL variables have to be known defined during compilation Otherwise the bank bit updating will not be correct The bank is defined by using pragma rambank between the variable definition statements also for extern variables An alternative is to use the bank type modifier bankO bank3 shrBank pragma rambank 0 char a b pragma rambank 1 extern char arrayl 10 pragma rambank extern char ex shared common RAM 71 CC5X C Compiler B Knudsen Data Bank bit updating CC5X use an advanced algorithm to update the bank selection bits However it is not possible to trace calls to external functions Therefore calling an external function
28. in the beginning of the application but after the interrupt routine for all libraries located on codepage 0 interrupt routine finclude math16 h 16 bit integer math finclude math24f h 24 bit floating point finclude math241b h 24 bit math functions CC5X will automatically delete unused library functions This feature can also be used to delete unused application functions pragma library 1 library functions that are deleted if unused pragma library 0 Math libraries Integer 8 16 24 and 32 bit signed and unsigned Fixed point 20 formats signed and unsigned Floating point 16 24 and 32 bit All libraries are optimized to get compact code All variables except for the floating point flags are allocated on the generated stack to enable efficient RAM reuse with other local variables A new concept of transparent sharing of parameters in a library is introduced to save code 55 CC5X C Compiler B Knudsen Data Note that fixed point requires manual worst case analysis to get correct results This must include calculation of accumulated error and avoiding truncation and loss of significant bits It is often straight forward to get correct results when using floating point However floating point functions requires significantly more code In general floating point and fixed point are both slow to execute Floating point is FASTER than fixed point on multiplication and division but slower on most ot
29. infinite loop for statement for initialization condition lt increment gt statement initialization all legal assignments or empty 29 CC5X C Compiler B Knudsen Data condition all legal conditions or empty increment incrementing or decrementing a variable or empty for v 0 v lt 10 vtt for p wx x T0 setzt E ze for v 0 v do statement do statement while condition switch statement switch variable case constantl lt statement gt lt statement gt break case lt constant2 gt lt statement gt lt statement gt break default lt statement gt lt statement gt break lt variable gt all 8 bit variables including W break optional default optional can be put in the middle of the switch statement switch token case 2 i 2 break case 9 case 1 default if PORTA 0x22 break case P pinl 0 i 2 break break statement The break statement is used inside loop statements for while do to terminate the loop It is also used in switch statements while 1 if var 5 break 30 CC5X C Compiler B Knudsen Data continue statement The continue statement is used inside loop statements for while do to force the next iteration of the loop to be executed skipping any code in between In while and do while loops the loop condition is executed next In for
30. is manipulated in the user code computed goto in a function of type char b The number of return literal exceeds 10 c The function is called from another codepage and the number of returns exceeds 10 2 Call followed by return is replaced by a single goto When subroutines are located in the second half of a codepage it can not be called directly when using 12 bit core devices The compiler automatically inserts links to such hidden subroutines Stack level checking when using interrupt CC5X will normally assume that an interrupt can occur anywhere in the main program also at the deepest call level An error message is printed if stack overflow may occur This is not always true 52 CC5X C Compiler B Knudsen Data because the interrupt enable bits controls when interrupts are allowed Sometimes the main program need all 8 stack levels for making calls The q lt N gt option force CC5X to assume that interrupt will NOT occur at the N deepest call levels of the main program The application writer must then ensure that interrupt will not occur when executing functions at the deepest lt N gt call levels normally by using the global interrupt enable bit CC5X will generate a warning for the critical functions The normal error message is always generated when the application contains more than 8 call levels For example the q1 option generates a warning for functions calls that will put the return address at stack level
31. loops the increment is processed before the loop condition for i 0 i 10 itt f if i 7 continue return statement return expression exits the current function return no return value return itl return value goto statement goto label Jumps to a location forward or backward goto XYZ XYZ 3 2 Assignment and Conditions lt variable gt lt value gt lt operation gt lt value gt lt variable gt lt operation gt lt value gt lt variable gt lt variable gt variable of a supported type operation amp STEI e RE news value variable or constant constant 34 OxFF Ai 0b01101111 varli x y W W amp Ox1F i x 100 yes TAPS yay AS W 3 W W 3 flag 1 bit variable H LA itt increment i ek decrement i i 1 Special syntax examples define mx a if mx 31 CC5X C Compiler B Knudsen Data W W 3 ADDLW 256 3 b fx v 35 Post and pre incrementing of pointers char ep t Cp E ep Pepa e ES t cpt 10 extended pre incrementing of variables t b 3 sum b 10 b saat bI Conditions variable lt cond oper gt value amp amp condition condition cond oper f gt gt lt lt if x dE f amp amp al lt a2 if y gt 44 Carry x z if ees gt 0
32. lt pl func2 ocdo 0 D delay 42 p0 5p2 func3 3 pO gt pO The meaning of the symbols is func2 delay and func3 are called from functionl 1 function is called once 3 func3 is called 3 times once from function pO lt pl function resides on page 0 pO lt pl function is called from page 1 pO p3 call to func2 resides on page 3 one pagebit have to be updated before call both pagebits have to be updated oO car ON SO PO The call structure is also expanded recursively LO main L1 function1 L2 func2 L2 delay L2 func3 LI functionl Explanation of symbols used e LI stack level 1 max 2 or 8 levels This is the REAL stack level compensated when CALL s have been replaced by GOTO e only the first call is fully expanded if more that one call to the same function occur inside the same function body e CALL gt GOTO CALL replaced by GOTO in order to get more call levels e T GOTO CALL RETURN is replaced by GOTO to save a call level e RECURSIVE recursive function call 90 CC5X C Compiler B Knudsen Data 9 APPLICATION NOTES 9 1 Delays Delays are frequently used There are various methods of generating them Instruction cycle counting Use of the TMRO timer Watchdog timeout for low power consumption Use of variables achieves longer intervals BU E void delay char millisec delays a multiple of 1 millisecond at 4 MHz OPTION 2 prescal
33. missing CC5X will read the error file generated by MPASM and write the error and warnings found there to the screen and the output file occ The error file is then deleted If the CC5X error file option F is present CC5X will write error and warnings to the error file err and append the error and warnings generated by MPASM at the end of this file Note that MPLAB will automatically show the contents of the error file if it is non empty Otherwise the screen output is displayed in Build Results window The MPLINK script file MICROCHIP supplies sample linker script files for each device with the file extension Ikr look in the MPLAB directory When making a linker script file for a specific project this file can be copied and edited to suit the needs of CC5X The changes are general and valid for several projects The sample MPLINK script files must be changed slightly if the interrupt function is written in C The reason is that the interrupt function must start at address 4 when using CC5X It could be possible to use a vector at address 4 but this slows down interrupt response Anyway using a goto vector directly is not possible when the device contains more than 2048 words of code This is because PCLATH needs to be saved before it can be updated CHANGE 1 Interrupt routine in C WITH a separate logical section CC5X generates a partial script file when using the r2 or r2 file Ikr command line option This file i
34. of inline code The algorithm makes shortcuts when possible for instance when multiplying by 2 This is treated as a left shift division S al6 b16 c8 16 8 bit modulo a32 b32 c16 32 16 bit The division algorithm also allows most combinations of variable sizes Shortcuts are made when dividing by 2 or 2 2 These are treated as right shifts Precedence of C operators Highest C Lowest H EC 33 CC5X C Compiler B Knudsen Data Mixed variable sizes are allowed a32 uns32 b24 c8 24 8 bit result 32 bit al6 al6 b8 16 8 bit result 16 bit Most combinations of variables are allowed the compiler performs sign extension is required Multiple operations in the same expression are allowed when using 8 bit variables a8 b8 c8 d8 10 3 3 Constants x 34 decimal x 0x22 hexadecimal x A ASCII x 0b010101 binary x 0x1234 256 0x12 MSB x 0x1234 256 0x34 LSB x 33 4 1 x OxF amp OxF3 3 x Ox2 0x8 10 x 0x2 OXF 0b1101 x 0b10 lt lt 2 jog ES x err 3 8 2 L 22 4 x rl 3 99 67 2 167 x OxF amp OxF3 1 4 16 Please note that parentheses are required in some cases Constant Expressions The size of integers is by default 8 bits for this compiler other C compilers use typically 16 or 32 bits dep
35. only p no in front of decimal constants f no object format directive is printed t no tabulators normal spaces only m single source line only i no source indentation straight left margin J put source after instructions to achieve a compact assembly file b do not add rambank info to variables in the assembly file e do not add 1 to instructions when result is written back to the register 0 do not replace OPTION with OPTION REG k do not convert all hexadecimal numbers 11h gt 0x11 g do not use PROCESSOR instead of the list directive N N N label mnemonic and argument spacing Default is 8 6 10 b do not update bank selection bits 12 bit core FSR 5 and 6 14 bit core STATUS RPO and RP1 bu non optimized updating of the bank selection bits CC lt file gt produce COD file C mode CA lt file gt produce COD file ASM mode dc do not write compiler output file lt src gt occ D lt name gt xxx define macro Equivalent to define name xxx e single line error messages no source lines are printed ed do not print error details ew do not print warning details eL list error and warning details at the end E lt N gt stop after lt N gt errors default is 4 f lt hex file format gt i e INHX8M INHX8S INHX16 INHX32 Default is INHX8M Note that INHXSS use output files file HXH and file HXL F produce error file src err g do not replace call by goto
36. or allowing incoming calls makes CC5X assume that the bank bits are undefined This often means that more code compared to the optimal bank bit update strategy It is therefore recommended to only use extern on those functions that have to be extern and keep the number of calls between modules to a minimum Functions Functions residing in other modules can be called Functions defined can be called from other modules also from assembly modules NOTE that ALL functions that are called from another module needs an extern first This is an extra requirement that is optional in C The reason is that the compiler needs to decide the strategy on bank bit updating and local variables allocation It is most efficient to use FEW extern functions extern void funcl void defined in another module extern void fc2 void available to all modules NOTE that extern functions can only have a single 8 bit parameter which is transferred in W This is because local storage information is not shared between modules The return value can not be larger than 8 bit for the same reason bit values are returned in Carry CC5X inserts a GLOBAL lt function gt in the generated assembly code for all external available functions EXTERN function is inserted for functions defined in other modules If the C module contains main then a goto main is inserted in the STARTUP section Using code pages Page bit updating only applies to functi
37. pragma config 0x100 set bit 8 pragma config amp OxFFFC clear bit 0 and 1 pragma config amp 3 clear bit 0 and 1 pragma config 3 set bit 0 and 1 More than one Zpragma config statement is possible The default setting of the attributes is O The operators l and amp can be used to achieve high level readability define CP off 0x3F30 16C62X and similar pragma config CP off FOSC LP Programming of ID locations pragma config ID 0x1234 all 4 locations 4 4 bit pragma config ID 0 OxFFF location 0 pragma config ID 1 0x010 location 1 pragma config ID 2 1 ID 3 0x23 The config and ID information is added at the end of the generated assembly and hex file Configuration word address e 12 bit core OXFFF e 14 bit core 0x2007 ID words PICmicro ADDRESS 16C54 55 0x200 0x203 4 locations 16C56 0x400 0x403 4 locations 16C57 58 0x800 0x803 4 locations 14 bit core 0x2000 0x2003 4 locations 47 CC5X C Compiler B Knudsen Data 5 COMMAND LINE OPTIONS The compiler needs a C source file name to start compiling Other arguments can be added if required The syntax is CC5X options src c options a lt asmfile gt produce assembly file The default file name is lt src gt asm A scHDpftmiJbeokgN N N assembly file options s symbolic arguments are replaced by numbers c no C source code is printed H hexadecimal numbers only D decimal numbers
38. rr swap nop and nop2 Carry can be used together with rl and rr Direct use of Zero should be avoided 5 Use of the W register Spon The recommended sequence is to 1 Write the program for the actual PICmicro device 2 Continue working until it can be compiled successfully 3 Debug low level modules separately by writing small test programs i e for keyboard handling displays IIC bus IO RT clocks 86 CC5X C Compiler B Knudsen Data 4 Add the necessary SIM code and definitions to the code Debug parts of the program in another environment Writing alternative code for the low level modules is possible 5 Return to the PICmicro environment and compile with SIM switched off and continue debugging using the actual chip File debug txt contains definitions that are useful when running a PICmicro application program in another environment using a standard C compiler 87 CC5X C Compiler B Knudsen Data 8 FILES PRODUCED The compiler produces a compiler output file and a hex file that may be used for programming the PICmicro chips directly The hex file is produced only there are no errors during compilation The compiler may also produce other files by setting some command line options e assembly variable list function outline COD and error files 8 1 Hex File The default hex file format is INHX8M The format is changed by the f command line option The INHX8M INHXSS and INHX32 formats are
39. source file and produce the required files Starting CC5X from Windows can be done by selecting the Start gt Run menu Then type the full path name including cc5x or use Browse The list of compiler command line options are then written to the screen The normal way of using CC5X is to use it as a tool from an integrate environment like MPLAB Compiling a program in a MSDOS window requires a file name and command line options cc5x a samplel c enter CC5X C Compiler B Knudsen Data 1 3 MPLAB Support CC5X can be selected as a tool in MPLAB which offers an integrated environment including editor and tool support compilers assemblers simulators emulators device programmers Compilation errors are easily handled MPLAB supports point and click to go directly to the source line that needs correction CC5X supports the COD file format used by MPLAB for program debugging CC5X offers two modes of source file debugging is available C or assembly mode Thus tracing programs in MPLAB can be done using assembly instructions or C statements MPLAB is free and can be downloaded from the Microchip Internet site Please refer to the supplied file install txt for a description on how to install and use CC5X in the MPLAB environment 1 4 Summary of Delivered Files CC5X EXE compiler INTRO TXT introduction INSTALL TXT installation guide and MPLAB setup CC5X TXT basic documentation on CC5X PRAG
40. the size of single const pointers can be chosen automatically e const pointers can access both RAM and program memory e the compiler will not put all constant data in a single table but rather make smaller tables if this saves code space 26 CC5X C Compiler B Knudsen Data e some devices supports 14 bits data PIC16F87X The compiler will automatically chose this format if space can be saved This allows very compact storage of 7 bit ASCII strings e duplicate strings and other data are automatically merged to save space Recommendations It is recommended to use small data tables and structures This allows the compiler to merge equal data items and build optimal blocks of constant data Limitations 1 The compiler will not initialize RAM variables on startup 2 Data items of 16 bit or more in structures with more than 256 byte data must be aligned Storing 14 bit data Some flash devices PIC16F87X supports 14 bits data stored in program memory This allows compact storage of 7 bit ASCII strings and 14 bits data The code sequence required for accessing these bits is longer than the code for a return table This means that code is saved when the amount of data exceeds 40 50 bytes The compiler will automatically chose the optimal storage format The 14 bit data format allows storing up to 16000 bytes of 7 bit ASCII text on a 8k device The compiler will use the 14 bit format when a pragma statement is used This is normally found i
41. thl 0x25 1 overlap warning bit thi th 1 no warning char tty bit b0 char io tty bit bx0 b bit bx2b tty 7 char tui b size exceeded long r tty size exceeded char tab 5 long tr tab struct long tiM long uu ham tab Pragma statements can also be used pragma char port PORTC pragma char varX 0x23 pragma bit IOpin PORTA 1 pragma bit ready H 0x20 2 pragma bit ready PA2 If the compiler detects double assignments to the same RAM location this will cause a warning to be printed The warning can be avoided if the second assignment use the variable name from the first assignment instead of the address Zpragma char var2 varl An alternative is to use the define statement define PORTX PORTC define ready PA2 Priority when allocating variables 1 Variables permanently assigned to a location 2 Local variables allocated by the compiler 3 Global variables allocated by the compiler Supported type modifiers static char a a global variable known in the current module only or having the same name scope as local variables when used in a local block extern char a global variable in another module auto char a local variable auto is normally not used 18 CC5X C Compiler B Knudsen Data register char a ignored type modifier const char a const tells that compiler that the data is not modifi
42. wO warning on operator library calls WP warning when code page bits are not correct Wr no warning on recursive calls wS warning no error when constant expression loses significant bits wU warning on uncalled functions cu use 32 bit evaluation of constant expressions x lt file gt assembler executable xC progra 1 mplab mpasm exe X lt option gt assembler option X q all options must be separate W wait until key pressed after compilation A path name can be written using if this is supported by the file system example c compiler lib file h Default compiler settings e hex file output on file lt name gt hex processor 16C54 optimizing on extended call level is allowed update bank and page selection bits Permanent assigned settings e nested comments is allowed e charis unsigned 5 1 Options on a file Options can be put on a file The syntax is cc5x lt filename gt Many option files can be included and up to 5 levels of nested include files are allowed Options on a file allows an unlimited number of options to be stated Linefeed space and TAB separates each option Comments can be added in the option file using the syntax the rest of the line is a comment Spaces can be added to each option if a space is added behind the starting the option This syntax disables using more than one option on each line Examples D MAC 1 OP p 16C54 comment p 16C54
43. whole text field AND the type identifier or none of them Macro s limited to a part of the text field are not translated Macro s can be used to switch on and off a group of assert statements or to define similar assert statements define COMMON ASSERT a text field define AA pragma assert COMMON ASSERT pragma assert AA a text field Macro AA can also disable a group of assert statements if writing define AA define XX a this will NOT work pragma assert XX causes an error message 7 4 Debugging in Another Environment Testing a program larger than 500 1000 instructions can be difficult It is possible to debug parts of the program in the MSDOS environment Another C compiler have to be used for this purpose Using another environment has many advantages like faster debugging additional test code use of printf use of powerful debuggers etc The disadvantage is that some program rewriting is required All low level activity like IO read and write have to be handled different Conditional compilation is recommended This also allows additional test code to be easily included ifdef SIM simulated sequence or test code printf statements etc else low level PICmicro code endif The following can be compiled and debugged without modifications General purpose RAM access Bit operations overlapping variables requires care Use of FSR and INDF with some precautions Use of rl
44. 0625 fixed16 16 4 2 2 32768 32767 99998 0 000015259 fixed24_ 8 4 3 1 8388608 8388607 996 0 00390625 fixedU8_8 2 1 1 0 255 996 0 00390625 fixedU8_16 3 1 2 0 255 99998 0 000015259 fixedU8 24 4 1 3 0 255 99999994 0 000000059605 fixedU16 8 3 2 1 0 65535 996 0 00390625 fixedU16 16 4 2 2 0 65535 99998 0 000015259 fixedU24_8 4 3 1 0 16777215 996 0 00390625 additional types with decimals only no integer part fixed 8 1 0 1 0 5 0 496 0 00390625 fixed 16 2 0 2 0 5 0 49998 0 000015259 fixed 24 3 0 3 0 5 0 49999994 0 000000059605 16 CC5X C Compiler B Knudsen Data fixed 32 4 0 4 0 5 0 4999999998 0 0000000002328 fixedU 8 1 0 1 0 0 996 0 00390625 fixedU 16 2 0 2 0 0 99998 0 000015259 fixedU 24 3 0 3 0 0 99999994 0 000000059605 fixedU 32 4 0 4 0 0 9999999998 0 0000000002328 To sum up 1 All types ending on _8 have 2 correct digits after decimal point 2 All types ending on _16 have 4 correct digits after decimal point 3 All types ending on _24 have 7 correct digits after decimal point 4 Alltypes ending on 32 have 9 correct digits after decimal point Fixed point constants The 32 bit floating point format is used during compilation and calculation fixed8 8 a 10 24 fixedl16_8 a 8 1 23 fixed8_16 x 2 3e 3 fixed8 16 x 23 45e1 fixed8 16 x 23 45e 2 fixed8 16 x 0 fixed8 16 x 1 23 Constant rounding error example Constant 0 036 Va
45. 1 7 What to do next It is important to know the PICmicro family and the tools well The easiest way to start is to read the available documentation and experiment with the examples Then move on to a simple project Some suggestions e study the supplied program samples e compile code fragments and check out what the compiler accepts e study the optional assembly file produced by the compiler Note that using more than one ram bank or code page requires pragma instructions Typical steps when developing programs is as follows e describe the system make requirements suggest solutions that satisfy these requirements write detailed code in the C language compile the program using the CC5X compiler test the program on a prototype or a simulator Writing programs for the PICmicro microcontroller family requires careful planning Program and RAM space are limited and the key question is often Will the application code fit into the selected controller Important File readme txt contains information on how to write code that can be compiled by CC5X 12 CC5X C Compiler B Knudsen Data 2 VARIABLES The compiler prints information on the screen when compiling Most important are error messages and how much RAM and PROGRAM space the program requires The compiler output information is also written to file occ Example delay c Chip 16C74 RAM OOh RA 20h KKKKK KKKKKKKK KKKKKKKK KKKKKKKK
46. 16_16 16_16 85 1546 1650 2097 16 16 16 16 16 16 57 1617 1733 2305 S 8_24 8_24 8 24 77 529 672 896 8_24 8_24 8_24 35 517 658 933 S 8_24 8_24 8 24 85 1794 1936 2433 8_24 8_24 8 24 57 1872 2033 2680 Floating point libraries mathl6f h 16 bit floating point basic math ath24f h 24 bit floating point basic math ath241b h 24 bit floating point library math32f h 32 bit floating point basic math math321b h 32 bit floating point library NOTE The timing values includes parameter transfer call and return and also assignment of the return value Basic 32 bit math S a b multiplication a b division a b addition a b subtraction ad int32 float32 float32 gt int32 Basic 24 bit math S a b multiplication a b division a b addition a b subtraction ad ize 91 125 182 d 5 79 89 ize 77 102 152 dt 5 kkk Timing KKKK min aver max 380 468 553 523 610 742 39 135 225 46 142 232 44 77 114 35 90 142 kk Timing KKKK min aver max 226 261 294 323 359 427 33 114 173 58 CC5X C Compiler B Knudsen Data int24 gt float24 float24 int24 Basic 16 bit math AK TIM ing EREK Size min aver max a b multiplication 62 104 107 114 a b division 82 137 154 171 a b addition 118 Zul 86 130 a b subtraction add 5 int16 gt floatl6 float16 gt int16 The following operations are handled by inline code assignment
47. 20 30 a b 0 Examples A table of pointers to strings const struct const char s E SP 4 Hello world Monday Ve world automatically merged with first string p tb il s const char p char i t pt char t t pI x char x Note that const struct is required to put the pointer array in program memory Using const char tx means that the strings resides in program memory but the table tx resides in RAM String parameters myfunc Hello void myfunc const char str myfunc amp tab i char tab 20 string in RAM myfunc ctab const char ctab A string 28 CC5X C Compiler B Knudsen Data 3 SYNTAX 3 1 Statements C statements are separated by semicolons and surrounded by block delimiters statement statement The typical statements are if while for do switch break continue return goto assignment function call while 1 k 3 X if PORTA 0 for i 0 i lt 10 i pin_l 0 do a sample a rr a S a while s lt 200 reg 1 if PORTA return 5 else if count 3 goto X if PORTB 0 break if statement if condition statement else if condition statement else statement The else if and else parts are optional while statement while condition statement while 1
48. 6 5 Library Support CC5X uses stores LOW ORDER FIRST This means that the least significant byte of a variable is assigned to the lowest address All variables are allocated from low RAM addresses and upwards Each 13 CC5X C Compiler B Knudsen Data RAM location can contain 8 bit variables Address regions used for special purpose registers are not available for normal allocation An error message is produced when there is no space left Special purpose registers are either predefined or defined in chip specific header files This applies to W INDF TMRO PCL STATUS FSR Carry PD TO etc Integer variables unsigned a8 8 bit unsigned char a8 8 bit unsigned unsigned long i16 16 bit unsigned char varX char counter L byte H byte bit ready 0 or 1 bit flag stop semafor int i 8 bit signed signed char sc 8 bit signed long i16 16 bit signed uns8 u8 8 bit unsigned unsl6 ul6 16 bit unsigned uns24 u24 24 bit unsigned uns32 u32 32 bit unsigned int8 s8 8 bit signed int16 s16 16 bit signed int24 s24 24 bit signed int32 s32 32 bit signed The bitfield syntax can also be used unsigned x 24 24 bit unsigned int y 16 16 bit signed The value range of the variables are TYPE SIZE MIN MAX int8 1 128 127 int16 2 32768 32767 int24 3 8388608 8388607 int32 4 2147483648 2147483647 uns8 1 0 255 unsi6 2 0 65535 uns24 3 0 16777215 uns32 4
49. 7 Data f size 10 Dit or more rir it re RE REEF ER E VUES PERENNE EIER Gun 27 Code PAGES ttu iit ri rer DURO re PH ENEE ee EEN EEN E Rea eh 28 Merging dats sirisser areis etae rer n P ee te er RD D ridere ue otra don 28 Examples egit ee eie eei eere tee e ete pter ied 28 S MESA dy HU o Quoad RE 29 SMS gU Id RM V Ster 29 Ef statemieht ite mte ano e eR eh nme n nets 29 while statemeht 4x5 uii esas edant ei etse ed aiti OES 29 RE 29 do statement ine eu Seet dee ee Ee dei ae tie I dei etes eee eese ec eet eus 30 Sw ch statement 3 senno EE idee tese dece PATI Sa aN tovc eet evo 30 break statement hs eie dee tee dri cites i bo PAT eese ec eet e 30 COntinue StatemieHl seis dee teer eI ede eeh ied eere I ee eeh ed Eed eeh deg ed eve ed EEN eeh ed ee ed eeh ge 3l Lee 3l BOLO Ee MEss ege vse Gate ads sacp ta sagvsdedondanyagsadods ise cased E deigvaeaave tush S E E ERE I E A 31 3 2 ASSIGNMENT AND CONDITIONS cscccsseeseeescesseesecesecseceaecenecseecaeeeaeeeceseeeseeesesesecaeeaecaeceneeeneeeneees 31 CC5X C Compiler B Knudsen Data Special E 31 EE 32 aU EE 32 Multiplication division and mode 33 Precedence of G operators e dieere e eet e PITE Pe e HER EE 33 Mixed variable sizes are allowed essent eene eerte nnne 34 33 CONSTANT EE 34 CONSTANT EXPLESSIONS E 34 JO rdc mcn 35 JA IBUNGTIONS 7 5 eee nene eR Pee pe deb e 35
50. Allows the automatic updating of the page selection bits to be swiched on and off locally This is not recommended except in special cases The page bits resides in the STATUS register for core 12 devices and in PCLATH for core 14 0 OFF 1 ON pragma update PAG pragma update PAG pragma update RP 0 1 Allows the automatic updating of the bank selection bits RPO and RPI to be switched on and off locally The statement is ignored when using the 12 bit core 46 CC5X C Compiler B Knudsen Data pragma update RP 0 OFF pragma update RP 1 JE Q9N These statements can be inserted anywhere but they should surround a region as small as possible pragma wideConstData lt N gt Enable storing of 14 bit data for 14 bit core devices Details is found in Section 2 5 Const Data Support 4 2 PlCmicro Configuration PICmicro configuration information can be put in the generated hex and assembly file ID locations can also be programmed The configuration information is generated IF AND ONLY IF the Zpragma config statement is included Note that some PICmicro programming devices may reject this information especially setting of the ID locations Syntax pragma config id state id lt state gt id PWRTE WDTE FOSC BODEN ID state on off LP HS XT RC number lt number gt pragma config WDTE off FOSC HS pragma config WDTE 0 FOSC 2 PWRTE 1
51. BBaaaaTT112233 CC BB number of data words of 8 bits max 16 aaaa hexadecimal address TT type 00 normal objects 01 end of file 00000001FF 13 8 bits data word CC checksum the sum of all bytes is zero The 16 bit format used by INHX16 is defined by BBaaaaTT111122223333 CC BB number of data words of 16 bits max 8 aaaa hexadecimal address TT type 00 normal objects 01 end of file 00000001FF 1111 16 bits data word CE checksum the sum of all bytes is zero 8 2 Assembly Output File The compiler produces a complete assembly file This file can be used as input to an assembler Text from the source file is merged into the assembly file This improves readability Variable names are used throughout A hex format directive is put into the assembly file This can be switched off if needed Local variables may have the same name The compiler will add an extension to ensure that all variable names are unique There are many command line options which change the assembly file produced Please note the difference between the a and the A options The a option is needed to produce an assembly file while the A option changes the contents of the assembly and list files The general format is A scHDpftmiJbeokgN N N s symbolic arguments are replaced by numbers c no C source code is printed H hexadecimal numbers only D decimal numbers only p no in front of decimal constants f n
52. CC5X C Compiler for the PICmicro Devices Version 3 1 User s Manual D 4 B Knudsen Data Trondheim Norway CC5X C Compiler B Knudsen Data This manual and the CC5X compiler is protected by Norwegian copyright laws and thus by corresponding copyright laws agreed internationally by mutual consent The manual and the compiler may not be copied partially or as a whole without the written consent from the author The PDF edition of the manual can be printed to paper for private or local use but not for distribution Modification of the manual or the compiler is strongly prohibited All rights reserved LICENSE AGREEMENT By using the CC5X compiler you agree to be bound by this agreement Only one person may use a licensed CC5X compiler at the same time If more than one person want to use the compiler for each license then this have to be done by some manual handshaking procedure not electronic automated for instance by exchanging this manual as a permission key You may make backup copies of the software and copy it to multiple computers You may not distribute copies of the compiler to others B Knudsen Data assumes no responsibility for errors or defects in this manual or in the compiler This also applies to problems caused by such errors Copyright B Knudsen Data Trondheim Norway 1992 2000 This manual covers CC5X version 3 1 and related topics New versions may contain changes without prior notice Microchip and PIC
53. CC5X offers 3 ways to select the PICmicro device in an application 1 By a command line option In MPLAB the device command line option is selected in Project gt Edit Project target Node Properties p16C73 pPIC16F877 2 By a pragma statement in the source code pragma chip PIC16F84 3 By an include file include 16c73 h NOTE 1 When using a pragma statement or header files remember to use this the beginning of the C program so that it is compiled first However some preprocessor statements like define and if may proceed the include pragma statement NOTE 2 When using the command line option or the pragma statement CC5X will use the internal definitions for some devices If the device is not known internally automatic include of a header file is started The internal known devices are 16C54 55 56 57 58 61 64 65 71 73 74 84 620 621 622 NOTE 3 If the header file does not reside in the default project folder then the path name is required This can be supplied by a command line option as an include folder directory Ipath NOTE 4 New header files can be defined according to file chip txt NOTE 5 ICD debugging requires defining a symbol first a Command line options DICD DEBUG p16F877 b define ICD DEBUG or by command line DICD DEBUG pragma chip PIC16F877 11 CC5X C Compiler B Knudsen Data c define ICD_DEBUG or by command line DICD_DEBUG include 16F877 H
54. Goto Regions affects 1 Optimization 2 Register bank bit updating RPO 1 FSR5 6 3 256 word page checks char sub01 char W The computed goto region can be constructed just as in assembly language However pragma computedGoto should be inserted around such a region Otherwise unexpected results may occur fpragma computedGoto 1 PCLATH 0 14 bit core only PCL W 14 bit core REMEMBER to make sure that the function is located within the first 256 addresses There is no warning on this when skip W is NOT used return H return e return III return bt return Toi pragma computedGoto 0 A VERY LARGE TABLE with more than 256 byte can also be constructed 14 bit core char L_dst H_dst char sub02 void H_dst L_dst index to the desired element starting from 0 define CGSTART 0x100 PCLATH CGSTART 256 H_dst MSB offset PCL L_dst GLOBAL JUMP AT THIS POINT return W dummy return never executed IMPORTANT THIS FUNCTION AND THE DESTINATION ADDRESSES HAVE TO BE LOCATED IN THE SAME 2048 WORD CODEPAGE OTHERWISE PCLATH WILL NOT BE CORRECT ON RETURN U pragma origin CGSTART the starting point The origin statement is the best way to set the starting point of the large return table The address should be defined
55. H Hello world pragma return 5 1 4 5 6 7 pragma return 0 1 2 3 44 H Hello 2 3 4 0x44 pragma return H e 1 MIT To pragma return 3 0b010110 Ob111 0x10 pragma return 9 a VN r n o pragma return 10 10 2 0x80 E nd pragma return 10000 16 16 bit constant 0x123456 24 24 bit constant 10000 10000 32 32 bit constant pragma stackLevels lt n gt The number of call levels can be defined normally not required The 12 bit core use by default 2 levels The 14 bit core use by default 8 levels fpragma stackLevels 4 max 64 pragma update FSR 0 1 Allows the automatic updating of the bank selection bits FSR 5 and FSR 6 to be switched on and off locally This can be useful in some cases when INDF is used directly in the user code The statement works for core 12 devices with more than one RAM bank It is ignored for the other devices pragma update FSR 0 OFF fpragma update FSR 1 ON These statements can be inserted anywhere but they should surround a region as small as possible pragma update IRP 0 1 Allows the automatic updating of the indirect bank selection bit IRP to be switched on and off locally The statement is ignored when using the 12 bit core pragma update IRP 0 OFF pragma update IRP 1 ON These statements can be inserted anywhere pragma update PAGE 0 1
56. ISB char PCLATH INTCON bit PSO PS1 PS2 PSA TOSE TOCS INTEDG RBPU bit RTE RTS optional bit Carry DC Zero PD TO RPO RP1 IRP bit RBIF INTF TOIF RBIE INTE TOIE GIE bit RTIF RTIE optional bit PAO PA1 PCLATH A3 Assembly Instructions Assembly Status Function NOP E No operation MOVWF f f W Move W to f CLRW Z W 0 Clear W CLRF f Z f 0 Clear f SUBWF f d C DC Z d f W Subtract W from f DECF fd Z dhs f 1 Decrement f IORWF f d Z d f W Inclusive OR W and f ANDWF t d Z d f amp W AND W and f XORWF f d Z d f W Exclusive OR W and f ADDWF f d C DC Z d f W Add W and f MOVF f d Z d f ove f COMF f d Z d f 255 Complement f INCF f d Z d f 1 Increment f DECFSZ f d B Decrement f skip if zero RRF td C Rotate right f through carry bit RLF d C Rotate left f through carry bit SWAPF f d Swap halves f INCFSZ f d Increment f skip if zero BCF 45 f b 0 Bit clear f BSF Eb Ge f b 1 Bit set f 98 CC5X APPENDIX BTFSC f b E Bit test f skip if clear BTFSS f b Bit test f skip if set OPTION OPTION W Load OPTION register SLEEP TO PD Go into standby mode WDT 0 CLRWDT TO PD WDT 0 Clear watchdog timer TRIS f Tristate port f 5 6 7 RETLW k Return put literal in W CALL k Call subroutine GOTO k Go to address MOVLW k
57. MA TXT the pragma statement INLINE TXT information on inline assembly syntax DEBUG TXT debugging details MPLAB support CHIP TXT how to make new chip definitions CONFIG TXT the PICmicro configuration bits CDATA TXT info on the pragma cdata statement CONST TXT standard C strings and constant data GLOBDEF TXT PICmicro registers STARTUP TXT special startup sequences LINKER TXT how to link several modules C or asm INT16CXX H interrupt header file INLINE H emulating inline instructions CC5X MTC MPLAB tool configuration file TLCC5X INI MPLAB tool configuration file OP INC command line options on a file RELOC INC options for object modules MPLINK SAMPLE1 C minimal program example IICBUS C IIC bus interface IIC COM C IIC bus communication SERIAL C serial communication RS232 RS485 STATE C state machines DELAY C implementing delays INT16XX C simple interrupt example MATH TXT Math library support MATH16 H 8 16 bit math library MATH16M H 8 16 bit multiply speed 1 ATH24 H 8 24 bit math library 1 MATH24M H 8 24 bit multiply speed 2 MATH32 H 8 32 bit math library 2 MATH32M H 8 32 bit multiply speed CC5X C Compiler B Knudsen Data 1 MATH16X H 16 bit fixed point library 1 MATH24X H 24 bit fixed point library 2 MATH32X H 32 bit fixed point library 2 MATH16F H 16 bit floating point library MATH24F H 24 bit floating point libra
58. NLY way to transfer multiple parameters and parameters different from 8 bit is to end assembly mode use C syntax and restart assembly mode again endasm f nci a L0 6 asm Some instructions are disabled depending on core type option 12 bits core only tris PORTA 12 bits core only movwf OPTION 14 bits core only movwf TRISA 14 bits core only The EQU statement can be used for defining constants Assembly blocks containing EQU s only can be put outside the functions Note that Equ constants can only be accessed in assembly mode Constants defined by define can be used both in C and assembly mode 65 CC5X C Compiler B Knudsen Data asm BO equ 0 B7 equ 7 MAXNUM2 4EXP equ OxFF endasm Equ can also be used to define variable addresses However the compiler do not know the know the difference between an Equ address and an Equ constant until it is used by an instruction When an Equ symbol is used as a variable that location is disabled for use by other variables The symbol then changes from an Equ symbol to a variable symbol and is made available in C mode also There is a slight danger in this logic DO NOT USE a series of Equ s to define an array If one of the locations are not read or written directly the compiler will not know that it is a part of an array and may use it for other purposes Reading and writing through FSR and INDF is not used to transform equ definitions Therefore define arrays by using C s
59. RA 40h CkCkCkck ck ckck o KKKKKKKK KKKKKKKK KKKKKKKK RA 60h KKKKKKKK KKKKKKKK KKKKKKKK KKKKKKKK RAM 80h RA AOh ACkCkCkck ck ckck o KKKKKKKK KKKKKKKK KKKKKKKK RAM COh CkCkCkck ck ckck o Ck kckckck ck ck ck o Ckckck ck ck ck kk Ckck ck kc k kk RA d EOh KKKKKKKK KKKKKKKK KKKKKKKK KKKKKKKK Optimizing removed 11 instructions 14 Si File delay asm Codepage 0 has 68 word s 3 Codepage 1 has 0 word s 0 File delay hex Total of 68 instructions 1 Si 2 1 Information on RAM allocation The compiler prints information on RAM allocation This map is useful to check out which RAM locations are still free The map for the 16C57 chip may look like this Mapped RAM 00h EN Ed Bank 0 RAM 10h 4 Bank 1 RAM 30h 6 KKKKKKKK Bank 2 RAM 50h x KKKKKKKK Bank 3 RAM 70h VJ k k kkkkx Symbols free location predefined or pragma variable local variable s g global variable 7 7 free bits in this location 16C71 map RAM 00h 3 7 XO RAM 20h KKKKKKKK KKKKKKKK 2 2 Defining Variables CC5X supports integer fixed and floating point variables The variable sizes are 1 8 16 24 and 32 bit The default int size is 8 bit and long is 16 bit Char variables are unsigned by default and thus range from 0 to 255 Note that 24 and 32 bit variables are not supported by all CC5X editions Math libraries have to be included for math operations Section
60. SR ZE 42 pit Gore 1657 58 BCF 03h RPO 14 bit core BSF 03h RPO 14 bit core BCF 03h RP1 14 bit core BSF 03h RP1 14 bit core NOTE The compiler REMOVES all bank updating done by the user Actually all of the above stated instructions are removed It is therefore possible to switch between manual and automatic updating by setting or removing the b command line option Local user update regions The automatic updating can be switched off locally This is done by pragma statements 22 CC5X C Compiler B Knudsen Data fpragma update FSR 0 OFF 12 bit core fpragma update FSR 1 ON 12 bit core fpragma update RP 0 OFF 14 bit core pragma update RP 1 ON 14 bit core These statements can be inserted anywhere but they should surround a smallest possible region Please check the generated assembly code to ensure that the desired results is achieved NOTE The safest coding is to not assume any specific contents of the bank selection bits when a local update region is started The compiler uses complex rules to update the bank selection bits outside the local regions Also all updating inside a local update region is traced to enable optimal updating when the local update region ends 2 4 Pointers Single level pointers is implemented Note that pointer arithmetic is limited to 8 bit Assignment is allowed for 8 16 24 and 32 bit char t 10 p p amp t 1 p 100 p 2 Pointe
61. Type Cast Constants and variables of different types can be mixed in expressions The compiler converts them automatically to the same type according to the stated rules For example the expression ac bt oc consists of 2 separate operations The first is the plus operation and the second is the assignment The type conversion rules are first applied to b c The result of the plus operation and a are treated last The CC5X compiler use 8 bit int size and contains significantly many data types integers fixed and floating point The type cast rules have been set up to provide best possible compatibility with standard C compilers which typically use 16 or 32 bit int size The type conversion rules implemented are if one operand is double gt the other is converted to double if one operand is float gt the other is converted to float if one operand is 32 bit gt the other is converted to 32 bit if one operand is 24 bit gt the other is converted to 24 bit if one operand is long the other is converted to long if one operand is unsigned gt the other is converted to unsigned QupA dep bacs NOTES The sign is extended before the operand is converted to unsigned Assignment is also an operation Constants are SIGNED except if U is added The bit type is converted to unsigned char The fixed point types are handled as subtypes of float Type conversion in C is difficult The compiler generates a warning if a typecast is req
62. W ANDWF f W f f amp W ANDWF f W f W XORWF W EI eg E We XORWF f W f W ADDWF f W f f LN ADDWF f W f MOVE W W E 255 COMF W 67 CC5X C Compiler B Knudsen Data F f bbs COMF f W f 1 INCF W f f 1 INCF f W decsz i DECFSZ f W f decsz i DECFSZ f W rr f RRE W f rr f RRE f W rl f RLF W f mw RLF f W swap f SWAPF W f swap f SWAPF f W incsz i INCFSZ W f incsz i INCFSZ f b 20 BCF b Bomal BSF f b btsc b BTFSC f b btss b BTFSS f b OPTION W OPTION MOVWF on core 14 sleep SLEEP clrwdt CLRWDT TRISA W TRIS f MOVWF on core 14 return 5 RETLW 5 s1 CALL s1 goto X GOTO X nop2 GOTO next address delay 2 cycles W 45 MOVLW 45 W W 23 IORLW 23 W W amp 53 ANDLW 53 W W 12 XORLW 12 Addition for the 14 bit core W 33 W ADDLW 33 W 33 W SUBLW 33 return RETURN retint RETFIE 6 7 Optimizing the Code The CC5X compiler contains an advanced code generator which is designed to generate compact code For example when comparing a 32 bit unsigned variable with a 32 bit constant this normally requires 16 or 15 instructions When comparing a 32 bit variable with 0 this count is reduced to 6 or 5 The code generator detects and take advanta
63. X B Knudsen Data APPENDIX A1 Using Interrupts pra pra inc pra inte void 1 e gma bit pinl PORTA 1 gma bit pin2 PORTA 2 lude intl16CXX H gma origin 4 rrupt int server void int save registers W STATUS and PCLATH if RTIF TMRO overflow interrupt TMRO 45 if pinl 1 pinl 0 else pinl 1 r RTIF 0 reset flag if INTF INT interrupt INTE 0 reset flag if RBIF RB port change interrupt W PORTB clear mismatch RBIF 0 reset flag int_restore_registers W STATUS and PCLATH main void fdef 16C71 ADCON1 bin 11 port A digital ndif PORTA 0 76543210 TRISA bin 11111001 OPTION 0 prescaler divide by 2 TMRO 45 45 2 90 periods RTIE 1 enable TMRO interrupt GIE 1 interrupts allowed 97 CC5X APPENDIX B Knudsen Data while 1 infinite loop pin2 0 nop2 2 Instruction cycles nop 1 Instruction cycle pin2 1 A2 Predefined Register Names Core 12 char W char INDF TMRO PCL STATUS FSR PORTA PORTB char INDFO RTCC PC optional char OPTION TRISA TRISB char PORTC TRISC bit Carry DC Zero PD TO PAO PAI PA2 bit FSR 5 FSR 6 Core 14 char W char INDF TMRO PCL STATUS FSR PORTA PORTB char INDFO RTCC PC optional char OPTION TRISA TR
64. YS remember to check the assembly file if the application program does not behave as expected Using a compiler does not remove the need for understanding assembly code Debugging methods There are several ways of debugging the program Test parts of the program on a simulator This allows full control of the input signals and thus exact repetition of program execution It is also possible to speed up testing to inspect long term behavior and check out rare situations How to do this is application dependent 2 Usea hardware emulator An emulator allows inspection and tracing of the internal program state during execution in the normal application environment including digital and analog electronics 3 Insert application specific fest code and run the program on a prototype board Then gradually remove the extra code from the verified program parts The key is to take small steps and restore the program to a working state before doing the next change The extra test code can consist of 1 Code that produces patterns square waves on the output pins This can be checked by an oscilloscope 2 Repetition of output sequences 3 Extra delays or extra code to handle special situations The different debugging methods have their advantages and disadvantages It can be efficient to switch between several methods Compiler bugs Compiler bugs are hard to detect because they are not checked out until most other tests have failed Silicon
65. ble P assigned to certain address R overlapping directly assigned E unused external variable not allocated b gt mapped RAM available in all banks 0 bank O 1 bank 1 etc REF 12 number of instructions using the variable ADR gt OAh file address OCh 0 bit address file bit number Examples R 0x006 0 1 in R 0x00B 10 alfa P 0x00B so EDs fixc L 0x00D E qus lode L 0 0x012 0 6 bl G 0 0x012 1 16 bx G 0 0x015 223 0 89 CC5X C Compiler B Knudsen Data When a function is not called unused all its parameters and local variables are truncated to the same location Example L 0xOOF 16 lt gt pm 2 8 4 List File The compiler can also produce a list file The command line option is L The general format is L lt col gt lt lin gt The maximum number of columns per line col and lines per page lin can be altered The default setting is L120 60 The contents of the list file can be changed by using the A option 8 5 Function Call Structure The function call structure can be written to file lt src gt fcs This is useful for codepage optimization and function restructuring in case of call level problems Note that two different formats are produced the first is a list of functions the second is a recursive expansion of the function call structure The command line option is Q for both formats Format sample F functionl 1 0 pO
66. bly instructions are not case sensitive However variables and symbols requires the right lower or upper case on each letter clrw Nop NOP Constant formats OVLW 10 decimal radix is default OVLW OxFF hexadecimal MOVLW 005010001 binary C style MOVLW A a character C style MOVLW 31 decimal constant OVLW 31 20 1 plus and minus are allowed OVLW H FF hexadecimal radix 16 OVLW h OFF MOVLW B 011001 binary radix 2 MOVLW b 1110 1101 OVLW D 200 decimal radix 10 OVLW d 222 OVLW MAXNUM24EXP defined by EQU or define MOVLW 22h NOT allowed 64 CC5X C Compiler B Knudsen Data Formats when loading then result into the W register decf ax 0 iorwf ax w iorwf ax W Formats when writing the result back to the RAM register decf ax decf ax l1 iorwf ax f iorwf ax F Bit variables are accessed by the following formats bcf Carry bsf Zero bcf ax B2 B2 defined by EQU or define bef ax 1 bef STATUS Carry Carry is a bit variable Arrays structures and variables larger than 1 byte can be accessed by using an offset clrf a32 uns32 a32 4 bytes clrf a32 0 clrf a32 3 clrf tab 9 char tab 10 clrf tab 1 not allowed Labels can start anywhere on the line goto LABEL4 LABEL1 LABEL2 LABEL3 LABEL4 nop nop goto LABEL2 Functions are called directly All single 8 bit parameter are transfered using the W register moviw 10 call f1 equivalent to f1 10 The O
67. bugs can be even harder Compiler bugs can often be removed by rewriting the code slightly or depending on the type of bug try 1 pragma optimize 2 pragma update_FSR 3 pragma update_RP 4 command line option u 5 command line option bu 6 command line option b ALWAYS remember to report instances of compiler bugs to B Knudsen Data 7 1 Compilation Errors The compiler prints error messages when errors are detected The error message is proceeded by 2 lines of source code and a marker line indicating where the compiler has located the error The printing of source and marker lines can be switched off by the e command line option The maximum number of errors printed can also be altered Setting the maximum to 12 lines is done by the command line option E12 The format of the error messages is Error filename line number error message Some errors are fatal and cause the compiler to stop immediately Otherwise the compiling process continues but no output files are produced NOTE When an error is detected the compiler deletes existing hex and assembly files produced by the last successful compilation of the same source file 83 CC5X C Compiler B Knudsen Data Error and warning details The compiler prints a short description of the error message to the output screen and to the occ file but not to the err file Note that the description will not be visible when enabling the error file in
68. by a define statement because it then can be safely changed without multiple updating 94 CC5X C Compiler B Knudsen Data char sub02r void pragma computedGoto 2 start of large table pragma return ALFA pragma return 0x10 0x11 pragma origin 0x0320 using an origin statement after a large return table is useful to check the number of return instructions generated In this case there should be 0x320 0x100 0x250 544 instructions If not any differences will be reported by the compiler either as an error or as a message void sub3 char s the next statements could also be written as a switch statement but this solution is fastest and most compact if s gt 3 goto Default skip s goto Case0 goto Casel goto LastCase pragma computedGoto 0 end of c goto region Case0 user statements returns Casel LastCase user statements return Default user statements returns void sub4 char s this solution can be used if very fast execution is important and a fixed number of statements 2 4 8 is executed at each selection Please note that extra statements have to be inserted to fill up empty space between each case if s gt 10 goto END Carry 0 95 CC5X C Compiler B Knudsen Data s rl s s rl s skip s execute 4 Case0 nop Casel nop Case2 nop Case3 nop
69. comparing with constants multiplication and division by a multiple of 2 i e a 0 5 b 1024 0 c 4 0 Floating point library functions float24 sqrt float24 square root Input range positive number including zero Accuracy ME 1 2 relativ Pors 3 10 5 Timing min aver max 1366 1561 1758 Size 153 words basic 24 bit math library Minimum complete program example 396 words float32 sqrt float32 square root Input range positive number including zero Accuracy ME 1 2 relativ rrore L 2 10 7 Timing min aver max 3447 3932 4450 Size 168 words basic 32 bit math library Minimum complete program example 588 words float24 log float24 natural log function Input range positive number above zero Accuracy ME 1 relative error 1 5 10 5 Timing min aver max 2179 3075 3299 Size 214 words basic 24 bit math library Minimum complete program example 623 words float32 log float32 natural log function Input range positive number above zero Accuracy ME 1 relative error 6 10 8 Timing min aver max 3493 4766 5145 Size 265 words basic 32 bit math library Minimum complete program example 762 words float24 log10 f10at24 logl10 function Input range positive number above zero Accuracy ME 1 2 relativ rrofic 3 XL505 5 Timing min aver max 2435 3333 3569 Size 15 words size of log Minimum complete program example 638 wor
70. ctions The statement pragma location is capable of locating prototypes on codepages as well as function definitions The statement is useful when locating functions defined in library files or when locating functions in large programs Its normal use is in limited regions in header files The rules when using pragma location are 1 A function prototype will locate the function on the desired codepage even if the current active codepage is different when the function definition is compiled 2 pragma location have higher priority than pragma codepage 3 fpragma location restores the active codepage defined by the last pragma codepage or pragma origin pragma location 1 codepage 1 void f1 void assigned to codepage 1 void f2 void void f3 void pragma location 3 codepage 3 void 4 void pragma location return to the active codepage void f5 void this prototype is not located 51 CC5X C Compiler B Knudsen Data Notes 1 The location statements have to be compiled before the function definition 2 Functions not located are placed on the current active codepage 3 A warning is printed in case of conflicts The Zpragma location statement should only be used if required An example is when functions inside a module file have to be placed on different codepages or if much tuning is required to find the optimal combination The Zpragma codepage statement is normally sufficient The page typ
71. ds float32 log10 f10at32 logl10 function Input range positive number above zero Accuracy ME 1 2 relativ Prone 1 2 10 7 Timing min aver max 3935 5229 5586 59 CC5X C Compiler B Knudsen Data Size 17 words size of log Minimum complete program example 779 words float24 exp float24 exponential e x function Input range 87 3365447506 88 7228391117 Accuracy ME 1 relative error 1 5 10 5 Timing min aver max 1969 3026 3264 Size 251 words 102 floor24 basic 24 bit math Minimum complete program example 673 words float32 exp float32 exponential e x function Input range 87 3365447506 88 7228391117 Accuracy ME 1 relative error 6 10 8 Timing min aver max 4465 4741 5134 Size 322 words 145 floor32 basic 32 bit math Minimum complete program example 847 words float24 exp10 float24 10 x function Input range 37 9297794537 38 531839445 Accuracy ME 1 relative error 1 5 10 5 Timing min aver max 1987 3005 3194 Size 256 words 102 floor24 basic 24 bit math Minimum complete program example 678 words float32 exp10 float32 10 x function Input range 37 9297794537 38 531839445 Accuracy ME 1 relative error 6 10 8 Timing min aver max 3605 4716 5045 Size 326 words 145 floor32 basic 32 bit math Minimum complete program example
72. e modifier The page type modifiers pageO page3 can replace pragma location codepage page2 void fx void in codepage 2 pagel char f2 char a in codepage 1 The page type modifier defines the codepage to locate the function in both for function prototypes and function definitions NOTE 1 The page type modifier have higher priority than both pragma codepage and pragma location NOTE 2 When the codepage have been defined using the page type modifier or pragma location then the location is fixed and can not be changed in the function definition or by using a second prototype Invalid code pages are mapped to valid ones Page selection bits The page selection bits PAO and PA1 are automatically updated by the compiler and attempts to set or clear these bits in the source code are removed by the optimizer This can be switched off by the j command line option Core 12 note assigning a value to the status register f3 may cause the automatic updating to fail 6 2 Subroutine Call Level Checking Subroutine calls are limited to 2 levels for the 12 bit core and 8 levels for the 14 bit core The compiler automatically checks that this limit is not exceeded The compiler can replace CALL by GOTO to seemingly achieve deeper call levels 1 When a function is called once only the call can be replaced by a goto All corresponding returns are replaced by gotos Call is NOT replaced by goto when a The program counter PCL
73. e px amp tab i may fool the compiler if the value of i is too large If the above syntax is too restrictive then a local update region is the solution All rambank updating then have to be done with C statements Normally local update regions requires inspection of the generated assembly file to avoid problems these statements clears the buffer i LTAB pragma update FSR 0 OFF FSR amp tab 0 do INDF 0 FSR while i gt 0 pragma update_FSR 1 ON Without a local update region i LTAB do tab i 1 0 while i gt 0 In this example the local update region only has a speed advantage The same amount of instructions are generated Note that although no rambank updating is required inside the above local region the compiler does not know the contents of FSR 5 6 at the end of the region and will therefore update these bits afterwards The 14 bit core the IRP bit For some 14 bit core chips rambank 2 and 3 is in use This means that register bit IRP have to be updated in user code when working with arrays and tables 25 CC5X C Compiler B Knudsen Data pragma rambank 2 char array 50 char x FSR amp array 256 x LSB of amp array x IRP amp array 256 MSB NOTE IRP is not updated by the compiler if INDF is used directly in the user code Using array x instead of INDF enables automatic updating of the IRP bit The compiler will trace all loadin
74. ed This allows global data to be put in program memory volatile char a ignored type modifier Note that CC5X use the address to automatically decide that most of the special purpose registers are volatile pageO void fx void x resides in codepage 0 page0 pagel page2 page3 bankO char a variable a resides in RAM bank 0 bank0 bank1 bank2 bank3 shrBank unbanked locations if available size2 char px pointer px is 16 bit wide sizel size2 Local Variables Local variables are supported The compiler performs a safe compression by checking the scope of the variables and reusing the locations when possible The limited RAM space in therefore used efficiently This feature is very useful because deciding which variables can safely overlap is time consuming especially during program redesign Function parameters are located together with local variables Variables should be defined in the innermost block because this allows best reuse of RAM locations It is also possible to add inner blocks just to reduce the scope of the variables as shown in the following example void main void char i no reuse is possible at the outermost level of main i 9 an inner block is added char a for a 0 a lt 10 att i x PORTB 0 sub i local variables can be assigned immediate char b s 1 int il 1 i2 0 more code Local variables ma
75. eese ttes tete then trente seb inen eene 43 pragma config id state id state sese 43 pragma config EE 44 Zpraema library 0 15 e teet o e e e erre i ER e Vl e Ente Sio E Tb eee eee 44 Zpragma location 2 0 1 2 3 E 44 Zpragma optimize IN OLD as sete eterne ETUSR ENEE Eee ER NR EU ERR ER eS 44 Zpragm d origin 5 EE 44 Zpragma rambank 0 1 2 3 ue eer ertet ete rame ipee Y Meere e eth pene tee Ree 45 Zpra ema rambase Y snae Ru HEU UE HERES eerte pO UR 45 pragma ramdef ra rb MAPPING sess neen eene 45 Zpr gma resetVector lt n gt ondiep ete e HE RE E HERE e PHI SUI E ere tae 45 pragma return lt n gt strings or constants eee nennen nennen nennen eene 45 Zpragma SLACKLEVEIS t iussione e rt eere ee ERAN et susan tee eo ene dees agecute eed 46 fpragma update FSR S0 gt an isse e e e Eae ia peaa Eaa aea enne tenentes 46 Zpragma update E E EE 46 CC5X C Compiler B Knudsen Data Hpragma update PAGE 0 12 aeter neget bee ebrei 46 Zpragma update RP Hf 0 15 isses e eere tr RR P RR DR PH RE o Se Rea 46 Zpragma wideConstData Tel 47 42 PICMICRO CONFIGURATION Eege EEN 47 5 COMMAND LINE OPTIONS eeeeeeeeee esent te testes totns tns tn tasas tata sons tn tn sons totns in sto sn sene ta ense n statu sno 48 2T OPTIONS ON A FICE ia tied ae darian lee ee Mu e tee e e ite rei 50 6 PROGRAM CODE
76. el char i Carry 0 W rl i multiply by 2 skip W pragma computedGoto 1 W 0 goto ENDS W 1 goto ENDS W 4 pragma computedGoto 0 ENDS processing continues here 7 2 MPLAB Debugging Support The CC5X compiler can be used inside the MPLAB environment More details is found in debug txt The COD file format for debugging purposes is supported Two modes of source file debugging is available a Using the C source file s b Using the generated assembly file as the source file The format of the assembly file in order to suit the debugging tool Take a look at the assembly file options Some suggestions 84 CC5X C Compiler B Knudsen Data A1 6 10 AmiJ simulator I Al 6 6 AmiJs simulator II A6 8 12Jt compact I Am6 8 412Jt compact II Enabling the COD file is done by a command line option CC filename generate debug file using C source file s filename is optional The asm file option is also switched on CA filename generate debug file using generated assembly file as source filename is optional The asm file option is also switched on Arrays Arrays and structures represent a slight challenge because all variables passed in the COD file are currently either char or bit types This is solved by adding new variables which appears during debugging char table 3 table offset 0 table el offset 1 table e2 o
77. en ener ener nnne neret nn nnne nennen 9 1 5 SHORT PROGRAM EXAMEN 10 L DEFINING THE PICMICRO DEVICE 57 ertet re tie ERI EXER ER EE RR et E AER EE 11 1 7 WHAT TO DO NEXT iioc PI REO DRE RE PRO OPEP EO KR EPI EENS 12 2 VARIABLES EE 13 2 1 INFORMATION ON RAM ALLOCATION c eeeeeeenee enne nene enneennenr enne en tenere en tent enne netten entente enne 13 2 2 DEFINING VARIABLES 5 trente eege ee E 13 Integer variables ee e ie tH suites Eu A RR EU E e ERE IURE RENS LEER SERERE 14 a EE 14 IEEE754 interoperability creere ed et dee eebe ERE ee etre ede EI eee feeder ett 15 Fixed point varidbles s RR An RS ERROR enhn e 16 Assigning variables to RAM addresses eese eene rentre 17 Supported type modifiers at Re t e a tie te deed etd 18 Local TEE deuten etus e 19 Temporary Variabl ss est Ee Eege Ie a se ae Sesh dE NE NR Ms Ree 19 Arrays EE 20 Bitfields eet nein denen eene wanes nahe 20 TyDed f xu onm E eR edat eene 21 2 3 USING RAM BANKS yeh ere e eee veo eroe ete ere pe ete eec ee eue veces reed 21 The bank type modifier ss RR hne enn e ten nhi ene 2l RAM Bank Selection Be 22 Local user update regions user gene n D RP nS pe ter irt arro 22 pM POINTERS ER 23 Pointer models vie RU tate er Ee Ee EOD ES 23 Tie D2 DU COT d As s cae er o aar B eO vinis 24 The 14 bit core the IRP bur 25 2 5 CONST DATA SUPPORT rn e REP Ee ER OT THE TOR DEOR 26 Storing EEN 2
78. ending on the CPU capabilities An error is printed if the constant expression loses significant bits because of value range limitations char a a 10 100 256 an error is printed 10L 100 256 no error uns16 10 100 256 no error uns16 10 100 256 error again 10 200 256 no error 200 is a long int D D D D Adding a L means conversion to long 16 bit The command line option cu force 32 bit evaluation of constants upward compatibility so that no significant bits are lost Some new built in types can also be used TYPE SIZE MIN MAX int8 8 bit signed 1 128 127 int16 16 bit signed 2 32768 32767 int24 24 bit signed 3 8388608 8388607 int32 32 bit signed 4 2147483648 2147483647 uns8 8 bit unsigned 1 0 255 unsl6 16 bit unsigned 2 0 65535 uns24 24 bit unsigned 3 0 16777215 uns32 32 bit unsigned 4 0 4294967295 34 CC5X C Compiler B Knudsen Data The constant type is by default the shortest signed integer Adding an U behind a constant means that it is treated as unsigned Note that constants above 0x7FFFFFFF are unsigned by default with or without an U behind Enumeration An enumeration is a set of named integer constants It can often replace a number of define statements The numbering starts with 0 but this can be changed enum Al A2 A3 A4 Jj enum alfa 8 beta zeta 4 eps 3 4 Functions Function defi
79. er divide by 8 do TMRO 0 clrwdt only if necessary while TMRO lt 125 125 8 1000 while millisec gt 0 void delay10 char n Delays a multiple of 10 millisec Clock 4 MHz gt period T 0 25 microsec DEFINITION 1 is 1 instruction cycle error 0 16 percent Er char i OPTION 7 do clrwdt only if necessary i TMRO 39 256 us 39 10 ms while i TMRO while n 0 void _delay10 char x Delays a multiple of 10 millisec Clock 32768 Hz gt period T 30 518 microsec DEFINITION 1 is 1 instruction cycle 4 T 122 microsec 10 ms 82 is 81 92 gt error 0 1 percent char i 91 CC5X C Compiler B Knudsen Data do i 26 2 is do i i 1 while i gt 0 26 3 1 2 77 is while x gt 0 3 is char counter void main void if TO 1 power up or MCLR PORTA 0 write output latch first TRISA 0 all outputs TRISB OxFF all inputs else watchdog wakeup counter 1 if counter gt 0 OPTION 0x0B WDT divide by 16 Sleep waiting 16 18 ms 288 ms 0 288 seconds L og delay 100 100 millisec AJ EA delay10 100 1 second AJ counter 7 7 0 288ms 2000 ms OPTION 0Ox0B O 1011 WDT divide by 16 sleep waiting 16 18 ms 0 288 seconds total
80. ess and codepage of the following code The current active location on a codepage can not be moved backwards even if there is no code in that area Origin can not be changed inside a function 44 CC5X C Compiler B Knudsen Data Examples pragma origin 4 interrupt start address fpragma origin 0x700 pragma rambank lt 0 1 2 3 gt 14 bit core gt mapped space chip specific 0 gt bank O0 0 0x000 127 0x07F 1 gt bank 1 128 0x080 255 0xOFF 2 gt bank 2 256 0x100 383 0x17F 3 gt bank 3 384 0x180 511 Ox1FF 12 bit core gt mapped space 8 15 0x0F 0 gt bank O0 16 0x10 31 Ox1lF 1 gt bank 1 48 0x30 63 0x3F 2 gt bank 2 80 0x50 95 0x5F 3 gt bank 3 112 0x70 127 Ox7F pragma rambank defines the region s where the compiler will allocate variable space The compiler gives an error message when all locations in the current bank are allocated RAM banks are only valid for some of the controllers Non existing banks for the other controllers are mapped into defined RAM space pragma rambase n Defines the start address when declaring global variables This statement is included for backward compatibility reasons only The use of rambank and rambase are very similar The address have to be within the RAM space of the chip used 12 bit core note The locations from address 0 to 31 are treated as a unit Using start address 7 means that
81. esses of the 14 bit core Built in skip function for computed goto The skip function also allow 16 bit parameter When using an 8 bit parameter carry is automatically generated 3 code words extra if the table cross a 256 word address boundary Carry is always inserted when generating relocatable assembly Options available GW dynamic selected skip format warning on long format default GD dynamic selected skip format GS always short skip format error if boundary is crossed GL always long skip format When using the GS option CC5X will generate an error if the table cross a 256 word code address boundary The short format enables most compact code but requires manually moving the table in the source code if the error is produced Computed Goto Regions The compiler enters a goto region when skip is detected In this region optimization is slightly changed and some address checks are made The goto region normally ends where the function ends A goto region can also be started by a pragma statement fpragma computedGoto 1 start c goto region useful if PCL is written directly A goto region can also be stopped by a pragma statement pragma computedGoto 0 end of c goto region recommended if the function contains code below the goto region for instance when the table consists of an array of goto statements examples follow later 93 CC5X C Compiler B Knudsen Data Computed
82. ever only one index can be a variable struct char e 4 char i multi 5 multi x e 3 4 multi 2 e i 1 temp Bitfields Bitfields in structures are allowed The size have to be 1 8 16 24 or 32 bit struct bitfield unsigned a 1 bit Gs 20 CC5X C Compiler B Knudsen Data unsigned d 32 char aa zz The CC5X compiler also allows the bitfield syntax to be used outside structures as a general way of defining variable size int x 24 a 24 bit signed variable Typedef Typedef allows defining new type identifiers consisting of structures or other data types typedef struct hh HH HH varl typedef unsigned ux 16 equal to unsi6 ux r a b 2 3 Using RAM Banks Using more than one RAM bank is done by setting the active rambank variables proceeding the first rambank statement are placed in mapped RAM or bank 0 This is also valid for local variables and parameters pragma rambank 1 char a b c a b and c are located in bank 1 parameters and local variables in functions placed here are also located in bank 1 pragma rambank 0 char d located in bank 0 The compiler automatically finds the first free location in the selected bank NOTE Local variables and function parameters also have to be located It may be necessary to use pragma rambank between some of the functions and even INSIDE a function The recommended strategy is to locate local
83. evice Command line options Page naming rpO PROGO is the name of the first codepage rpl PROG1 is the name of the first codepage Bank naming rbO BANKO is the name of the first RAM bank rbi BANK1 is the name of the first RAM bank Separate interrupt logical section named ISERVER r2 use name of current module lkr r2 file lkr use defined file nam Example with 3 modules This example demonstrates the syntax only J RRR RRR KKK hh kkk k kkk k kkk k kkk k k k k k k Kok ke koe k ke k MODULE1 C pragma chip PIC16C63 include globdefl h include intl16CXX H pragma origin 4 interrupt int_server void int_save_registers W STATUS and PCLATH if RTIF TMRO overflow interrupt TMRO 45 RTIF 0 reset flag if INTF INT interrupt INTE 0 reset flag if RBIF RB port change interrupt W PORTB clear mismatch RBIF 0 reset flag int_restore_registers W STATUS and PCLATH char a bit bl 52 void main void PORTA 0b0010 77 CC5X C Compiler B Knudsen Data TRISA 0b0001 if TO 1 amp amp PD 1 power up clearRAM set all RAM to 0 a 5 bl 1 mulcnd 10 mulplr 20 mpy assembly routine demo b2 b1 do if in 1 break sub a amp 3 while a lt 200 J RRR RRR KR KKK KK KK KK RK OK RK kkk kkk MODULE2 C
84. ffset 2 struct char a char b st st offset 0 element a st el offset 1 element b This means that the name of a structure element is not visible when inspecting variables in a debugger ICD debugging Including ICD debugging support is simple just use define ICD DEBUG include 16F877 H 7 3 Assert Statements Assert statements allows messages to be passed to the simulator emulator etc Syntax pragma assert type text field optional character type a user defined assert user defined emulator command f user defined printf 1 user defined log command lt text field gt undefined syntax valid to the end of the line The line can be extended by a character like other preprocessor statements pragma assert e text passed to the debugger pragma assert e text passed to the debugger pragma assert this assert command is ignored 85 CC5X C Compiler B Knudsen Data NOTE 1 comments in the text field will not be removed but passed to the debugger NOTE 2 Only ASCII characters are allowed in the assert text field However a backslash allows some translation kt Em 0S NE S lh NA 24 Ne Bo NA SS 4 5 gt 5 6 gt 6 7 gt 7 a gt 7 No gt 8 NE 99 9 Xn Se 10 Ane gt Ll NE 12 Ae SS USE OF MACRO S Macro s can be used inside assert statements with some limitations The macro should cover the
85. find the address of stored items Empty cdata statements can be used to set or read the current cdata address pragma cdata ADDRESS set current cdata address fpragma cdata IDENTIFIER get current cdata address Only cdata within the valid code space is counted when calculating the total number of code words Storing EEPROM data Note that EEPROM data must be put in an assembly module when using MPLINK 80 CC5X C Compiler B Knudsen Data define CDATA START 0x2100 pragma cdata CDATA START start of cdata block pragma cdata Ox3F 10 20 Ta pragma cdata a 0b 0c 0 NO is added in strings due to 14 bit compression pragma cdata a b c Using the cdata statement 1 Defining special startup sequences define _NOP 0x0000 pragma cdata 0 _NOP pragma resetVector 1 goto main at address 1 2 Restoring calibration values define ResetAddress Ox3FF 16C509 A define _MOVLW 0x3000 pragma cdata ResetAddress _MOVLW CalValue 3 Storing packed strings and other data in flash devices 16F87X The cdata definitions should be put in a separate file and included in the beginning of the program This enables identifiers to be used in the program and checking to be performed define CDATA_START 0x80 pragma cdata CDATA_START start of cdata block pragma cdata Ox3FFF 0x2000 0x1000 pragma cdata 0x100 10 lt lt 4 3456 10 456 10000 define D7 1 h L AB d 128
86. g of pointers to decide how the IRP bit should be updated This applies to both 8 and 16 bit pointers It is also possible to use Zpragma assume to state the bank directly bank char t 3 bank3 char i pi pit pragma assume pi in rambank 3 or rambank 2 pragma assume pit in rambank 1 or rambank 0 pi amp i pit amp t 2 An error message is printed if a pointer is loaded with an address from the wrong RAM half Note that rambank 0 and 1 are grouped together the lower RAM half 0 OXFF Rambank 2 and 3 are the upper RAM half 0x100 Ox1FF Updating of IRP can be switched off locally The compiler does not remove superfluous updating of the IRP register This means that IRP is updated for each pointer or table access An efficient strategy may be to locate most of the tables in upper or lower RAM above or below address 0x100 and do all updating of IRP in the user code Few updates are sometimes sufficient fpragma update IRP 0 off IRP 1 updated by user code pragma update IRP 1 f noc 2 5 Const Data Support CC5X supports constant data stored in program memory The C keyword onst tells the compiler that these data do not change Examples const char ps Hello world const float ftx 1 0 33 34 1 3e 10 t ps ps mie fx ftx i The implementation of constant data supports the following features e both 8 and 16 bit pointers to const data in the same application e
87. ge of similar situations to enable compact code Most of the code is generated inline even multiplication and division However if many similar and demanding math operations have to be performed then it is recommended to include a math library Optimized Syntax Bit toggle now use the W register to get compact code bit b b b MOVLW K XORWF var Testing multiple bits of 16 bit variables or greater uns16 x if x amp OxFO0 68 CC5X C Compiler B Knudsen Data if x amp Ox3C if x amp OxF00 0x300 if x amp Ox7F00 lt 0x4000 Testing single bits using the amp operator if if ast a amp Ox10 a amp 0x80 al6 amp 0x200 BTFSC BTFSS a 4 BTFSS BTFSC a 7 0 BTFSS BTFSC al6 1 1 Peephole optimization Peephole optimizing is done in a separate compiler pass which removes superfluous instructions or rewrite the code by using other instructions This optimization can be switched off by the u command line option The optimization steps are 1 redirect goto to goto remove superfluous gotos 3 replace goto by skip instructions replace INCF and DECF by INCFSZ and DECFSZ remove instructions that affects the zero flag only remove superfluous updating of PAO and PAI remove other superfluous instructions 8 remove superfluous loading of the W register NOTE Optimization can also be switched on or off in a local region Please refer to the pragma optimize statemen
88. gh8 0 7 8 15 16 23 24 31 mid8 0 7 8 15 8 15 8 15 midL8 0 7 8 15 8 15 Be midH8 0 7 8 15 16 23 1 6 23 low16 0 7 0 15 9 15 4 5 midl6 0 7 0 15 8 23 8223 highl6 0 7 0 15 8 23 16 31 low24 0 7 0 15 0 23 0 23 high24 0 7 0 15 0 23 ero 3 7 C Extensions CC5X adds some extensions to the standard C syntax 1 The bit variable type 2 The interrupt function type 37 CC5X C Compiler B Knudsen Data 3 C style comments are allowed A a comment valid to the end of the line 4 Local variables can be declared between statements as in C Standard C requires local variables to be defined in the beginning of a block 5 Binary constants Obxxxxxx or bin xxxxxx The individual bits can be separated by the 050100 05 0 000 1 01 00000 bin 0100 bin 0001 0100 6 Preprocessor statements can be put into macros Such preprocessor statements are not extended to multiple lines The inserted preprocessor statements are evaluated when the macro is expanded and not when it is defined define MAX X a Hz if AAA 0 amp amp BBB 0 ice N endif N 7 Several of the type modifiers are not standard in C pageO page3 bankO bank3 shrBank size1 size2 More C extensions are allowed by the pragma statement 3 8 Predefined Symbols The basic PICmicro registers are predefined header files defines the rest W INDF PCL STATUS FSR PORTA Carry etc The following names are defined as i
89. gx e ree e a bei repere Pete ee 68 Optimized EECH 68 Peepholeoptimization EEN 69 6 8 LINKER SUPPORT erect Sues cavechesratesnes cavech ences shu ete ee terree tier sts rere esee dee 70 Variables ea RU ee Ee eie ume nud aas 70 ET 71 Header files UU ee auc aed aaa ets ones eI hs oes Repent due ce deat 71 Using RAM banks teer eR PR E REESE Re A ei RUE tie P reas 71 EE 72 Ia eun MEE 72 Using code pages EE 72 Inter pls Saisie o et DU ERE RR GATES RR RERUM EROR GRE RE eh Pe ERE D xri 72 Call level checking oiov ear ED ER E ORE RE ar 73 Compulted eoto c ot E E EPOR GNE ERRORES UE HS Save tee RE e Redes 73 Recommendations when using MPLINK eese enne nnne ne en eren 73 MPLAB and MPASM Support ere tete PH REPRE RO DH EINE RID P ev Ere tbe das 74 The MPLINK script file aieo o Ry e n e E URP E ERR RD Et ERE m Peg 75 Example With 3 modules eter Pe e RU E URDU E eae He EEEak 77 6 9 THE CDATA STATEMENT iere He ode Dee ERU De i o peer PT aei 80 Storing EEPROM data EE 80 EE 61 OU DD ELE 83 TV SCOMPILATION ERRORS 5 noa cecusectv3 di dee ait e ee deed 83 CC5X C Compiler B Knudsen Data Error and warning details e oe e Re tr TO Sri RUE P Seti epe areae 64 Some common compilation problem 64 7 2 MPLAB DEBUGGING SUpbORT tetest rtre Eere sE EEEE sss eise EEEE Eeee etia eite ene 84 LGD I TTA MM HET 65 1 3 ASSERT STATEMENTS ccccssssscccsssesssssssccesececssssssscesecesssssssssseececssssessseccecssssssssse
90. her operations Operations not found in the libraries are handled by the built in code generator Also the compiler will use inline code for operations that are most efficient handled inline The following command line options are available we no warning when fixed point constants are rounded wO warning on operator library calls wI warning on long inline code for multiplication and division Integer libraries The math integer libraries allows selection between different optimizations speed or size The libraries contains operations for multiplication division and division remainder ma ma ma CT cr CT 3 Di ct ct c hl16 h h24 h h32 h hl6m h24m h32m E pd 1 3 24 JD oO basic basic basic Speed Speed Speed library up t library up t library up t size 8 8 16 size 8 8 16 size 8 8 16 math m h libraries can b they must be included first math h contains similar functions o 16 bit o 24 bit o 32 bit 16 16 and 24 8 multiply 16 and 32 8 multiply used when which are deleted The min and max timing cycles are approximate only Sign unsigned S Sign Res argl op arg2 A math32 h B math24 h C mathl16 h ABC 16 ABC S 16 AB 24 Sek Je 16 RL 24 A J 32 pBe s 24 A 32 UB S 24 A S 32 ABC 16 AB 24 A 32 ABC 16 aB 24 A 32 gwg 16 16 16 S 16 x 8 24
91. ider using the linker support and link C and assembly modules using MPLINK asm assembly instructions fendasm 63 CC5X C Compiler B Knudsen Data Features many assembly formats equ statements can be converted to variable definitions macro and conditional assembly capabilities call C functions and access C variables C style comments is possible optional optimization optional automatic bank and page updating Note that the file inline h is for emulating inline assembly and should NOT be included when using real inline assembly The compiler does not optimize inline assembly or update the bank or page bits unless it is instructed to do so Inline assembly is NOT C statements but are executed in between the C statements It is not recommended to write the code like this if a b asm nop this is not a C statement by definition endasm a 0 THIS is the conditional statement Inline assembly supports DW This can be used to insert data or special instructions CC5X will assume that the data inserted are instructions but will not interpret or know the action performed Bank selection bits are assumed to be undefined when finishing executing DW instructions PCLATH bit 3 and 4 must remain unchanged or restored to correct value if more than one code page is available on the device Example use is found in startup txt asm DW OxS3FFF any data or instruction DW CALL 0x2000 ADDRESS endasm Assem
92. if bx i lt max if fee 10 0 Bit variables bit ar D GS d char i j k bit bitfun void bit return type using Carry bit return 0 Clear Carry return return 1 Set Carry return nop return Carry return return b Carry b return return i return b amp PORTA 3 b bitfun2 bitfun 1 if bitfun if bitfun if bitfun 0 b charfun b charfun gt 0 b bitfun Carry bitfun b amp bitfun 32 CC5X C Compiler B Knudsen Data bitfun b bitfun PORTA 1 b conditional increment b conditional decrement k Carry k Carry b Toggle bit or b b 0 Ier assign inverted bit PORTA 0 Carry a amp PORTA 0 PORTA 1 a PORTA 2 amp a Fh Fh DD H H H H H assign condition using 8 bit char variables b ti b IW b j 0 b k 0 b i gt 0 assign bit conditions b c amp d also amp amp Dx MWg ba e E Re is Ee SE conditions using bit variables if b c also s gt lt gt lt initialized local bit variables bit bx cx bit by fx OxFF Multiplication division and modulo multiplication al6 b16 c16 16 16 bit A general multiplication algorithm is implemented allowing most combinations of variable sizes Including a math library allows library calls to be generated instead
93. ind the V define define counter v1 define MAX 145 define echo x v2 x define mix echo 1 nested macro Note that all define s are global even if they are put inside a function Preprocessor directives can be put into the define statement include include test h include lt test h gt include s can be nested When using Zinclude test h the current directory is first searched If the file is not found there then the library directories are searched in the same order as supplied in the command line option list I dir The current directory is skipped when using Zinclude lt test h gt undef define MAX 145 fundef MAX removes definition of MAX undef does the opposite of define The undef statement will not produce any error message if the symbol is not defined if dif defined ALFA amp amp ALFA statements compiled if ALFA is equal to 1 conditional compilation may be nested endif An arbitrary complex constant expression can be supplied The expression is evaluated the same way as a normal C conditional statement is processed However every constant is converted to a 32 bit signed constant first 1 macro s are automatically expanded 2 defined SYMBOL and defined SYMBOL are replaced by 1 if the symbol is defined otherwise 0 3 legal constants 1234 1 a 4 legal operations gt gt lt lt lt lt gt gt amp
94. ines the chip type The device have to be known internally 16C54 55 56 57 58 61 64 65 71 73 74 84 or supported by a header file i e 16F877 H Default device is 16C54 q lt N gt assume disabled interrupt at the lt N gt deepest call levels Le q1 allows the main program to use all stack levels for function calls If the interrupt routine makes function calls then q2 etc can be used Disabling interrupt at the deepest call level MUST be properly ensured in the user application program r generate relocatable assembly no hex file r2 lt filename lkr gt use separate logical section for interrupt routine rp lt N gt name on codepage 0 is PROG lt N gt default PROGI rb lt N gt name on RAM bank 0 is BANK lt N gt default BANKO ro lt N gt add offset lt N gt when generating local variable block name Q write the call tree to lt src gt fcs S silent operation of the compiler u no optimizing V rnuD generate variable file lt src gt var sorted by address as default r only variables which are referenced in the code n sort by name u unsorted D decimal numbers we no warning when fixed point constants are rounded wB warning when function is called from another code page 49 CC5X C Compiler B Knudsen Data wI warning on long inline code for multiplication and division wL 12 bit core only print warning on all GOTO links to functions residing on hidden half of a codepage
95. ions like sin logO expQ sqrt etc Supports standard C constant data and strings in program memory const Automatic storing of compressed 7 bit data in PICI6F87X devices Pointer models of 8 and 16 bits mixed sizes in same application allowed RAM and or ROM pointers The size of single pointers can be automatically chosen by the compiler Linker support MPLINK interfaces with assembly MPASM modules Extended call level by using GOTO instead of CALL when possible Inserts links to hidden subroutines Access to all assembly instructions through corresponding C statements Lookup tables pragma return Hello world Integrated interrupt support Chip configuration information in source code Size in bits of the variables supported by the different compiler editions RED FREE STANDARD EXTENDED integer 8 16 8 16 24 8 16 24 32 fixed 8116424 8116424432 float 24 24432 16 24 32 CC5X C Compiler B Knudsen Data 1 1 Supported devices 12 bit core PICI6C5X PIC12C50X etc e upto 2048 words of code on 1 4 code pages e up to 73 byte RAM in 1 4 banks 14 bit core PIC12C67X PIC14000 PIC16CXX etc e upto 8192 words of code on 1 4 code pages e upto 512 byte RAM in 1 4 banks 1 2 Installation and System Requirements The CC5X compiler uses 32 bit processing and runs on IBM PC compatible machines using MSDOS or Windows NT 95 98 2000 Installing CC5X is done by first creating a directory f
96. itten in the fcs file External function calls are marked EXTERN Computed goto 14 bit core CC5X will always use the long format when generating code for skip It is not possible to use the GS option The long format is 3 instructions longer than the short format 12 bits core All destination addresses must be located in the first 256 word half of the codepage Unfortunately CC5X can not check the layout done by MPLINK It is therefore strongly recommended to apply some manual design rules that will prevent destination addresses to be moved into the invisible high code page half This can be done by ensuring a linking sequence that will put all modules containing computed goto at the beginning offset 0 of the codepage Inspection of the generated list file is recommended Recommendations when using MPLINK 1 Use as few C modules as possible because of a inefficient bank bit updating between modules b local variable space can not be reused between modules c only single 8 bit parameter in calls between modules d only 8 or 1 bit return values between modules 2 Use definition header files that are shared between modules Include the shared definition in all C modules to enable consistency checking a function headings prototypes Add page information when using more than one code page modulel c extern page0 void sub char ax module2 c extern pagel void mpy void 73 CC5X C Compiler B Knudsen Data Do
97. l RAM locations to zero The generated code use the FSR register The recommended usage is void main void if TO 1 amp amp PD 1 power up WARM RESET ClearRAM set all RAM to 0 if condition goto WARM RESET 54 CC5X C Compiler B Knudsen Data The code size and timing depends on the actual chip The following table describes the basic types Chip devices not found here maps to one of the described types INS ICYC TOTCYC 4MHz RAM START LAST BANKS PICmicro 8 6 145 0 15ms 25 7 OxlF 16C54 9 4 202 0 20ms 41 7 Ox3F 2 16C509 13 4 290 0 29ms 72 8 Ox7F 4 16C57 8 7 254 0 25ms 36 12 Ox2F S 16C84 6 5 482 0 48ms 96 32 Ox7F 1 16C620A 12 5 644 0 64ms 128 32 OxBF 2 12C671 9 4 770 0 77ms 176 32 OxFF 2 16C642 9 4 770 0 77ms 192 32 OxFF 2 16C74 10 4 TIL 0 770s 192 32 OxFF 4 16C923 19 5 1110 1 11ms 224 32 Ox14F 4 16C627 12 4 1058 1 06ms 256 32 0x17F 4 16C773 d 4 1807 1 81ms 368 32 OxlFF 4 16C77 INS number of assembly instructions required ICYC cycles 4 clock for each RAM location cleared TOTCYC total number of cycles 4 clock required 4MHz approx time in milliseconds required at 4 MHz RAM total number of RAM locations START first RAM address LAST last RAM address BANKS number of RAM banks PICmicro chip type described 6 5 Library Support The library support includes standard math and support for user defined libraries The library files should be included
98. locations in the mapped register space and bank 0 are allocated Using start address 32 implies that locations in the mapped register space are allocated NOTE The start address is not valid for local variables but rambase can be used to select a specific RAM bank pragma ramdef ra rb MAPPING This statement is normally used in PICmicro header files Refer to file chip txt for details pragma resetVector lt n gt Some chips have an unusual startup vector location like the PIC16C508 9 The reset vector then have to be specified This statement is normally NOT required because the compiler normally use the default location which is the first 14 bit core or the last location 12 bit core fpragma resetVector Ox1lFF at last code location fpragma resetVector 0 at location 0 fpragma resetVector 10 at location 10 fpragma resetVector NO reset vector at all pragma return lt n gt strings or constants Allows multiple return statements to be inserted This statement should be proceeded by the skip statement The compiler may otherwise remove most returns The constant n is optional but it allows 45 CC5X C Compiler B Knudsen Data the compiler to print a warning when the number of constants is not equal to n Refer to Section 9 2 Computed Goto for more details Note that const data types should normally be used for constant data skip W define NoH 11 pragma return No
99. made unavailable for allocation in the linker script file This applies to 14000 16C63 16C63A 16C65 16C65A 16C65B 16C73 16C73A 16C73B 16C74 16C74A 16C74B 16F873 16F874 and similar devices CC5X generates a warning when variables are assigned to fixed addresses Example change OLD VALUE 0x20 DATABANK NAME gpr0 START 0x21 END 0x7F OLD VALUE OxAO DATABANK NAME gpri1 START 0xA1 END 0xFF CHANGE 6 LOGICAL RAM sections must be added one for each DATABANK that contain RAM locations not special function registers Note that if a logical RAM section is missing then the variables that belongs to this section will be put in the default section MPLINK gives no error on missing logical sections in the script file and the program will fail SECTION NAME BANKO RAM gpr0 RAM bank 0 SECTION NAME BANK1 RAM gprl RAM bank 1 SECTION NAME BANK2 RAM gpr2 RAM bank 2 SECTION NAME BANK3 RAM gpr3 RAM bank 3 SECTION NAME SHRAM RAM gprnobnk shared RAM SECTION NAME GPRAM RAM gprs no RAM banks Logical code blocks STARTUP startvector ISERVER optional section for interrupt routine PROG for devices with one codepage only PROG1 first codepage PROG2 PROG3 PROG4 CONFIG config word IDLOCS id locations Logical RAM blocks GPRAM devices without RAM banks BANKO bank 0 76 CC5X C Compiler B Knudsen Data BANK1 bank 1 BANK2 bank 2 BANK3 bank 3 SHRAM shared common RAM if available on d
100. math library to include For floating point the main decision is between the 24 bit or the 32 bit library If you use 32 bit operations this can be combined with 24 and 16 bit floating point types to save RAM Automatic type conversion integer lt gt float double integer lt gt fixed point float lt gt double fixed point lt gt float double requires additional functions In general using the smallest possible data type will save code and RAM space This must be balanced against the extra work to analyze the program to prevent overflow and too large accumulated errors If there is plenty of code space in the controller and timing is no problem then large types can be used Otherwise analysis is required to get optimal selections It is recommended to keep the number of called library functions as low as possible Although function selection is done automatically by the compiler it is possible to use type casts or even make a custom library by copying the required functions from existing libraries All libraries are written in C CC5X can print a warning for each operator function that is called option wO 6 6 Inline Assembly The CC5X compiler supports inline assembly located inside a C function There are some restrictions compared to general assembly First it is only possible to CALL other functions Second GOTO is restricted to labels inside the function If these restrictions makes program design too difficult cons
101. me Itis possible to design SPECIAL service routines if the use of interrupts is limited to certain program regions for instance in wait loops or after SLEEP instructions In such cases registers do not have to be saved Be careful with PCLATH and RPO RP1 anyway NOTE clearing GIE requires special attention on some PICmicro devices The recommended procedure is do GIE 0 while GIE 1 Note that this applies only to some of the devices the first designed ones Refer to the documentation from Microchip for more details INTERRUPTS CAN BE VERY DIFFICULT THE PITFALLS ARE MANY 6 4 Startup and Termination Code The startup code consists of a jump to main which has to be located on page zero No variables are initiated AII initialization has to be done by user code This simplifies design when using the watchdog timer or MCLR pin for wakeup purposes The SLEEP instruction is executed when the processor exit main This stops program execution and the chip enters the low power mode Program execution may be restarted by a watchdog timer timeout or a low state on the MCLR pin The 14 bit core also allows restart by interrupt An extra GOTO is therefore inserted if main is allowed to terminate SLEEP This ensures repeated execution of the main program However no extra GOTO is added when a sleep command is inserted anywhere else in the application program Clearing ALL RAM locations The internal function clearRAM will set al
102. meters allowed in function calls Space for parameters are allocated in the same way as local variables which allows efficient reuse The bit type is also allowed Note that if W is used this has to be the LAST parameter char func char a unsl6 b bit ob char W Internal functions The internal functions provides direct access to certain inline code btsc Carry void btsc char BTFSC f b btss bit2 void btss char BTFSS f b 35 CC5X C Compiler B Knudsen Data clrwdt void clrwdt void CLRWDT clearRAM void clearRAM void clears all RAM i decsz i char decsz char DECFSZ f d W incsz i char incsz char INCFSZ f d nop void nop void NOP nop2 0 void nop2 void GOTO next address retint void retint void RETFIE W rl i char rl char RLF i d i rr i char rr char RRF i d sleep void sleep void SLEEP Skip i void skip char computed goto k swap k char swap char SWAPF k d The internal rotate functions are also available for the larger variable sizes al6 rl al6 16 bit left rotation a32 rr a32 32 bit right rotation The inline function nop2 is implemented by a GOTO to the next address Thus nop2 can replace two nop to get more compact code The main use of nop and nop2 is to design exact delays in timing critical parts of the application 3 5
103. micro header files Refer to file chip txt for details pragma library 0 1 CC5X will automatically delete unused library functions pragma library 1 functions that are deleted if unused applies to prototypes and function definitions pragma library 0 pragma location lt 0 1 2 3 gt This statement can be used to locate the functions on different codepages Refer to Section 6 1 Program Code Pages for more details pragma optimize N lt 0 1 gt This statement enables optimization to be switched ON or OFF in a local region A specific type of optimization can also be switched on or off The default setting is on 1 redirect goto to goto 2 remove superfluous gotos 3 replace goto by skip instructions 4 remove instructions that affects the zero flag only 5 replace INCF and DECF by INCFSZ and DECFSZ 6 remove superfluous updating of PAO and PA1 7 remove other superfluous instructions 8 remove superfluous loading of W Examples pragma optimize 0 ALL off pragma optimize 1 ALL on pragma optimize 2 1 type 2 on pragma optimize 1 0 type 1 off combinations are also possible pragma optimize 3 0 4 0 5 1 pragma optimize 1 1 0 2 0 3 0 NOTE The command line option u will switch optimization off globally which means that all settings in the source code is ignored pragma origin n Valid address region 0x0000 0x1 FFF Defines the addr
104. micro are trademarks of Microchip Technology Inc Chandler U S A COMPILER BUG REPORTS The compiler has been carefully tested and debugged It is however not possible to guarantee a 100 96 error free product If the compiler generates application code bugs it is almost always possible to rewrite the program slightly in order to avoid the bug Zpragma optimize can be used to avoid optimization bugs Other pragma statements are also useful Please report cases of bad generated code and other serious program errors 1 Investigate and describe the problem If possible please provide a short code sample which demonstrates the problem A fragment of the generated assembly file use cut and paste is normally enough Alternatively a short and complete C program 10 100 lines 2 This service is intended for difficult compiler problems not application problems 3 Language English 4 State the compiler version 5 Send your report to support bknd com or by fax to 47 73 96 51 84 CC5X C Compiler B Knudsen Data CONTENTS 1 INTRODUCTION oops E 7 LL SUPPORTED DEVICES ec ertet eee toe meer cede pe eee e e tended eee teure ere d erede 8 1 2 INSTALLATION AND SYSTEM REQUIREMENTS eene enne eth nnnn asse enne ten aa sess enne tarn nn nennen 8 Support for LONG File Names esse ep tee eii re b reet E Hebreo Eo dentes 8 User ET 8 1 3 MPLAB SUPPORT 5 35 EEE NEET AEE ed o ive oe P pre e EE E 9 1 4 SUMMARY OF DELIVERED FILES nenn
105. mily of microcontrollers The CC5X compiler enables programming using a subset of the C language Assembly is no longer required The reason for moving to C is clear Assembly language is generally hard to read and errors are easily produced C enables the following advantages compared to assembly e Source code standardization Faster program development Improved source code readability Easier documentation Simplified maintenance Portable code The CC5X compiler was designed to generate tight and optimized code The optimizer automatically squeezes the code to a minimum It is possible to write code that compiles into single instructions but with C syntax This means that the code can be optimized by rewriting C instead of rewriting assembly The design priority was not to provide full ANSI C support but to enable best possible usage of the limited code and RAM resources If the compiler generated less optimal code this would force assembly to be used for parts of the code CC5X features Local and global variables of 1 8 16 24 and 32 bits Efficient reuse of local variable space Generates tight and optimized code Produces binary assembly list COD error function outline and variable files Automatic updating of the page selection bits Automatic updating of the bank selection bits Enhanced and compact support of bit operations including bit functions Floating and fixed point math up to 32 bit Math libraries including funct
106. n an assembly module NOTE 2 Constant data should normally be stored using the const type modifier However cdata is useful for storing EEPROM data or data and instructions at fixed addresses NOTE 3 There is no check on validity of the inserted data or address However it is NOT possible to overwrite program code and other cdata sections The data is added at the end of the assembly and hex file in the same order as it is defined SYNTAX pragma cdata ADDRESS VXS lt VXS gt pragma cdata VXS L i lt VXS gt fpragma cdata IDENTIFIER lt VXS gt lt VXS gt ADDRESS 0 Ox7FFE VXS VALUE EXPRESSION STRING VALUE 0 Ox3FFF EXPRESSION C constant expr i e 0x1000 3 1234 STRING Valid C String r n 0 x24 x8 xe xFF xff String translation xHH or NxH hexadecimal number NO m0 AT ss 1 2 gt 2 3 gt 3 4 gt 4 5 gt 5 6 gt 6 7 gt 7 a gt 7 b gt 8 t gt 9 n gt 10 f gt 12 v gt 11 r gt 13 Strings are stored as 7 bit ASCII characters 14 bit core devices The least significant 7 bits of each code word are filled first Strings are aligned on word addresses for each lt VXS gt However alignment does not occur when writing abc def IDENTIFIER any undefined identifier It is converted to a macro identifier and set to the current cdata word address The purpose is to provide an automatic way to
107. n the header file for the device pragma wideConstData The device must also contain the following special purpose registers char EEADRH EEADR EEDATH EEDATA bit EEPGD RD When a constant table contains less than 256 byte of data there will be a tradeoff between speed and size Using a return table executes faster but requires more code when the table contains more than 40 50 bytes of data If speed is required the following pragma statement defines a new limit for size optimization fpragma wideConstData 200 return table limit Data of size 16 bit or more The compiler allows access of 8 16 24 and 32 bits data including fixed and floating point formats When using arrays or structures with more than 256 byte data single data items have to be aligned Alignment means that there should not be any remainder when dividing the offset with the size of the data item This is only a problem when defining structures containing data of different sizes const long tl1 5 10000 10000 0 30000 1 F const uns24 th 1000000 OxFFFFFF 9000000 const int32 ti 1000000000 Ox7FFFFFFF 900000000 const fixed8 8 tf 1 1 200 25 100 25 Y const float tp 1 1 200 25 23e20 Jj const double td 1 1 200 25 23e 30 const floati16 ts 1 1 200 25 23e 30 l1 tl i reading a long integer d td x reading a double fl
108. nd line option r makes CC5X generate relocatable assembly This file is then assembled by MPASM and linked together with other C and assembly modules by MPLINK This can automated by using make to build the whole application in several stages NOTE that if you need the application program to be as compact as possible then it is recommended to use only ONE C module Source code modularity is obtained by using many C files and include these in the main C module by using include Command line options r generate relocatable assembly no hex file r2 file lkr use separate section for interrupt External assembler options x file assembler xC progra 1 mplab mpasm exe X option assembler option X q all options must be separate Assembly file options normally not used rp N name on codepage 0 is PROG lt N gt default PROG1 rb N name on RAM bank 0 is BANK lt N gt default BANKO ro N add offset lt N gt on local variable block name Variables Variables defined in other module can be accessed CC5X needs to know the type and this is done by adding extern in front of a variable definition extern char a All global variables that are not static are made available for other modules automatically CC5X inserts GLOBAL statements in the generated assembly file 70 CC5X C Compiler B Knudsen Data CC5X will generate a MOVLW LOW var name offset when using the address operators
109. nitions can look as follows void subroutine2 char p C statements bit functionl void long function2 char W void main void Function calls subroutinel subroutine2 24 bitX functionl x function2 W y xl fx3 x The compiler need to know the definition of a function before it is called to enable type checking A prototype is a function definition without statements Prototypes are useful when the function is called before it is defined The parameter name is optional in prototypes char function3 char void subroutinel void Function return values Functions can return values up to 4 bytes wide Return values can be assigned to a variable or discarded Handling and using return values is automated by the compiler The least significant byte is always placed in W when using 14 bit core devices Signed variables and variables larger than 8 bits also use temporary variables on the computed stack The 12 bit core use the W register when returning 8 bit constants All other return values are placed in return variables on the computed stack A function can return any value type The W register is used for 8 bit return value if possible The Carry flag is used for bit return values The compiler will automatically allocate a temporary variable for other return types A function with no return value is of type void Parameters in function calls There are no fixed limit on the number of para
110. not add extern to functions that are not called from other modules char localFunctionA void local function Note that it is required to use extern in the function definition when an extern prototype is not defined b variables add bank information modulel c extern shrBank char b fdefine ARRAY SIZE 10 extern bankO char array ARRAY SIZE module3 asm extern bankl char mulcnd mulplr H byte L byte C constants definitions enumerations and type information fdefine MyGlobalDef 1 enum S1 10 S2 S3 S4 S5 names assigned to port pins pragma bit in PORTB O pragma bit out PORTB 1 3 define bit variables to overlap with a char variable extern char myBits bit b1 H myBits 0 bit b2 myBits 1 use extern char myBits for global bits and put the definitions in a shared header fil Move definition char myBits to one of the modules 4 Make a linker script file according to the description stated later Follow the guidelines when using interrupts 5 Set up a makefile to enable automatic re compilation and linking Follow the guidelines when using MPLAB Edit and use the option reloc inc when compiling C modules 6 Do the final call level checking manually 7 Update conventions in assembly functions called from C modules a The bank selection bits should be updated in the beginning of assembly functions that are called from C b The page selecti
111. nternal functions and are translated into special instructions or instruction sequences btsc btss clearRAM clrwdt decfsz incfsz nop nop2 retint rl rr sleep skip swap Extensions to the standard C keywords bank0 bank bank2 bank3 bit fixed8 8 fixed24 8 floatl6 float24 float32 int8 int16 int24 int32 interrupt page0 pagel page2 page3 shrBank sizel size2 uns8 unsl6 uns24 uns32 Standard C keywords used auto break case char const continue default double enum extern do else float for goto if int long return short signed sizeof static struct switch typedef union unsigned void while define elif ifdef ifndef include endif error pragma undef The remaining standard C keywords are detected and compiled One is ignored register and the rest cause a warning to be printed volatile line 38 CC5X C Compiler B Knudsen Data Automatically defined macros and symbols The following symbols are automatically defined when using the CC5X compiler and can be used in preprocessor macros CodePages J equal to 1 4 depending on the number of code pages on the actual chip CC5X Integer version number 3002 means 3 0B CoreSet 1200 12 bit core 1400 14 bit core IRP SFR 1 if IRP is active when accessing special function registers using INDF O0 otherwise Note that pragma update IRP OI will set this macro to 0 until
112. o object format directive is printed t no tabulators normal spaces only m single source line only i no source indentation straight left margin J put source after instructions to achieve a compact assembly file b do not add rambank info to variables in the assembly file e do not add 1 to instructions when result is written back to the register 0 do not replace OPTION with OPTION REG 88 CC5X C Compiler B Knudsen Data k do not convert all hexadecimal numbers 11h gt 0x11 g do not use PROCESSOR instead of the list directive N N N label mnemonic and argument spacing Default is 8 6 10 Note that the options are CASE sensitive Some examples Default xtt mO01 INCF x ASDJ mOO01 INCF 10 xtt AC mOO1 INCF x AJ6 8 11 m001 INCF x A sb AiJ1 6 10 m001 INCF x PECES AiJs1 6 6 m001 INCF OAh PX 8 3 Variable File The variable list file contains information on the variables declared Variables are sorted by address by default but this can be changed The compiler needs the command line option V to produce this file The file name is lt src gt var The general format is V rnuD The additional letters allows the file contents to be adjusted r only variables which are referenced in the code n sort variables by name u keep the variables unsorted D use decimal numbers Variable file contents X b ADR REF name X gt I local variable G global varia
113. oat constant 27 CC5X C Compiler B Knudsen Data Code pages When using devices with more than one codepage the compiler will automatically calculate the optimal codepage for the data The disadvantage is that the compiler will not know when a codepage is full so the functions still have to be moved manually between codepages to find allowed and optimal combinations Also the data can be located on a specific codepage by using a page type modifier const pagel char tx Hello Merging data The compiler will automatically merge equal strings and sub strings and also other data items Using small tables will increase the chance of finding data items that can be merged Note that data containing initialized addresses ROM and RAM are not merged Examples 1 The string world is identical to the last part of the string Hello world It is therefore not required to use additional storage for the first string The compiler handles the address calculations so that merged or overlapping strings are handled fully automatically Note that the string termination 0 also have to be equal otherwise merging is not possible For example the string world can not be merged with the above strings 2 Merging applies to all kinds of data Data is compared byte by byte This allows the first two of the following tables to be merged with the last one const char al 10 20 30 const char a2 ab const char a3 5 10
114. of 2 seconds low power consumption 9 2 Computed Goto Computed goto is a compact and elegant way of implementing a multiselection It can also be used for storing a table of constants However the const type modifier is normally the best way to store constant data in program memory WARNING Designing computed goto s of types not described in this section may fail The generated assembly file will then have to be studied carefully because optimization and updating of the bank selection bits can be wrong The 12 bit core requires that all destinations of the computed goto are within the first half code page The 14 bit core requires that PCLATH is correctly updated before loading PCL The compiler can do ALL updating and checking automatically Study the following code samples 92 CC5X C Compiler B Knudsen Data char subO char i skip i jumps i instructions forward pragma return Hello world pragma return 10 more text 0 12 3 OxFF This is a safe and position independent method of coding return arrays or lookup constant tables It works for all PICmicro devices The compiler handles all checking and code generation issues It is possible to use return arrays like above or any C statements return 110 return Ox2F char sub01 char W Skip W using W saves one instruction pragma return Simple isn t it 0 skip W is allowed on the 12 bit core and for the first 256 addr
115. older on the hard disk where the compiler files should be located Then copy all CC5X files to this directory CC5X is now ready to compile C files Header and C source files have to be edited by a separate editor not included for instance in MPLAB If you are mainly working in a MSDOS window it may be useful to modify PATH in the AUTOEXEC BAT file to allow the operating system to find the compiler from any directory PATH 0CNCC5X This is not a required step The CC5X files can be deleted without any un installation procedure Support for Long File Names CC5X supports long file names Currently it is not possible to use spaces in file names or in directory names The truncated short format must then be used The truncated form is decided by the file system according to some rules The best guess consists of the 6 first characters of the long name plus 1 The last number is different 2 if the first 6 characters are equal to another name in the same directory If the path should be C Program Files MPLABVongfilename the equivalent short format is often CAPROGRA 1 MPLAB LONGFI 1 The name C file becomes CFILE 1 because spaces must be removed MPLAB use and display the short format only The short format name can be found when using Open File and point and click to move along a particular path User Interface The CC5X compiler is a command line program It requires a list of command line options to compile a C
116. on bits must be correct set to the current module page when returning MPLAB and MPASM support Please refer to linker txt for details on how to set up a project with several modules in MPLAB The linker script file must be made or adapted according to the description stated Note that MPASM will generate its own warnings and messages These should normally be ignored MPASM do not know about the automatic bank bit updating and will display messages about this MPASM have generated the message if the asm file extension is used in the message Program execution tracing will always use the assembly file as source when using the r option and MPASM MPLINK MPASM can generate object code from assembly modules There are some restrictions and additions when using relocatable modules compared to using a single assembly module 74 CC5X C Compiler B Knudsen Data CC5X does not support the object code directly but generates relocatable assembly that MPASM use to generate the object file MPASM is started from within the CC5X so that no extra command is required only the right command line options Case Sensitivity option in MPASM is by default On and should remain On because C use case dependent identifiers Options to start MPASM and generate relocatable object code xC progra 1 mplab mpasm exe X o X q Options when assembling and linking a single file xC progra l1 mplab mpasm exe X q If the CC5X error file option F is
117. only when the same pointer have to access locations in different 256 byte RAM segments c 8 bit pointer to program memory This pointer can access up to 256 byte data d 16 bit pointer to program memory This pointer can access more than 256 byte data e 8 bit pointer to RAM or program memory This pointer can access up to 128 byte data and 128 byte RAM Bit 7 is used to detect RAM or program memory access The compiler will only chose this format if all RAM addresses loaded to the pointer is in the same bank 14 bit core f 16 bit pointer to RAM or program memory Bit 15 is used to detect RAM or program memory access The 12 bit Core Indirect RAM access on the 16C57 58 12C500 requires some care because the RAM bank selection bits resides in the FSR register bit 5 6 The compiler can do most of the checking and generate error messages if required Automatic bankbit updating can be switched off globally b command line option or locally pragma update FSR 0 Most of the checking described is performed only if the automatic bankbit updating in ON Reading and writing arrays is straight forward bank2 char a e t 3 s 3 ac EDT s i e s i 3 e The last three statements requires that variable e is located in mapped RAM below 0x10 or in the same bank as array s Otherwise an error message is printed to indicate that the compiler can not update the bank selection bits Pointers may need a Zpragma assume statement
118. ons with more than one code page The code page of all function calls have to be known defined during compilation Otherwise the page bit updating will not be correct The page is defined by using pragma location or the page type modifier for functions defined in another module For functions defined in the current module pragma codepage can also be used It is recommended to define the function heading prototypes for all extern functions in a header file including page information This file should be included in all C modules Interrupts CC5X requires that the interrupt function is located at address 4 Writing the interrupt service routine in C using MPLINK will require some care The main issue is to set up the linker script file as described later in this file Two options are possible ALTERNATIVE 1 Use the linking sequence to locate the interrupt service routine This is done by listing the module with the interrupt service routine FIRST in the module list used by MPLINK This is the important point which makes MPLINK put the interrupt service routine in the beginning of the PROG PROGI logical code section address 4 When using MPLAB the C module containing the interrupt function must be listed first in the project file list The list file generated by MPLINK should be inspected to ensure that the interrupt routine starts at address 4 Another important point is to remove the 72 CC5X C Compiler B Knudsen Data fpragma
119. origin 4 when using MPLINK This is the only difference in the C source compared to using the built in CC5X linker single C module ALTERNATIVE 2 Set up a SEPARATE logical section in the linker script file for the interrupt service routine This is a more robust solution CC5X will generate a partial script file to avoid manual address calculation The partial script file must be included in the main script file The setup is described in Section The MPLINK script file It is also possible to design an assembly module containing the interrupt service routine Information on how to do this should be found in the MPASM MPLINK documentation Call level checking CC5X will normally check that the call level is not exceeded This is only partially possible when using MPLINK CC5X can ONLY check the current module NOT the whole linked application When calling an external function from the C code CC5X will assume that the external call is one level deep This checking is sometimes enough especially if all C code is put in one module and the assembly code modules are called from well known stack levels Calling C function from assembly will require manual analysis Therefore careful verification of the call structure is required to avoid program crash when overwriting a return value on the hardware stack which is 2 or 8 levels deep The compiler generated fcs files can provide information for this checking Calls to external functions is wr
120. pragma chip PIC16C63 include globdefl h void sub bank1 char ax bankl char i a local variable generate pulses for i 0 i lt axtl i out 1 nop2 out 0 a increment global variable EE BPR Ae MER Ae Ae RR EE eA EE AS EE GLOBDEF1 H names assigned to port pins pragma bit in PORTA 0O pragma bit out PORTA 1 modulel c extern Dank char a module3 asm extern bankl char mulcnd mulplr H byte L byte module2 c extern page0 void sub char ax module3 asm extern page0 void mpy void 78 CC5X C Compiler B Knudsen Data kk ck kCk ck kCk ck kCk ck kCk kk kk kk ck ck ck ck ck ck ck ck ck k ck ck k ck kk kk k ck kk kk kk MODULE3 ASM INCLUDE P16C63 INC BANK1 UDATA mulcnd RES 1 8 bit multiplicand mulplr RES 1 8 bit multiplier H byte RES 1 High byte of the 16 bit result L byte RES 1 Low byte of the 16 bit result count RES 1 loop counter GLOBAL mulcnd mulplr H_byte L_byte PROG1 CODE paged mpy GLOBAL mpy bsf STATUS RPO access bank 1 clrf H byte movlw 8 movwf count movf mulcnd W loop rrf mulplr F btfsc STATUS C addwf H byte F rrf H byte F rrf L byte F decfsz count F goto loop retlw 0 END J RRR RRR KKK KR KK I RR RK OK OK RK KK File 16c63 1kr
121. pragma rambank 3 char px r define LTAB 5 char tab LTAB pragma assume px in rambank 3 px amp tab 0 px r if px amp tab LTAB px amp tab 0 A pointer may access more than one bank The pragma assume statement should NOT be used in such cases The only difference is that the compiler will know the contents of the FSR 5 6 when a variable in a specific bank is accessed Therefore a statement like pointer to any rambank e requires that e in located in mapped RAM address less than 0x10 Note that the Zpragma assume statement works for single pointers and pointers in arrays but not for pointers located in structures Arrays are often more efficient than pointers 24 CC5X C Compiler B Knudsen Data i 0 LL xs tab i r if i LTAB i 0 Direct use of INDF and FSR is also possible FSR px INDF i Variable i have to reside in mapped RAM The compiler performs the checking when INDF is accessed The compiler does not try to trace the contents of FSR when it is loaded directly Therefore a statement like px r is normally preferred Using pragma assume px in rambank 3 also makes loading of px more restrictive An error message is printed if px is loaded with an address in another bank The following cases are checked px tab same as amp tab 0 px amp tab 0 px amp tab il px pxx pxx is another pointer px amp pxx il A statement lik
122. r the current operation is found The operator definition prototype sequence may therefore influence the operator selected It is not recommended to modify the libraries by adding pragma codepage between the functions Instead prototypes and pragma location or the page type modifier makes function placement easier to set up and tune For example placing the division function on codepage and the other on the default codepage can be done by include my_math h include math24f h 61 CC5X C Compiler B Knudsen Data eegend beginning of file my math h float24 operator fmul24 sharedM float24 argl sharedM float24 arg2 pagel float24 operator fdiv24 sharedM float24 argl sharedM float24 arg2 float24 operator fadd24 sharedM float24 argl sharedM float24 arg2 Ze SEC nd of file my math h Fixed point example include 16C73 h include math24x h uns16 data fixedl16 8 tx av mg a vx prev kp void main void vx 3 127 tx data automatic type cast data kp assign integer part if tx 0 tx tx make positive av tx 20 0 mg av 1 25 a mg 0 98 0 980469 error 0 000478 prev vx vx a 5 0 prev kp vx 0 036 0 03515626 error 0 024 kp vx 1 0 0 036 27 7773437 CODE 274 instructions including library 130 Floating point example CODE 635 instructions including library 470 The code is identical to the
123. r models Using 8 bit pointers when possible saves both code and RAM space CC5X allows the size of all single pointers to be decided automatically However pointers in structures and arrays have to be decided in advance by using the memory model command line options or a size type modifier Note that the operator sizeof pointer will fix the size according to the chosen default model Using sizeof pointer is normally not required and should be avoided That default pointer sizes are used only when the pointer size is not chosen dynamically The priority when deciding the pointer size is 1 Pointer size type modifiers 2 Automatic chosen pointer size single pointers 3 Pointer size chosen according to the default model Command line options mc1 default const pointer size is 1 byte 8 bits mc2 default const pointer size is 2 bytes 16 bits mr1 default RAM pointer size is 1 byte mr2 default RAM pointer size is 2 bytes mm1 default pointer size is 1 byte all pointer types mm2 default pointer size is 2 bytes all pointer types Pointer size type modifiers e sizel pointer size is 1 byte 8 bits e size2 pointer size is 2 bytes 16 bits bankl size2 float pf The supported pointer types are a 8 bit pointer to RAM The compiler will automatically update the IRP bit if required when RAM space exceeds 256 bytes 23 CC5X C Compiler B Knudsen Data b 16 bit pointer to RAM This format is required
124. riable type fixedl16 8 1 byte for decimals Error calculation 0 036 256 9 216 The byte values assigned to the variable are simply 0 0 9 The error is 9 256 0 036 0 036 0 023 The compiler prints this normalized error as a warning Type conversion The fixed point types are handled as subtypes of float Type casts are therefore infrequently required Fixed point interoperability It is recommended to stick to one fixed point format in a program The main problem when using mixed types is the enormous number of combinations which makes library support a challenge However many mixed operations are allowed when CC5X can map the types to the built in integer code generator fixed8 16 a b fixed 16 c ass bk ce OK code is generated directly a b 10 22 OK library function is supplied a b c a new user library function is required type cast can select an existing library function a b fixed8_16 c Assigning variables to RAM addresses All variables including structures and arrays can be assigned to fixed address locations This is useful for assigning names to port pins It is also possible to assign overlapping variables similar to union The syntax is 17 CC5X C Compiler B Knudsen Data type varName address type varName H lt varName2 gt bit bitName lt address gt lt bit gt bit bitName H lt varName gt lt bit gt Examples char th 0x25 bit
125. rs W STATUS and PCLATH The keyword interrupt allows the routine to be terminated by a RETFIE instruction It is possible to call a function from the interrupt routine it have to be defined by a prototype function definition first 53 CC5X C Compiler B Knudsen Data The interrupt routine requires at least one free stack location because the return address is pushed on the stack This is automatically checked by the compiler even function calls from the interrupt routine However if the program contains recursive functions then the call level can not be checked by the compiler The interrupt vector is permanently set to address 4 The interrupt service routine can only be located at this address if it is the first function defined in the program The pragma origin statement have to be used in order to skip unused program locations Vital registers such as STATUS and W should be saved and restored by the interrupt routine However registers that are not modified by the interrupt routine do not have to be saved Saving and restoring registers is device dependent The file int 6CXX H contains recommended program sequences for saving and restoring registers FSR must be saved manually if the interrupt service routine use it for accessing RAM arrays For instance to access data buffers used for serial communication The interrupt routine can also contain local variables Space is allocated separately because interrupts can occur anyti
126. ry 1 MATH32F H 32 bit floating point library MATH24LB H 24 bit floating point functions log sqrt cos 1 MATH32LB H 32 bit floating point functions log sqdrt cos 12C508 H 16C924 H PICmicro header files NEWS TXT Recent added features README TXT 1 Not available on the RED and FREE editions 2 Not available on the RED STANDARD and FREE editions 1 5 Short Program Example global variables char a bit bL 12 assign names to port pins pragma bit in PORTB O pragma bit out PORTB 1 void sub void char i a local variable generate 20 pulses for i 0 i lt 20 itt out 1 nop out 0 void main void if TO 1 amp amp PD 1 power up WARM_RESET clearRAM clear all RAM first decide the initial output level on the output port pins and then define the input output configuration This avoids spikes at the output pins 10 CC5X C Compiler B Knudsen Data PORTA 05 0010 out 1 TRISA 0b 1111 0001 xxxx 0001 a 9 value assigned to global variable do if in 0 stop if in is low break sub while a 0 9 iterations if some condition goto WARM RESET main is terminated by a SLEEP instruction 1 6 Defining the PlCmicro Device
127. s written if and only if CC5X compiles a module with an interrupt service routine It may look like CODEPAGE NAME intserv START 0x4 END 0x1C CODEPAGE NAME page0 START 0x1D END 0x7FF Example change in the main script file CODEPAGE NAME page0 START 0x4 END 0x7FF INCLUDE modulel lkr or other script file name SECTION NAME ISERVER ROM intserv Interrupt CHANGE 2 Interrupt routine in C WITHOUT a separate logical section Example change OLD VALUE 0x4 CODEPAGE NAME vectors START 0x0 END 0x3 PROTECTED OLD VALUE 0x5 CODEPAGE NAME page0 START 0x4 END 0x7FF 75 CC5X C Compiler B Knudsen Data CHANGE 3 If INTERRUPTS are not used then the first code page can start at address 1 Example change OLD VALUE 0x4 CODEPAGE NAME vectors START 0x0 END 0x0 PROTECTED OLD VALUE 0x5 CODEPAGE NAME page0 START 0x1l END 0x7FF CHANGE 4 The 12 bit core devices need a logic section for the startup vector Example change for the 16C57 OLD VALUE Ox7FF CODEPAGE NAME page3 START 0x600 END 0x7FE CODEPAGE NAME vectors START 0x7FF END 0x7FF NEW SECTION NAME STARTUP ROM vectors NEW CHANGE 5 Certain devices requires a special interrupt save sequence that needs to use certain RAM locations 0x20 and Ox AO These addresses must be
128. t for more details n while 1 H if Carry 0 m001 BTFSC status Carry GOTO m004 REDIRECTED TO m001 1 H LEE INCF i REPLACED BY INCFSZ 4 if 1 0 MOVF i REMOVED 5 BTFSS status Zero REMOVED 4 GOTO m002 REMOVED 3 vartt INCF var T test 2 m002 MOVLW 2 ADDWF test if test 0 MOVF test REMOVED 5 BTFSS status Zero_ REPLACED BY BTFSC 3 GOTO m003 REMOVED 3 z break GOTO m005 W var m003 MOVF var W if W 0 XORLW 0 REMOVED 5 BTFSS status Zero_ GOTO m004 REDIRECTED TO m001 1 break GOTO m005 REMOVED 7 69 CC5X C Compiler B Knudsen Data m004 GOTO m001 REMOVED 2 subl m005 BSF status PAO CALL subl BCF status PAO REMOVED 6 sub2 BSF status PAO REMOVED 6 BSF status PA1 CALL sub2 BCF status PAO BCF status DAT 6 8 Linker Support CC5X supports the relocateable assembly format defined by Microchip This means that MPLINK can be used to link code modules generated by CC5X also including assembly modules There are many details to be aware of It is therefore recommended to read this file carefully The important issues are related to external functions and variables ram bank updating page bit updating call level checking MPLINK script files MPLAB integration The comma
129. uired to make the intention clear unsi6 a16 char b8 c8 al6 b8 c8 warning 8 bit result al6 uns16 b8 c8 warning 8 bit result al6 uns16 b8 c8 no warning 16 bit result al6 uns8 b8 c8 no warning 8 bit result 36 CC5X C Compiler B Knudsen Data 3 6 Accessing Parts of a Variable Each bit in a variable can be accessed directly uns32 a av e fs set bit 7 of variable a to 1 if a 31 0 test bit 31 of variable a RIINA 0s bit 4 of the i th element Sak Bit 0 least significant bit Bit 7 most significant bit of a 8 bit variable Bit 15 most significant bit of a 16 bit variable Bit 23 most significant bit of a 24 bit variable Bit 31 most significant bit of a 32 bit variable eT CT Also parts of a variable can be accessed directly unsi6 a uns32 b a low8 100 set the least significant 8 bits a b highl6 load the most significant 16 bits low8 least significant byte high8 most significant byte mid8 second byte midL8 second byte midH8 third byte lowl6 least significant 16 bit midl6 middle 16 bit highl6 most significant 16 bit low24 least significant 24 bit high24 most significant 24 bit The table shows which bits are accessed depending on the variable size in bytes 1 2 3 4 and the sub index used The indicates normal use of the sub index 1 2 3 4 low8 0 7 X 0 7 0 7 hi
130. variables and function parameters in mapped RAM or bank 0 Mapped RAM is selected by pragma rambank The bank type modifier It is also possible to use the bank type modifier to select the RAM bank bank0 bank3 shrBank can replace pragma rambank shrBank is the unbanked locations if available bank1l char tx 3 tx is located in bank 1 The bank type modifier defines the RAM bank to locate the variable It can locate global variables function parameters and local variables The bank type modifier applies to the variable itself but not to the data accessed This difference is important for pointers 21 CC5X C Compiler B Knudsen Data NOTE 1 The bank type modifier have higher priority than pragma rambank NOTE 2 Using extern makes it possible to state the variable definition several times However the first definition defines the rambank and later definitions must use the same bank NOTE 3 When defining a function prototype this will normally not locate the function parameters However when adding a bank type modifier to a function parameter in a prototype this will define the bank to be used for this variable If variables are located in non existing RAM banks for a device these variables are mapped into existing RAM banks bank 0 This applies to the bank type modifiers and the pragma rambank statement Using RAM banks requires some planning The optimal placement requires least code to update the
131. vsecsssssesssessecsesseesees 85 7 4 DEBUGGING IN ANOTHER ENVIRONMENT eneeene nne tnnr ntes eniin ete ne s sse eese te reset see a eite n ae 86 S FILES PRODUCED 5 eocseecsccs senses ec eee eo cepe euo Crea os CURVE ERO eEde e Ens aee EE SYN pa Pe ear o dE Te naro dee aea Sah 88 Se THEXIFDEE es cscs uc 88 8 2 ASSEMBLY OUTPUT AE CR 88 923 VARIABEEBILEE EE 89 SUP IST EE 90 8 5 FUNCTION CALE STRUCTURE eee eerte ce ve EENS 90 9 APPEICATION NOT ES tene eo coop orae eo ra sa Rea pepe eva fo eere DU QU ae Pe vene a Uo Gu Ce dU ga aee Po vU eae ep Pee e Ue paa as 91 9 1 DEEAYS eee EO eee e 91 9 2 COMPUTED GOTO irises cakes shes nece er uere eire ERN 92 Built in skip function for computed goto eese eene entren nennen 95 Computed Goto Regions iicet pec Sed edu i e E pce feet e beled E dee eee seve 93 93 THE SWITCH S PATEMEN T WEE 96 ES VW KE 97 I USINGINTERBUPTS EE 97 A2 PREDEFINED REGISTER NAMES e e e n n nennen nnn nnn nnn a unu nnn a a un e p pn aen I s pte nna 98 A3 ASSEMBLY INSTRUCTIONS eee nn e n nen nnn nnn nn nnn e nnn e hn a a a hu n pp e nne e n tapes esset sen nna 98 Addition for the T bit core i as atte ede eere ere EE 99 Instruction execution time uu eececcccccccccsscccccseccccceeccccusecceusescecsuscessusceceuseccecsscessuceseusessecsescessuscesssuesceuee 99 CC5X C Compiler B Knudsen Data 1 INTRODUCTION Welcome to the CC5X C compiler for the Microchip PICmicro fa
132. y have the same name However the compiler adds an extension to produce an unique name in the assembly list and COD files When a function is not called defined but not in use then all parameters and local variables are truncated to the same unused location Temporary Variables Operations like multiplication division modulo division and shifts often require temporary variables However the compiler needs NO PERMANENT SPACE for temporary variables 19 CC5X C Compiler B Knudsen Data The temporary variables are allocated the same way as local variables but with a narrow scope This means that the RAM locations can be reused in other parts of the program This is an efficient strategy and often no extra space is required in application programs Arrays structures and unions One dimensional arrays is implemented Note that indexed arithmetic is limited to 8 bit Assignment is allowed for 8 16 24 and 32 bit char t 10 i index x temp unsi6 tx 3 tx i 10000 t 1 t i 20 ok t i t x 20 not allowed temp t x 20 temp ct p ll Normal C structures can be defined also nested types Unions are allowed struct hh f long a char b keete union struct char a THE 1 pp char x 4 uns32 1 uni accessing structure elements vxl a 10000 uni x 3 vxl b 10 The equivalent of a small multidimensional array can be constructed by using a structure How
133. yntax or pragma char enable equ to variable transformation pragma asm2var 1 A1 equ 0x20 CLRF Al Al is changed from an equ constant to a char variable Comments types allowed in assembly mode NOP a comment NOP C style comments are also valid CLRW NOP nested C style comments are also valid Sa Conditional assembly is allowed However the C style syntax have to be used ifdef SYMBOLA nop else clrw endif C style macros can contain assembly instructions and also conditional statements Note that the compiler does not check the contents of a macro when it is defined define UUA a b clrw movlw a if a LD A nop N endif cort P UUA 10 ax UUA 9 PORTA Most preprocessor statements can be used in assembly mode pragma return Hello 66 CC5X C Compiler B Knudsen Data The compiler can optimize and perform bank and page updating in assembly mode This does not happen automatically but has to be switched on in the source code It is normally safe to switch on optimization and bank page updating Instructions updating the bank and page register are removed before the compiler insert new instructions If the assembly contains critical timing then the settings should be left off at least in local regions default local assembly settings are b o p pragma asm default b o change default settings asm using default local settings endasm

Download Pdf Manuals

image

Related Search

Related Contents

特注品、試作品の開発はタートル工業にお任せください  MI2012-2  Dell Brocade 6520 Quick Start Manual  Especificações  取扱説明書 - お客様サポート  LH—Jetronic 2.4 (B 230 FT) - The Volvo Bertone Register  INFORMATIONS - Cour d`appel de Versailles  Tesis Laura Higueras - Roderic  Crestron electronic CEN-SW-POE    

Copyright © All rights reserved.
Failed to retrieve file