TOP Contributors

  1. MIKROE (2779 codes)
  2. Alcides Ramos (376 codes)
  3. Shawon Shahryiar (307 codes)
  4. jm_palomino (118 codes)
  5. Bugz Bensce (97 codes)
  6. S P (73 codes)
  7. dany (71 codes)
  8. MikroBUS.NET Team (35 codes)
  9. NART SCHINACKOW (34 codes)
  10. Armstrong Subero (27 codes)

Most Downloaded

  1. Timer Calculator (139563 times)
  2. FAT32 Library (72041 times)
  3. Network Ethernet Library (57254 times)
  4. USB Device Library (47615 times)
  5. Network WiFi Library (43219 times)
  6. FT800 Library (42556 times)
  7. GSM click (29930 times)
  8. mikroSDK (28292 times)
  9. PID Library (26930 times)
  10. microSD click (26309 times)
Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

< Back
Library

Replacement libraries for PIC18 & P16enh processors (mP and mB PRO)

Rating:

5

Author: janni

Last Updated: 2019-10-31

Package Version: 1.1.2.0

Category: Other Codes

Downloaded: 1648 times

Followed by: 3 users

License: MIT license  

Replacement libs may supplant official mE libraries to decrease final code size and RAM use, increase speed of code execution and fix various quirks. Most of replacement libraries also add new functionality. Replacement libs cover the most substantial areas of coding and thus improve performance of other libraries that use the replaced ones.

No Abuse Reported

Do you want to subscribe in order to receive notifications regarding "Replacement libraries for PIC18 & P16enh processors (mP and mB PRO)" changes.

Do you want to unsubscribe in order to stop receiving notifications regarding "Replacement libraries for PIC18 & P16enh processors (mP and mB PRO)" changes.

Do you want to report abuse regarding "Replacement libraries for PIC18 & P16enh processors (mP and mB PRO)".

  • Information
  • Comments (2)

Library Blog

Replacement libraries form a group of libraries that may supplant official mE libraries to decrease final code size and RAM use, increase speed of code execution and fix various quirks. Most of replacement libraries also add new functionality. There are two kinds of libraries - the system libs, that are invisible to the user, like System and basic math libraries, and libraries that may be found in Library Manager, like Conversions, String, or math functions libraries. Replacement libs cover the most substantial areas of coding and improve performance of other libraries that use the replaced ones. For those that do not (yet) have a licence it may be good news that replacement libraries may allow to pack noticeably more code into the 2k words demo limit.

A Windows program, called LibsSwitch, makes installation of the replacements painless and furthermore allows to switch easily between mE libs and their replacements when desired. For easy access, one may add the program to Tools in compiler IDE  by placing its name and location (compiler installation directory\Packages\RepLibs\LibsSwitch.exe) in appropriate fields in Tools\Options\Tools. 

The replacement libraries, though written for mP, perform equally well in mB PRO and documentation for the latter is supplied. Note that the same archive is uploded for mP and mB and LibsSwitch may install and service libraries for one or both compilers.

Present version of replacement libraries package (1.05) was prepared for version 6.50 of both compilers. I'll try to keep the libraries updated here, though Libstock is quirky and sometimes it make take a while. Up to date version is always available at my site (link at the bottom). 

LibsSwitch

LibsSwitch

A Windows program, called LibsSwitch, makes installation of the replacements painless and furthermore allows to switch easily between mE libs and their replacements when desired.

View full image

Available replacement libraries:

  •   Delays library (__Lib_Delays)
  •   Strings library (__Lib_String)
  •   Conversions library (__Lib_Conversions)
  •   Internal EEPROM library (__Lib_EEPROM_256 and __Lib_EEPROM_1024)
  •   System library (__Lib_System)
  •   Fixed-point math library (__Lib_Math - two versions)
  •   Floating-point math library (__Lib_MathDouble - two versions)
  •   Floating-point functions library (__Lib_Trigon)
  •   Trigonometric functions library (__Lib_Trigonometry)

Delays library replacement offers slight advantage in code size and decidedly more precise VDelay_ms and VDelay_Advanced_ms procedures, which are less dependent on chosen processor clock.
As the source is provided and all but one routine have same prototypes as the original, there was no need to prepare a separate documentation for this library. The additional routine, VDelay_us, allows to obtain variable delays in sub-millisecond range. It's been written in assembly, but it still has some limitations. Real 1us resolution and accuracy may be achieved only with clock frequencies in excess of 16MHz. You'll find additional information in the source file.

Strings library, prepared some time ago with yo2lio's help for PIC18 processors, has been later ported to mP PRO and expanded quite a bit by adding several Delphi-like routines. The replacement routines are more effective, both in size and in execution time, than the originals - that was the main reason to write them.
For those that use Florin's Ethernet libs a small library was added that should be used instead of his Additional Strings Library (which cannot be used in parallel with the Conversions lib replacement). It contains just few routines that differ in naming from the Strings lib replacement and are needed by Ethernet libs. It's naturally called the same as original: pic_additional_string_library.mcl as the Ethernet libs search for it.

Due to use of assembly, the Conversions library produces smaller and faster (up to several times) code than the official one and is also more extensive, introducing new functions and procedures. For those that want just smaller and faster code, but do not want to bother with changing names of routines, the library contains all routines present in the official version. In the replacement lib some routines perform similar tasks - that's because new routines work differently than those from official lib (in fact, the latter call the former and only then change the result). The official lib routines are there both for compatibility with older code and for other official libs that use them. 


  EEPROM libraries were prepared for processors with smaller (256 bytes) and larger (>256 bytes) internal EEPROMs – just like the official libs they replace (compiler chooses the right one for given processor based on data in appropriate definition file).
