Conversions library for PIC18s

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Conversions library for PIC18s

#1 Post by janni » 21 May 2009 17:25

There are actually two libraries - the Conversions2 library, prepared some time ago with yo2lio's help for PIC18 processors, ported now to mP PRO and a litle expanded, and a direct replacement of the official Conversions library (__Lib_Conversions.mcl). The replacement was prepared for those that want smaller and faster code, but do not want to bother with changing names of routines, and for cases where other official libs use the conversions library.

The Conversions2 library is independent of the replacement lib, but the replacement lib uses Conversions2. It is therefore possible to use the Conversions2 library alongside the official conversions lib, but if one replaces the latter, one has to add Conversions2 lib to the ...Uses\P18 subdirectory.

For those that are unfamiliar with previous version of the Conversions2 library, a short description follows.

Conversions2 routines do not follow the way the official routines work. For example, routine Word2str converts number of type word to string of minimal length - not, like the official one, WordToStr, always to string of length 5. If one needs such formatting, one may use more general procedure, Word2strN, which may produce string of any (resonable) length with spaces filling the beginning of string. (Word2strN('123',st,5) gives st:=' 123' just like WordToStr.) The advantage of such approach is that strings of the same length may be produced (and displayed in simple way) from variables of different types:

Byte2strN(b,st,6);
Word2strN(w,st,6);
Int2strN(i,st,6);


If one needs to, one may replace spaces with other characters (like zeroes) using procedure StrReplChr from strings library replacement (http://www.mikroe.com/forum/viewtopic.php?t=20125). Additionally, Conversions2 routines converting strings to numbers detect errors and set the result to maximum value, if error occurs.

Due to use of assembly, the library produces smaller and faster code (up to several times) than the official one.

New library is also more extensive, introducing new functions and procedures. To differenciate from the official lib, different naming convention was assumed. Here is the list of routines in Conversions2 library:


Byte2Hex .................. Hex2Byte
Word2Hex ................ Hex2Word
Byte2Bin
Word2Bin
dWord2Bin
Bcd2Dec_8 ............... Dec2Bcd_8
Bcd2Dec_16 ............. Dec2Bcd_16

Byte2str .................... str2byte
Byte2strN
Short2str ................... str2short
Short2strN
Word2str ................... str2word
Word2strN
Int2str ....................... str2int
Int2strN
long2str ...................... str2long
dWord2str .................. str2dWord
float2str ..................... str2float

IP2str ......................... str2IP
MAC2str ..................... str2MAC

strU ............................ Val
strS


You'll find their descriptions in a Conversions2.txt file added to the library.

The real innovation of the library is function Val and procedures str (there are separate routines for signed and unsigned numbers) resembling those from Delphi.

Function Val converts string of length 1-11 to number of any type (but real) - defined by assignment instruction, like

byte_var:=Val('123',Res);
integer_var:=Val('-1234',Res);
dWord_var:=Val('12345678',Res);


Res (variable of type byte), like in Delphi, is zero when conversion is successful, indicates position of unrecognizable character (if spotted), or equals $FF, if the number is out of dWord type range, or negative limit for Longint.

If one uses conversions from string to numbers of different type, Val is the perfect choice, saving code space. On the other hand, if one uses only one type of variables, code will be faster when function corresponding to the type is used (like str2byte for numbers ranging from 0 to 255).

The str procedures, strU & strS, convert any (but real) unsigned/signed number to string using only required string space (i.e. working with bytes one may use just string[3] as output in strU). One may also wish to obtain strings of specified length, like

strU(number, output_string, 15);

where the number in string will be preceeded by spaces.

You may download the library here: http://www.cobir.com/ConvPRO.rar. Unpack it and place the Conversions2.mcl file in mP PRO installation directory where official libs for PIC18s are placed (...\Uses\PIC18). To be able to use the library, one has to add it's name to the uses clause in one's program:

Code: Select all

Uses Conversions2;
or add it to the *.mlk file of a chosen processor (mP PRO help gives instructions how to do it in Creating New Library chapter).
If one wants to use also the replacement lib (__Lib_Conversions.mcl), it's enough to replace the official lib in the ...\Uses\PIC18 subdirectory (it's advisable to save a copy of the official lib first).

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 21 May 2009 18:57

