PIC Specifics
In order to get the most from your mikroPascal PRO for PIC compiler, you should be familiar with certain aspects of PIC MCU. This knowledge is not essential, but it can provide you a better understanding of PICs’ capabilities and limitations, and their impact on the code writing.
Types Efficiency
First of all, you should know that PIC’s ALU, which performs arithmetic operations, is optimized for working with bytes. Although mikroPascal PRO for PIC is capable of handling very complex data types, PIC may choke on them, especially if you are working on some of the older models. This can dramatically increase the time needed for performing even simple operations. Universal advice is to use the smallest possible type in every situation. It applies to all programming in general, and doubly so with microcontrollers.
Get to know your tool. When it comes down to calculus, not all PIC MCUs are of equal performance. For example, PIC16 family lacks hardware resources to multiply two bytes, so it is compensated by a software algorithm. On the other hand, PIC18 family has HW multiplier, and as a result, multiplication works considerably faster.
Nested Calls Limitations
Nested call represents a function call within function body, either to itself (recursive calls) or to another function. Recursive function calls are supported by mikroPascal PRO for PIC but with limitations. Recursive function calls can't contain any function parameters and local variables due to the PIC’s stack and memory limitations.
mikroPascal PRO for PIC limits the number of non-recursive nested calls to:
- 8 calls for PIC12 family,
- 8 calls for PIC16 family,
- 31 calls for PIC18 family.
Note that some of the built-in routines do not count against this limit, due to their “inline” implementation.
Number of the allowed nested calls decreases by one if you use any of the following operators in the code: * / %
. It further decreases if you use interrupts in the program.
Number of decreases is specified by number of functions called from interrupt. Check functions reentrancy.
If the allowed number of nested calls is exceeded, the compiler will report a stack overflow error.
PIC18FxxJxx Specifics
Shared Address SFRs
mikroPascal PRO for PIC does not provide auto setting of bit for acessing alternate register. This is new feature added to pic18fxxjxx family and will be supported in future. In several locations in the SFR bank, a single address is used to access two different hardware registers. In these cases, a “legacy” register of the standard PIC18 SFR set (such as OSCCON, T1CON, etc.) shares its address with an alternate register. These alternate registers are associated with enhanced configuration options for peripherals, or with new device features not included in the standard PIC18 SFR map. A complete list of shared register addresses and the registers associated with them is provided in datasheet.
PIC16 Specifics
Breaking Through Pages
In applications targeted at PIC16, no single routine should exceed one page (2,000 instructions). If routine does not fit within one page, linker will report an error. When confront with this problem, maybe you should rethink the design of your application – try breaking the particular routine into several chunks, etc.
Limits of Indirect Approach Through FSR
Pointers with PIC16 are “near”: they carry only the lower 8 bits of the address. Compiler will automatically clear the 9th bit upon startup, so that pointers will refer to banks 0 and 1. To access the objects in banks 2 or 3 via pointer, user should manually set the IRP, and restore it to zero after the operation. The stated rules apply to any indirect approach: arrays, structures and unions assignments, etc.

- It is very important to take care of the IRP properly, if you plan to follow this approach. If you find this method to be inappropriate with too many variables, you might consider upgrading to PIC18.
- If you have many variables in the code, try rearranging them with the linker directive absolute. Variables that are approached only directly should be moved to banks 3 and 4 for increased efficiency.
PIC16 Enhanced Family Specifics
PIC16 Enhanced family has 16-bit FSRs, FSRxL and FSRxH, capable of accessing entire RAM and Flash memory.
This makes pointers to constants and pointers to variables compatible.
Linear Data Memory
The Linear Data Memory is a memory space consisted of GPRs (each 80 bytes in size) located in every bank. Together, they form a linear, continuous, memory area and allow large stacks, arrays, buffers, etc.
This means that there are no gaps between the end of GPR in one bank and the beginning of the next GPR in the following bank.
To use this memory space, ldm
memory specifier must be used.
What do you think about this topic ? Send us feedback!