Replacement libraries for PIC18s
(and enhanced family)


List of replacement libraries: Latest version (1.12) for mP/mB PRO 7.60 (contains also all documentation)

Newest version contains System and EEPROM libs' versions for PIC18FxxK40 processors (the problem present in original EEPROM libs is naturally fixed). Added is also a set of libraries for enhanced processor series that do not exhibit problems of official libraries.

Installation: There's a PC program, called LibsSwitch, that does the installation and allows to switch between mE libs and their replacements. After unpacking the files in a temporary directory, simply run the LibsSwitch program and press Help for further instructions. Follow the same procedure for updates.

LibsSwitch

A couple of short videos demonstrating the differences and ease of switching between official and replacement libs:

Using the replacement libs with mB PRO.


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.

      Read Strings lib documentation

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.

      Read Conversions lib documentation

C_Stdlib library replacement contains two pseudo-random functions from the original library. It was introduced to make use of these functions possible as there was a conflict between the abs function contained in the original library and similarly named function in Math replacement library.

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.

      Read EEPROM lib documentation

Besides some marginal optimisation, the main change introduced to System library concerns only indirect function calls, i.e. calls made using pointers. 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 is just one public routine in official System library – the Swap function, which is 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.
Starting from v. 6.40 two functions known from mC, setjmp and longjmp, were silently included in System libraries of mP and mB. Unfortunately, with a couple of errors (one was removed in v. 6.60) that make their use unadvisable. In case somebody would find a use for these functions in mP or mB, the replacement of System library contains their errorless version.

      Read System lib documentation

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:

Naturally, I could not do anything about the fact that compiler never calls signed multiplication routines for 16- and 32-bit integers (the routines are there, compiler simply does not call them as, due to errors in original signed routines, unsigned multiplication routines are called instead).

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.

      Read Math lib documentation

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.

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.

      Read MathDouble lib documentation

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 routines that were formerly contained in an additional lib, called Fpmath and distributed with earlier MathDouble lib replacement versions (up to v 0.07). 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:

Due to introduction of rounding while converting to integer types (in both implicit and explicit conversion), use of the replacement libs may require older code correction, as with the official MathDouble lib 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.

      Read f-p functions libs documentation


Using the replacement libs with mB PRO

Apart from the programming language syntax, mB PRO is identical to mP PRO in all practical aspects, like program structure, language elements, libraries structure and contents (in fact almost all mB libs are written in Pascal and assembly). Therefore, precompiled replacement libs may freely be used with mB PRO. System libs, like System, Math or MathDouble will mostly work in background, and other libs require only understanding of their procedures and functions prototypes - and only those that are absent in original libs and thus missing from Help (and for the Delays lib replacement there is a source file in Basic). Replacement libraries documentation installed by LibsSwitch for mB PRO lists the prototypes in Basic.

For those that want to take a look at documentation for mP linked to on this page, here are few tips about the syntax differences:

   - parameter passed by value
       Pascal   bb: byte
       Basic    dim bb as byte
   - parameter passed by address (reference)
       Pascal   var bb: byte
       Basic    dim byref bb as byte
   - parameters are separated by colons in Basic while in Pascal also semicolons are used
       Pascal   procedure example(a1,a2: byte; var w1: word);
       Basic    sub procedure example(dim a1,a2 as byte, dim byref w1 as word)
   - simple types' names are identical in both languages except for longword (dword)
     and float (real)
   - strings and pointers are declared in the same way in both languages while arrays
     a bit differently
       Pascal   arr: array[5] of byte
       Basic    dim arr as byte[5]
Examples:
   procedure CopyC(var Dest:string; Source: ^const char; Index,Count: word);
   sub procedure CopyC(dim byref Dest as string, dim Source as ^const char,
                       dim Index,Count as word)

   function str2real(var inst: string[17]): real;
   sub function str2real(dim byref inst as string[17]) as real


TopTop        MainHome        Copyright © 2011-2018 by janni