Wow! Thanks again!

:D :D

One question tough: will there come versions of Florin's ENC v3.5 library that uses "conversions2" in stead of the "pic_additional_string_library"?
If I try to use the conversions replacement together with the ENC libraries, I get errors about double definitions. :?:
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#3 Post by janni » 21 May 2009 22:13

Dany wrote:One question tough: will there come versions of Florin's ENC v3.5 library that uses "conversions2" in stead of the "pic_additional_string_library"?
You'd have to ask Florin about that :) , but I wouldn't think so. He's got enough work already and it's not only problem with the conversions library, but also with the strings lib. Florin merged both in his additional strings library.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 22 May 2009 08:24

Hello,

Ethernet library use from additional_string_library the following functions :

Mem_Cmp
Mem_Cpy
Str_Cut_Left
Byte2StrWithZeros
Word2StrWithZeros


It's hard now to rename and include this functions directly in the Ethernet library.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#5 Post by Dany » 22 May 2009 08:36

Thanks Florin and Janni. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#6 Post by Dany » 22 May 2009 09:59

janni wrote:...and it's not only problem with the conversions library, but also with the strings lib. Florin merged both in his additional strings library.
Hi Janni, apparently the usage of the strings replacement lib and the Additional string library of yo2lio can be used together without any problems. The names in the Additional string library have an underscore. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#7 Post by janni » 22 May 2009 13:03

Hi Dany,

Yes, Florin named strings routines in a way that wouldn't collide with the official lib, so this part of his library may be used alongside the strings replacement lib. It's different with the conversion routines, but if all the Ethernet library uses are the five routines listed in his post (BTW, thanks Florin :) ), then it's easy to write a small lib that, together with the Conversions2 lib and strings replacement lib will allow the Ethernet library to work. It'll be called the same as Additional Strings Utils lib, so the Ethernet lib will see no difference.

This way anyone will have a choice in using strings and conversions libraries. I'll send you a first version soon so you may check if such solution works.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#8 Post by Dany » 22 May 2009 15:29

janni wrote:Hi Dany,

Yes, Florin named strings routines in a way that wouldn't collide with the official lib, so this part of his library may be used alongside the strings replacement lib. It's different with the conversion routines, but if all the Ethernet library uses are the five routines listed in his post (BTW, thanks Florin :) ), then it's easy to write a small lib that, together with the Conversions2 lib and strings replacement lib will allow the Ethernet library to work. It'll be called the same as Additional Strings Utils lib, so the Ethernet lib will see no difference.

This way anyone will have a choice in using strings and conversions libraries. I'll send you a first version soon so you may check if such solution works.
Thanks in advance! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#9 Post by janni » 22 May 2009 15:40

Check your mailbox at edpnet.be :) .

ch9fod
Posts: 54
Joined: 10 May 2008 22:08
Location: Mexico

#10 Post by ch9fod » 27 May 2009 20:30

Hello! Thx for this great library. One question. Can i use this library with the older compilers? Or its just for the proversion?
Thx guys!
Ch9FOD

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#11 Post by Dany » 28 May 2009 09:01

janni wrote:Check your mailbox at edpnet.be :) .
Thanks Janni, all works fine now together with the new conversions library. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#12 Post by janni » 28 May 2009 13:29

ch9fod wrote:Hello! Thx for this great library. One question. Can i use this library with the older compilers? Or its just for the proversion?
The library was originally written for older compiler versions (starting from v 7). You'd have to use older library version for older compilers (see here http://www.mikroe.com/forum/viewtopic.php?t=11944).

Post Reply

Return to “mikroPascal PRO for PIC General”