Both libraries contain the same routines, only adjusted for EEPROM size. Block read/write routines have been added to allow operation on blocks of data. One may read up to 256 bytes with EEPROM_readBlk to a RAM memory locations starting at an address specified by a pointer. The amount of data that can be stored with EEPROM_writeBlk function has been intentionally limited to 32 bytes. 

Besides some marginal optimisation, the main change introduced to System library concerns only indirect function calls, i.e. calls made using pointers, and setjmp/longjmp mechanism. The original library uses Top Of Stack registers in such cases – which in turn requires blocking of interrupts for several assembly instructions. If this is undesirable, indirect function calls may be performed using PCLAT rather than TOS - and without the need to block interrupts. 
There are three public routines in official System library (Swap, setjmp, longjmp), which are naturally preserved. Added public routine, FSRcontext, should be called to force the compiler to add FSR registers to context saving/restoring in ISRs. This may prove necessary if one used FSR registers directly in Pascal in both main code and ISR (or calls routines that do it). Naturally, one may perform the context saving by oneself, instead. 
 

Math library is an improved version of the fixed-point library, free of its quirks and code-optimized to give twice smaller overall size. (Most code savings were obtained on division routines for signed numbers). In some cases it's also noticeably faster. The library comes in two versions – one of them introduces math error exception mechanism. 
Problems that were corrected:

  1. numbers of type short:
  •      division by 0 gives -128, whatever the dividend's sign,
  •      division of -128 by -1 gives -128,
  •      division of positive odd and negative even numbers by -128 gives 1, not 0,
  1. division of integer by 0 gives maximum positive value, whatever the dividend's sign,
  2. division of longint by 0 gives maximum positive value, whatever the dividend's sign,
  3. modulo operation on signed types gives results with wrong sign if second operand is negative ('signed' division routines produce wrong sign of remainder if divisor is negative),
  4. modulo operation on minimal longint number produces results smaller by one in magnitude than valid ones.

Note that problems with modulo operations, besides leading to wrong results, lead also to discrepancies between compilation-time calculations and runtime ones.
Some public routines were added to the Math library. Four of them help to exploit the exception mechanism (or just use the status byte of fixed-point calculations). There are also absolute value and square root functions as well as two additional execution-time effective multiplication routines.
The library defines also constants representing integral types' range limits.      

 

Floating-point math library replacement has also two versions - both produce smaller code than the official one and give more accurate calculation results by fixing problems that the official lib inherited from the original Microchip library, i.e.

  1. Lack of 'unbiased' rounding.
  2. Lack of rounding while converting to integer types.
  3. Lack of big numbers 'rounding', i.e. conversion error reduction.
  4. Max negative number reported as integer overflow.
  5. Overflow sign not dependent on dividend for division by zero.
  6. No protection against numbers with EXP=0 and significand<>0 that do not belong to Microchip format.

Both versions give access to floating-point operations' status byte. One of them may generate 'exceptions', i.e. allows to write code that contains provisions for calculation errors (like overflow, underflow, or division by zero). The library requires presence of appropriate version of Math library replacement.
 

 

Floating-point functions libraries, Trigon and Trigonometry, were prepared to work with MathDouble replacement of mE's system floating-point library (__Lib_MathDouble.mcl). Please do not use them separately, i.e. with the original MathDouble lib. Moreover, the Trigon library requires presence of other replacement libs, namely the Strings and Conversions libraries.
Both f-p functions libs contain all the routines present in the originals (though differently distributed) plus a set of additional routines. The replacement libraries produce smaller code than the originals, while giving more accurate calculation results, and allow to exploit the exception mechanism from Math and MathDouble lib replacements.    Detailed differences from the originals:

  1. Exception mechanism for f-p math errors.
  2. NaN (Not a Number) bit set in FPstatus for negative arguments in Sqrt and in pow(x,y) when x is negative and y is not an integral number.
  3. Added routines: rSgn, rAbs, Round, Trunc, Int, Frac, rFrexp, rMin, rMax, Sqr, conversion routines to/from string and IEEE 754 format.
  4. Corrected errors in atan2 function.
  5. Most functions are noticeably faster, some, like Sqrt - much faster.

   Due to introduction of rounding while converting to integer types( in both implicit explicit conversion), use of replacement libs may require older code correction, as with the official MathDouble library conversion was based on truncation (the fractional part of real number was simply omitted).    This issue is addressed by introducing a set of useful routines to the Trigon library - one of them, Trunc, may be used whenever rounding is not wanted. (There is also Round function which does nothing more than explicit conversion but was added for completeness.)      

 

For clarity and ease of use, strictly trigonometric functions were moved from Trigon to Trigonometry library. All functions contained in the latter are present in official libraries and as such are documented in Help.

________________________________________

As it uses both Conversions and Math Double libraries, mE's Conversions example (..\Examples\Other\Conversions), supplied with compiler, may serve as extreme, tough somewhat tentative demonstration of replacement libraries efficiency. Results of compilation and execution of this example, after switching to PIC18 processor, are presented below  

                            ROM [bytes]   execution time [ms]

mE libs                    11 093             176

replacements           4 768               54 

ALSO FROM THIS AUTHOR

uGLCD library optimized for PIC18 processors

5

uGLCD is a quite extensive library optimised for PIC18 processors. It may work with different GLCD types with screen memory organised in horizontal 'pages' (8 pixels high), like those with KS107/8, ST7565R, or uC1701 controllers. Thanks to the ability to draw on virtual screen, the library may be used with write-only displays.

[Learn More]