mE's mikroPascal PRO for PIC tips, tricks & quirks

Main topics:

Libraries' replacements (version 1.11)

uGLCD library

mP PRO quirks

Tips and tricks

Assembly syntax

Replacement libraries for PIC18 (and now also enhanced family) processors are precompiled libs replacing official ones with the advantage of smaller and faster code for routines present in official libs, plus many additional routines that should simplify programming and make it more Pascal/Delphi-like. Windows program, called LibsSwitch, makes installation of the replacements painless and furthermore allows to switch between mE libs and their replacements when desired.
Available replacement libraries (P18):
  • Delays library (__Lib_Delays)
  • Strings library (__Lib_String)
  • Conversions library (__Lib_Conversions)
  • C_Stdlib library (__Lib_CStdlib)
  • 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)
For the enhanced family processors basically the same set of library replacements is available now (some file names may differ) making positive difference in code size and speed of execution.

In my 'unbiased' opinion :), using all replacement libs together is best solution. However, if one wants to use only some of them, then libs dependencies have to be taken into account (LibsSwitch takes care of that).
Conversions library uses the Strings and fixed-point Math libs. System, Internal EEPROM, and fixed-point Math libs are independent of other libraries. Source file of Delays lib has a switch (compiler directive) that needs to be commented when fixed-point Math lib replacement is not used (LibsSwitch does that automatically). The last three math libraries, on the other hand, are interconnected and need the Fixed-point math library as well as Conversions and String libs.

The precompiled libs may be used with mB PRO as well and documentation for Basic is available now.

      Download latest version (1.12) for mP/mB PRO 7.60 (3MB, 30.10.2019) Read more

uGLCD library

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

Basic properties:
	- both text and graphics may be drawn anywhere on the screen,
	- ability to draw on virtual screen(s) placed in RAM,
	- 'packed' bitmap format implemented saving program space,
	- clipping rectangle for most graphical functions,
	- routines for copying whole, or part of the screen from GLCD to RAM and back,
	- routines for icon drawing (icons from RAM or ROM), inverting, and horizontal shifting,
	- both fixed-width and variable-width fonts of any size (created, for example with Octal's
	  GLCD Font Creator) may be used,
	- increased speed performance over the standard lib, especially visible for higher clock
	- hardware-dependent part is separate and may be modified to fit particular GLCD controller.
Read more

mP PRO for PIC quirks

The ones carried from previous versions (command-line compiler only):
	- results of runtime calculations may differ from compile-time ones
	- whole range of quirks in fixed and floating-point math libraries
	- while evaluating in main a byte holding sbits declared in a unit
	  optimizer may overlook changes applied to single bits
	- applying sfr modifier to a variable declared at GPR absolute address makes
	  the linker believe it's indeed in SFR space, so it's real space is freed
	  and may be taken by another variable
	- FSR registers are not saved in ISR unless used in assembly
	- modifying variables with the use of FSR registers may confuse optimizer
	- mixing bit and boolean logic in one expression may lead to errors
	- System and Math library for enhanced processors series contain errors that
	  may lead to overwriting of unrelated parts of RAM

For more information and errors specific to latest version click on 'Read more'.
Read more

Tips and tricks

As time allows, I will be adding more mP tips. The ones collected so far:
	- internal representation of variables
	- storing/accessing variables
	- speeding up sequential memory access
	- reentrancy

Read more

Assembly syntax

Assembly syntax tended to change from version to version. Lately this tendency almost died away so, though the following was thoroughly checked for version 4.80 of mP PRO, unless stated otherwise, it is valid for later versions, as well.
	- labels declared in Pascal
		- in main:   L__main_<label_name>
		- in subroutine:  L_[<unit_name>]_<routine_name>_<label_name>
	- other labels
		- subroutine beginning:  [<unit_name>]_<routine_name>
		- subroutine end:  L_end_<routine_name>
		- beginning of main: _main
		- end of main: L_end_main
	- labels in asm block: <label_name> (label by itself requires colon)

	Note:  optional [unit_name] is added for private routines - those that do not have
	       their prototypes declared in interface section of a unit.

	- global vars: _<var_name>
	- SFRs (always global):  <SFR_name>
	- vars declared in implementation section: <unit_name>_<var_name>
	  (from v. 5.60 also variables declared as sfr)
	- local vars: <routine_name>_<var_name>

	- global vars declared with rx specifier: <var_name>
	- local vars rx'ed: <routine_name>_<var_name>
	- vars declared in implementation section rx'ed: <var_name>

	- global constants: _<const_name>
	- constants in unit implementation section: <unit_name>_<const_name>
	- local constants: <routine_name>_<const_name>
	- sbit: regular sbits (GPR) need an underscore in assembly while SFR ones do not:
	    bcf _mybit,BitPos(_mybit)   // mybit: sbit at mybyte.7;
	    bcf SFRbit,BitPos(SFRbit)   // SFRbit: sbit at PORTB.7;
	- bit variable is used in assembly like sbit one:
	    bsf _mybit,BitPos(_mybit)   // mybit: bit;

	- vars & constants declared with register specifier preserve names from Pascal

	- function result: <routine_name>_local_result
	- parameter of a routine: FARG_<routine_name>_<par_name>[+offset]

	- addresses of variables, constants and labels may be obtained with lo_addr (this
	  one may be omitted), high_adr and higher_addr operators, like
	    MOVLW       _constant
	    MOVWF       TBLPTRL
	    MOVLW       hi_addr(_constant)
	    MOVWF       TBLPTRH
	    MOVLW       higher_addr(_constant)
	    MOVWF       TBLPTRU

	Note: global vars or constants are those declared in main unit or interface section
	of another unit while local ones are those declared within a routine.
	- as compiler knows variables' distribution, adding accessed memory specifier
	  in user asm block for PIC18s is not necessary - in fact any such specifier
	  is ignored (though both ACCESS and BANKED constants are declared in any PIC18
	  processor def file, neither produces any effect)
	- starting from v 4.80 one may include in code text files containing assembly
	  with help of $I directive (as {$I filename} or {$I "filename"})
	- there's no macro assembler in mP PRO so only plain instructions are interpreted
	- for final assembly code one should look at the *.lst file - *.asm files, prepared
	  for every unit, may differ from the real code (are prepared before final
	- use of GOTO and CALL instructions is severely limited as labels in assembly
	  (as well as all other labels but the routine start) are local to a routine;
	  this means that one may only jump/call within a Pascal routine or call (jump to
	  beginning of) other Pascal routines (within Pascal scope rules) - for enhanced
	  series processors one cannot even call within a Pascal routine
	- GOTOs are treated by linker exactly like CALLs, which means that unfounded
	  'lack of stack space' error will be claimed if GOTOs lead to procedure loops
	- semicolon does not act as real comment in asm block ('end' or apostrophe are
	  interpreted by compiler and Code Explorer); also Code Assistant gets confused
	  by comments in assembly containing keywords, like 'end' or 'case'
	- one has to use explicit numbers in LFSR instructions as FSRx constants are declared
	  as addresses (equal to FSRxL). In other words, notation known from MPLAB

	      LFSR FSR0,_some_var

	  has to be replaced by

	      LFSR 0,_some_var

           (same is true for ADDFSR instruction in enhanced processors)
	 - for code larger than 64K additional GOTOs appear at $1C as compiler doesn't use
	   addresses larger than two bytes. The list of GOTOs includes all routines above 64K,
	   jumps for indirect function calls, and ISRs. Most of the GOTOs are not used if the code
	   is small, as compiler uses RCALLs (relative calls) whenever possible. Unfortunately,
	   if the call range is greater than 1k words, regular CALL is made to one of the GOTOs
	   rather than directly to a routine
	- any change to STKPTR causes simulator break
	- simulator freezes if any of bits is set changing STKPTR
	- simulator does not emulate DC bit (digital carry) in STATUS register

MikroPascal Forum
mP PRO forum
Dany's website
(Lots of mP goodies!)

Updated 14-11-2019

TopTop     Copyright © 2011-2019 by janni