Additional library for MikroBasic PRO 2009

General discussion on mikroBasic PRO for PIC.
Author
Message
Drigo
Posts: 66
Joined: 23 Jun 2009 10:23

Re: Additional library for MikroBasic PRO 2009

#121 Post by Drigo » 03 Feb 2012 10:34

If i want to switch on the heater of a SHT11 is this command right?

Code: Select all

sensirion_init(Portb,6,5)
sensirion_write_status($20)
And this for switch off?

Code: Select all

sensirion_init(Portb,6,5)
sensirion_write_status($00)

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

Re: Additional library for MikroBasic PRO 2009

#122 Post by peterverkaik » 04 May 2012 09:25

Hi Florin,

I need to send a packet at the Data Link Layer (also used by ARP)
but the MikroE Ethernet library does not support that.
I found this topic and downloaded the pascal source for the ENC28J60 ethernet library.
It includes a function Eth_SendARP(var ipb : IpHeader) : Boolean;
but I need to send a packet to a specific MAC address, so I'll need
a function Eth_SendDLLPacket(var macb : MacAddr; var data : array of byte);
or something like that. Maybe such a function is already in the library and
I am not seeing it.

I am trying to implement LLDP or CDP protocol so any info about what registers
in the ENC28J60 to setup are appreciated.

regards peter

LenG
Posts: 19
Joined: 10 Dec 2008 19:09
Location: Los Angeles, CA

Re: Additional library for MikroBasic PRO 2009

#123 Post by LenG » 15 May 2012 16:43

Where did it go?

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Additional library for MikroBasic PRO 2009

#124 Post by igeorge » 22 Oct 2012 14:02

Florin,
It might be a childish question, but i cannot get the software to find or use the libraries
It is a special procedure to do it ?
I know that hundreds of users did it and are happy, but i cannot.
I put the line
include "pic_additional_string_library"
but it return an error
file pic_additional_string_library.mbas not found
I checked the folder and pic_additional_string_library.mcl is there
What should i do to make it working ?
I downloaded all the links with library from you web link, but no luck
Thank you
Experience is something you don't get until just after you need it

caterpillaire
Posts: 9
Joined: 31 Jan 2013 08:11

Re: Additional library for MikroBasic PRO 2009

#125 Post by caterpillaire » 17 Feb 2015 08:35

HI Yo2lio,

i m working on a rs232 to TCP/IP boar i m using your library and pic18f4620 with mikrobasic pro.

the scenario is when received a DATA from serial port (UART) it will be sent to a server VIA TCP/IP if the server didn't send receve message the message will be stored in eeprom and re-transmitted later.

the problem is when sending the data:

Code: Select all

TCP_Open_Port(finalport)
                               setflag1 = setflag1 + 9
                               if Open_TCP_Connection(my_pc_ip_addr, remotesmsport, finalport) then  
                                       Send_TCP(my_pc_ip_addr, remotesmsport, Str_Len(data_user_str), data_user_str)
                                       writeln("tcp sent")

                               else
                               writeln("tcp not sent")
                               Close_TCP_Connection(my_pc_ip_addr, remotesmsport)
                               'TCP_Close_Port(finalport)
                               end if 
it will wait to receive the data from server:

Code: Select all

sub procedure Eth_UserTCP(dim byref dest_ip_addr_T as IpHeader, dim byref source_port_T, dest_port_T, len_T as word, dim CC_Sock as byte)

  if dest_port_T = remotesmsport then
   checkrxmsg(len_T, CC_Sock )
   exit
  end if
end sub

Code: Select all

sub procedure checkrxmsg(dim len_tt as word, dim trt as byte )
dim trtt as byte
dim strdata1 as string[30]
dim checksentflag as byte
dim txt51 as string[12]

checksentflag = Eeprom_read($C0)
delay_ms(5)
if checksentflag = 255 then
trtt = 0
  while trtt < len_tt
    strdata1[trtt] = Eth_GetByte
    inc(trtt)
  wend
  writeln(strdata1)
  txt51 = "CL DATA OK"
  check = strcheck(strdata1, txt51)
  if (check <> 0) and (check <> $FF) then
  Eeprom_write($C0,128)
  delay_ms(10)
  else
  Eeprom_write($C0,0)
  delay_ms(10)
  end if
else
exit 
end if
end sub
this dosn't work when the server send the answer this what is captured by wireshark
15221 5257.496564000 192.6.184.160 192.6.184.92 TCP 62 gcsp > spock [SYN] Seq=0 Win=8192 Len=0 MSS=1460 SACK_PERM=1
15222 5257.498935000 192.6.184.92 192.6.184.160 ICMP 90 Destination unreachable (Port unreachable)

any help is appreciated

regards,

Gerges

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

Re: Additional library for MikroBasic PRO 2009

#126 Post by yo2lio » 27 Jan 2017 20:08

Source code in basic for pic additional string library.
Enjoy!

Code: Select all

module pic_additional_string_library

sub procedure PIC_additional_string_library_version(dim byref version_ as string[$FF])
sub procedure SaveAll
sub procedure Mem_Set_Short(dim p as word, dim ch as char, dim n as byte)
sub procedure Mem_Cpy_Short(dim p1,p2 as word, dim n as byte)
sub procedure Mem_Move_Short(dim p1,p2 as word, dim n as byte)
sub function Mem_Cmp_Short(dim p1,p2 as word, dim n as byte) as byte
sub function Mem_Chr_Short(dim p as word, dim ch as char, dim n as byte) as byte
sub procedure Mem_Set(dim p as word, dim ch as char, dim n as word)
sub procedure Mem_Cpy(dim p1,p2,n as word)
sub procedure Mem_Move(dim p1,p2,n as word)
sub function Mem_Cmp(dim p1,p2,n as word) as byte
sub function Mem_Chr(dim p as word, dim ch as char, dim n as word) as word
sub function Str_Len(dim byref data_str as string[4095]) as word
sub procedure Str_Cat(dim byref inst1,inst2 as string[4095])
sub procedure Str_AppendSuf(dim byref inst as string[4095], dim ch as char)
sub procedure Str_AppendPre(dim byref inst as string[4095], dim ch as char)
sub function Str_Cpy(dim byref inst1,inst2 as string[4095]) as word
sub procedure Str_Cut_Chr(dim byref inst as string[4095], dim ch as char)
sub function Str_Chr(dim byref inst as string[4095], dim ch as char) as word
sub procedure Str_Replace_Chr(dim byref inst as string[4095], dim ch1 , ch2 as char)
sub function Str_Cmp(dim byref inst1,inst2 as string[4096]) as byte
sub procedure Str_Split(dim byref inst1,inst2 as string[4095], dim n as word)
sub procedure Str_Insert_Chr(dim byref inst1 as string[4095], dim chr_ as byte, dim n as word)
sub procedure Str_Cut_Left(dim byref Str_inx as string[4095], dim n as word)
sub procedure Str_Cut_Right(dim byref Str_inx as string[4095], dim n as word)
sub procedure LongWord2StrWithZeros(dim data_in as longword, dim byref data_str as string[10])
sub procedure Word2StrWithZeros(dim data_in as word, dim byref data_str as string[5])
sub procedure Byte2StrWithZeros(dim data_in as byte, dim byref data_str as string[3])
sub procedure Byte2Hex(dim data_hex as byte, dim byref hex__ as string[2])
sub procedure Word2Hex(dim data_hex as word, dim byref hex__ as string[4])
sub function Hex2Byte(dim byref hex_in as string[2]) as byte
sub function Hex2Word(dim byref hex_in as string[4]) as word
sub function Str2Byte(dim byref byte_in as string[3]) as byte
sub function Str2Short(dim byref byte_in as string[4]) as short
sub function Str2Word(dim byref word_in as string[5]) as word
sub function Str2Int(dim byref word_in as string[6]) as integer
sub function Str2LongWord(dim byref dWord_in as string[10]) as longword
sub function Str2Long(dim byref Long_in as string[11]) as longint
sub procedure Short2StrWithZeros(dim data_in1 as short, dim byref data_str3 as string[4])
sub procedure Int2StrWithZeros(dim data_in1 as integer, dim byref data_str3 as string[6])
sub procedure LongInt2StrWithZeros(dim data_in1 as longint, dim byref data_str3 as string[11])
sub procedure Short2Str(dim data_in1 as short, dim byref data_str3 as string[4])
sub procedure Int2Str(dim data_in1 as integer, dim byref data_str3 as string[6])
sub procedure LongInt2Str(dim data_in1 as longint, dim byref data_str3 as string[11])
sub procedure Byte2Str(dim data_in1 as byte, dim byref data_str3 as string[3])
sub procedure Word2Str(dim data_in1 as word, dim byref data_str3 as string[5])
sub procedure LongWord2Str(dim data_in1 as longword, dim byref data_str3 as string[10])
sub procedure Float2Str(dim data_in4 as float, dim byref data_str4 as string[17], dim digit as byte) ' olnly 4 digits after "."
sub procedure Float2Str_5(dim data_in4 as float, dim byref data_str4 as string[17], dim digit as byte) ' olnly 5 digits after "."
sub function Str2Float(dim byref data_str4 as string[17]) as float
sub procedure Ip2Str(dim byref ip_in as byte[4], dim byref str_out as string[15])
sub procedure Mac2Str(dim byref mac_in as byte[6], dim byref str1_out as string[12])
sub procedure Str2Ip(dim byref str_in as string[15], dim byref ip_out as byte[4])
sub procedure Str2Mac(dim byref str1_in as string[12], dim byref mac_out as byte[6])
sub procedure Str2Ip_(dim byref str_in as string[15], dim addr_ip_out as word)
sub procedure Str2Mac_(dim byref str1_in as string[12], dim addr_mac_out as word)
sub function BcdToDec(dim number as byte) as byte
sub function BcdToDec16(dim number as word) as word
sub function DecToBcd(dim number as byte) as byte
sub function DecToBcd16(dim number as word) as word
sub procedure CpyFlash2Mem(dim Faddr as longword, dim Maddr_,nb as word)
sub function CpyFlashString2Mem(dim Faddr as longword, dim Maddr_ as word) as word
sub function CmpFlashWithMem(dim Faddr as longword, dim Maddr_,nb as word) as byte
sub function CmpFlashStringWithMem(dim Faddr as longword, dim Maddr_ as word) as byte
sub function CmpFlashWithFlash(dim Faddr1,Faddr2,nb as longword) as byte
sub procedure ConvertWithAutoScale(dim v_in as longint, dim byref str_res as string[13], dim decimals as word)

implements

sub procedure PIC_additional_string_library_version(dim byref version_ as string[$FF])
  version_ = "PIC_A_S_L V4.5 PRO 14-APR-2010"
end sub

sub procedure SaveAll
  asm
    movf FSR0L,w
    movf FSR1L,w
    movf FSR2L,w
    movf PRODL,w
  end asm
end sub

sub procedure Mem_Set(dim p as word, dim ch as char, dim n as word)
dim
  i as word
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = p
  i = 0
  while i < n
   'begin
     POSTINC2 = ch
     inc(i)
   wend
  FSR2 = SS_FSR2
end sub

sub procedure Mem_Set_Short(dim p as word, dim ch as char, dim n as byte)
dim
  i as byte
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = p
  i = 0
  while i < n

     POSTINC2 = ch
     inc(i)
   wend
  FSR2 = SS_FSR2
end sub

sub procedure Mem_Cpy(dim p1,p2,n as word)
dim
  i as word
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = p1
  FSR0 = p2
  i = 0
  while i < n

    POSTINC2 = POSTINC0
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Mem_Cpy_Short(dim p1,p2 as word, dim n as byte)
dim
  i as byte
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = p1
  FSR0 = p2
  i = 0
  while i < n

    POSTINC2 = POSTINC0
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Mem_Move(dim p1,p2,n as word)
dim
  i as word
  dir_ as boolean
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  dir_ = p1 > p2
  if dir_ then

      FSR2 = p1 + n -1
      FSR0 = p2 + n -1
  else

      FSR2 = p1
      FSR0 = p2
    end if
  i = 0
  while i < n

    if dir_ then POSTDEC2 = POSTDEC0
     else POSTINC2 = POSTINC0
    end if
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Mem_Move_Short(dim p1,p2 as word, dim n as byte)
dim
  i as byte
  dir_ as boolean
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  dir_ = p1 > p2
  if dir_ then

      FSR2 = p1 + n -1
      FSR0 = p2 + n -1
  else

      FSR2 = p1
      FSR0 = p2
    end if
  i = 0
  while i < n

    if dir_ then POSTDEC2 = POSTDEC0
     else POSTINC2 = POSTINC0
    end if
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Mem_Cmp(dim p1,p2,n as word) as byte
dim
  i as word
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = p1
  FSR0 = p2
  i = 0
  while i < n

    result = POSTINC2 - POSTINC0
    if result <> 0 then break
    end if
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Mem_Cmp_Short(dim p1,p2 as word, dim n as byte) as byte
dim
  i as byte
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = p1
  FSR0 = p2
  i = 0
  while i < n

    result = POSTINC2 - POSTINC0
    if result <> 0 then break
    end if
    inc(i)
   wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Mem_Chr(dim p as word, dim ch as char, dim n as word) as word
dim
  i as word
  SS_FSR2 as word

  SS_FSR2 = FSR2
  i = 0
  result = $FFFF
  FSR2 = p
  while i < n

    if POSTINC2 = ch then break
    end if
    inc(i)
   wend
  if i < n then result = i
  end if
  FSR2 = SS_FSR2
end sub

sub function Mem_Chr_Short(dim p as word, dim ch as char, dim n as byte) as byte
dim
  i as byte
  SS_FSR2 as word

  SS_FSR2 = FSR2
  i = 0
  result = $FF
  FSR2 = p
  while i < n

    if POSTINC2 = ch then break
    end if
    inc(i)
   wend
  if i < n then result = i
  end if
  FSR2 = SS_FSR2
end sub

sub function Str_Len(dim byref data_str as string[4095]) as word
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = word(@data_str)
  asm
asl1:
    movf  POSTINC2,w
    bnz   asl1
    movf  POSTDEC2,w
  end asm
  result = FSR2 - word(@data_str)
  FSR2 = SS_FSR2
end sub

sub procedure Str_Cat(dim byref inst1,inst2 as string[4095])
dim
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@inst1)
  FSR0 = word(@inst2)
  asm
    movf INDF2,F
    bz asl2
asl3:
    movf PREINC2,F
    bnz asl3
asl2:
    movf POSTINC0,W
    bz asl4
    movwf POSTINC2
    bnz asl2
asl4:
    clrf INDF2
  end asm
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str_AppendSuf(dim byref inst as string[4095], dim ch as char)
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = word(@inst)
  asm
    movf INDF2,F
    bz asl9
asl10:
    movf PREINC2,F
    bnz asl10
asl9:
  end asm
  POSTINC2 = ch
  INDF2 = 0
  FSR2 = SS_FSR2
end sub

sub procedure Str_AppendPre(dim byref inst as string[4095], dim ch as char)
dim
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@inst)
  asm
    clrf WREG
asl11:
    incf WREG,F
    movf POSTINC2,F
    bnz asl11
    movff FSR2L,FSR0L
    movff FSR2H,FSR0H
    movf POSTDEC0,F         ' changed by me movf        POSTDEC2,F
asl12:
    movff POSTDEC0,POSTDEC2 ' changed by me movff        POSTDEC2,POSTDEC0
    decfsz WREG,F
    bra asl12
  end asm
  INDF2 = ch  ' changed by me INDF0 = ch
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str_Cpy(dim byref inst1,inst2 as string[4095]) as word
dim
  SS_FSR0, SS_FSR2 as word

  result = $FFFF
  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@inst1)
  FSR0 = word(@inst2)
  asm
asl13:
  end asm
  inc(result)
  asm
    movf POSTINC0,W
    movwf POSTINC2
    bnz asl13
  end asm
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str_Cut_Chr(dim byref inst as string[4095], dim ch as char)
dim
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@inst)
  WREG = ch
  asm
    xorlw 0
    bz asl19
asl20:
  end asm
  WREG = ch
  asm
    xorwf POSTINC2,W
    bz asl20
    movf POSTDEC2,W
asl19:
  end asm
  FSR0 = word(@inst)
  asm
asl21:
    movf POSTINC2,W
    movwf POSTINC0
    bnz asl21
  end asm
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str_Chr(dim byref inst as string[4095], dim ch as char) as word
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  result = $FFFF
  FSR2 = word(@inst)
  asm
    clrf R10
    clrf R11
asl15:
  end asm
  WREG = ch
  asm
    xorwf INDF2,W
    bz asl14
    infsnz R10,F
    incf R11,F
    movf POSTINC2,W
    bnz asl15
    bra asl16
asl14:
  end asm
  FSR2 = word(@result)
  asm
    movff R10,POSTINC2
    movff R11,INDF2
asl16:
  end asm
  FSR2 = SS_FSR2
end sub

sub procedure Str_Replace_Chr(dim byref inst as string[4095], dim ch1 , ch2 as char)
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = word(@inst)
  asm
asl28:
    movf INDF2,W
    bz asl26
  end asm
  WREG = ch1
  asm
    xorwf INDF2,W
    bnz asl27
  end asm
  WREG = ch2
  asm
    movwf INDF2
asl27:
    movf POSTINC2,F
    bnz asl28
asl26:
  end asm
  FSR2 = SS_FSR2
end sub

sub function Str_Cmp(dim byref inst1,inst2 as string[4096]) as byte
dim
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@inst1)
  FSR0 = word(@inst2)
  while true

      result = INDF2 - POSTINC0
      if result <> 0 then break
      end if
      if POSTINC2 = 0 then break
      end if
    wend
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str_Split(dim byref inst1,inst2 as string[4095], dim n as word)
' Split inst1 in 2 strings after n char
dim
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  inst2 = ""
  if n >= str_Len(inst1) then goto end_label
  end if
  FSR2 = word(@inst1) + n
  FSR0 = word(@inst2)
  INDF0 = INDF2
  INDF2 = 0
  while PREINC2 <> 0  PREINC0 = INDF2
  wend
  PREINC0 = 0
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str_Insert_Chr(dim byref inst1 as string[4095], dim chr_ as byte, dim n as word)
' Insert Chr at position n
dim
  k as word
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  k = str_Len(inst1)
  if n > k then goto end_label
  end if
  FSR2 = word(@inst1) + k
  PREINC2 = 0
  FSR0 = word(@inst1) + k
  asm
    movf POSTDEC2,w
    movf POSTDEC0,w
  end asm
  while n < k

      POSTDEC2 = POSTDEC0
      inc(n)
    wend
  INDF2 = chr_
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str_Cut_Left(dim byref Str_inx as string[4095], dim n as word)

  FSR2 = word(@Str_inx)
  FSR0 = FSR2 + n
  while INDF0 <> 0  POSTINC2 = POSTINC0
  wend
  INDF2 = 0
end sub

sub procedure Str_Cut_Right(dim byref Str_inx as string[4095], dim n as word)
dim
  i as word

  i = Str_Len(Str_inx)
  if i < n then exit
  end if
  FSR2 = word(@Str_inx) + i - n
  INDF2 = 0
end sub

sub procedure adjust_1

  asm
    movf INDF2,w
    andlw $0F
    addlw $30
    movwf POSTDEC0
    swapf POSTINC2,w
    andlw $0F
    addlw $30
    movwf POSTDEC0
  end asm
end sub

sub procedure LongWord2StrWithZeros(dim data_in as longword, dim byref data_str as string[10])
dim
  loop_cnt,j as byte
  buf as byte[9]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@buf)
  POSTINC2 = Lo(data_in)
  POSTINC2 = Hi(data_in)
  POSTINC2 = Higher(data_in)
  POSTINC2 = Highest(data_in)
  POSTINC2 = 0
  POSTINC2 = 0
  POSTINC2 = 0
  POSTINC2 = 0
  INDF2 = 0
  loop_cnt = 0
  while loop_cnt < 32

      FSR2 = word(@buf)
      asm
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf INDF2,f
      end asm
      if loop_cnt = 31 then

          FSR0 = word(@data_str) + 10
          FSR2 = word(@buf) + 4
          POSTDEC0 = 0
          adjust_1
          adjust_1
          adjust_1
          adjust_1
          adjust_1
          goto end_label
        end if
      FSR2 = word(@buf) + 3
      j = 0
      while j < 5

          asm
            movf POSTINC2,w
                  movlw        0x03
                  addwf        INDF2,w
                  btfsc        WREG,3
                  movwf        INDF2
                  movlw        0x30
                  addwf        INDF2,w
                  btfsc        WREG,7
                  movwf        INDF2
          end asm
          inc(j)
        wend
      inc(loop_cnt)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Word2StrWithZeros(dim data_in as word, dim byref data_str as string[5])
dim
  loop_cnt,j as byte
  buf as byte[5]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@buf)
  POSTINC2 = Lo(data_in)
  POSTINC2 = Hi(data_in)
  POSTINC2 = 0
  POSTINC2 = 0
  INDF2 = 0
  loop_cnt = 0
  while loop_cnt < 16

      FSR2 = word(@buf)
      asm
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf INDF2,f
      end asm
      if loop_cnt = 15 then

          FSR0 = word(@data_str) + 5
          FSR2 = word(@buf) + 2
          POSTDEC0 = 0
          adjust_1
          adjust_1
          asm
            movf INDF2,w
            andlw $0F
            addlw $30
            movwf POSTDEC0
          end asm
          goto end_label
        end if
      FSR2 = word(@buf) + 1
      j = 0
      while j < 3

          asm
            movf POSTINC2,w
            movlw        0x03
            addwf        INDF2,w
            btfsc        WREG,3
            movwf        INDF2
            movlw        0x30
            addwf        INDF2,w
            btfsc        WREG,7
            movwf        INDF2
          end asm
          inc(j)
        wend
      inc(loop_cnt)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Byte2StrWithZeros(dim data_in as byte, dim byref data_str as string[3])
dim
  loop_cnt,j as byte
  buf as byte[3]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@buf)
  POSTINC2 = data_in
  POSTINC2 = 0
  INDF2 = 0
  loop_cnt = 0
  while loop_cnt < 8

      FSR2 = word(@buf)
      asm
        rlcf POSTINC2,f
        rlcf POSTINC2,f
        rlcf INDF2,f
      end asm
      if loop_cnt = 7 then

          FSR0 = word(@data_str) + 3
          FSR2 = word(@buf) + 1
          POSTDEC0 = 0
          adjust_1
          asm
            movf INDF2,w
            andlw $0F
            addlw $30
            movwf POSTDEC0
          end asm
          goto end_label
        end if
      FSR2 = word(@buf)
      j = 0
      while j < 2

          asm
            movf POSTINC2,w
            movlw        0x03
            addwf        INDF2,w
            btfsc        WREG,3
            movwf        INDF2
            movlw        0x30
            addwf        INDF2,w
            btfsc        WREG,7
            movwf        INDF2
          end asm
          inc(j)
        wend
      inc(loop_cnt)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Byte2Hex(dim data_hex as byte, dim byref hex__ as string[2])
dim
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR0 = word(@hex__)
  FSR2 = word(@data_hex)
  asm
    swapf INDF2,w
    andlw $0F
    addlw $30
    movwf INDF0
  end asm
  if INDF0 > $39 then INDF0 = INDF0 + $07
  end if
  asm
    movf POSTINC0,w
    movf INDF2,w
    andlw $0F
    addlw $30
    movwf INDF0
  end asm
  if INDF0 > $39 then INDF0 = INDF0 + $07
  end if
  PREINC0 = 0
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Word2Hex(dim data_hex as word, dim byref hex__ as string[4])
dim
  buf_h as string[2]

  Byte2Hex(Hi(data_hex),buf_h)
  hex__[0] = buf_h[0]
  hex__[1] = buf_h[1]
  Byte2Hex(Lo(data_hex),buf_h)
  hex__[2] = buf_h[0]
  hex__[3] = buf_h[1]
  hex__[4] = 0
end sub

sub function adjust_hex_ as byte

  if ((INDF2 >= "0") and (INDF2 <= "9")) then result = (INDF2 - 48)
  else if ((INDF2 >= "A") and (INDF2 <= "F")) then result = (INDF2 - 55)
  else if ((INDF2 >= "a") and (INDF2 <= "f")) then result = (INDF2 - 87)
  else result = 0 end if end if end if
end sub

sub function Hex2Byte(dim byref hex_in as string[2]) as byte
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = word(@hex_in)
  WREG = adjust_hex_
  asm
    swapf WREG,f
  end asm
  result = WREG
  asm
    movf POSTINC2,w
  end asm
  result = result + adjust_hex_
  FSR2 = SS_FSR2
end sub

sub function Hex2Word(dim byref hex_in as string[4]) as word
dim
  buf_h as string[2]

  buf_h[0] = hex_in[0]
  buf_h[1] = hex_in[1]
  buf_h[2] = 0
  result = Hex2Byte(buf_h)*256
  buf_h[0] = hex_in[2]
  buf_h[1] = hex_in[3]
  buf_h[2] = 0
  result = result + Hex2Byte(buf_h)
end sub

sub procedure adjust_char_

  if ((INDF0 >= "0") and (INDF0 <= "9")) then POSTDEC2 = POSTDEC0
  else

      asm
        movf POSTDEC0,w
        movf POSTDEC2,w
      end asm
    end if
end sub

sub function Str2Byte(dim byref byte_in as string[3]) as byte
dim
  i as byte
  buf_str as string[3]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "000"
  i = Str_Len(byte_in)
  if i > 3 then goto end_label
  end if
  FSR0 = word(@byte_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str)
  asm
    movf POSTINC2,w
    movf POSTINC2,w
  end asm
  while i > 0

      adjust_char_
      dec(i)
    wend
  WREG = buf_str[0] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  result = PRODL
  WREG = buf_str[1] - "0"
  asm
    movwf R10
    movlw 10
    mulwf R10
  end asm
  result = result + PRODL + (buf_str[2] - "0")
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str2Short(dim byref byte_in as string[4]) as short
dim
  i as byte
  buf_str as string[4]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "0000"
  i = Str_Len(byte_in)
  if i > 4 then goto end_label
  end if
  FSR0 = word(@byte_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str)
  asm
    movf POSTINC2,w
    movf POSTINC2,w
    movf POSTINC2,w
  end asm
  while i > 0

      adjust_char_
      dec(i)
    wend
  WREG = buf_str[1] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  result = PRODL
  WREG = buf_str[2] - "0"
  asm
    movwf R10
    movlw 10
    mulwf R10
  end asm
  if byte_in[0] = "-" then result = 0 - result - PRODL - (buf_str[3] - "0")
  else result = result + PRODL + (buf_str[3] - "0")
  end if
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str2Word(dim byref word_in as string[5]) as word
dim
  i as byte
  buf_str as string[5]
  SS_FSR0, SS_FSR2 as word
  buf as word absolute 0xFF3


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "00000"
  i = Str_Len(word_in)
  if i > 5 then goto end_label
  end if
  FSR0 = word(@word_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str) + 4
  while i > 0

      adjust_char_
      dec(i)
    wend
  WREG = buf_str[0] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := ((PRODH * 256) + PRODL)*100;
  result = buf*100
  WREG = buf_str[1] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := result + ((PRODH * 256) + PRODL)*10;
  result = result + buf*10
  WREG = buf_str[2] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := result + ((PRODH * 256) + PRODL);
  result = result + buf
  WREG = buf_str[3] - "0"
  asm
    movwf R10
    movlw 10
    mulwf R10
  end asm
  result = result +  PRODL + (buf_str[4] - "0")
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str2Int(dim byref word_in as string[6]) as integer
dim
  i as byte
  buf_str as string[6]
  SS_FSR0, SS_FSR2 as word
  buf as word absolute 0xFF3


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "000000"
  i = Str_Len(word_in)
  if i > 6 then goto end_label
  end if
  FSR0 = word(@word_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str) + 5
  while i > 0

      adjust_char_
      dec(i)
    wend
  WREG = buf_str[1] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := ((PRODH * 256) + PRODL)*100;
  result = buf*100
  WREG = buf_str[2] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := result + ((PRODH * 256) + PRODL)*10;
  result = result + buf*10
  WREG = buf_str[3] - "0"
  asm
    movwf R10
    movlw 100
    mulwf R10
  end asm
  'result := result + ((PRODH * 256) + PRODL);
  result = result + buf
  WREG = buf_str[4] - "0"
  asm
    movwf R10
    movlw 10
    mulwf R10
  end asm
  if word_in[0] = "-" then result = 0 - result -  PRODL - (buf_str[5] - "0")
  else result =  result + PRODL + (buf_str[5] - "0")
  end if
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str2LongWord(dim byref dWord_in as string[10]) as longword
dim
  i as byte
  buf_str as string[10]
  buf_str1,buf_str2,buf_str3 as string[4]
  res1,res2,res3 as longword
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "0000000000"
  i = Str_Len(dWord_in)
  if i > 10 then goto end_label
  end if
  FSR0 = word(@dWord_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str) + 9
  while i > 0

      adjust_char_
      dec(i)
    wend
  FSR0 = word(@buf_str)
  FSR2 = word(@buf_str1)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  FSR2 = word(@buf_str2)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  FSR2 = word(@buf_str3)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  res1 = Str2Byte(buf_str1)
  res2 = Str2Word(buf_str2)
  res3 = Str2Word(buf_str3)
  result = res1*100000000 + res2*10000 + res3
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function Str2Long(dim byref Long_in as string[11]) as longint
dim
  i as byte
  buf_str as string[11]
  buf_str1,buf_str2,buf_str3 as string[4]
  res1,res2,res3 as longint
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0
  buf_str = "00000000000"
  if Long_in[0] = "-" then

      buf_str[0] = "-"
    end if
  i = Str_Len(Long_in)
  if i > 11 then goto end_label
  end if
  FSR0 = word(@Long_in) + i
  asm
    movf POSTDEC0,w
  end asm
  FSR2 = word(@buf_str) + 10
  while i > 0

      adjust_char_
      dec(i)
    wend
  FSR0 = word(@buf_str)
  FSR2 = word(@buf_str1)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  FSR2 = word(@buf_str2)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  FSR2 = word(@buf_str3)
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  POSTINC2 = POSTINC0
  INDF2 = 0
  res1 = Str2short(buf_str1)
  res2 = Str2Word(buf_str2)
  res3 = Str2Word(buf_str3)
  if Long_in[0] = "-" then result = res1*100000000 - res2*10000 - res3
  else result = res1*100000000 + res2*10000 + res3
  end if
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Short2StrWithZeros(dim data_in1 as short, dim byref data_str3 as string[4])
dim
  data_str4 as string[3]
  buf as byte

  data_str3[0] = "0"
  data_str3[1] = 0
  buf = data_in1
  if TestBit(buf,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  Byte2StrWithZeros(buf, data_str4)
  Str_Cat(data_str3, data_str4)
end sub

sub procedure Int2StrWithZeros(dim data_in1 as integer, dim byref data_str3 as string[6])
dim
  data_str4 as string[5]
  buf as word
  buf1 as byte

  data_str3[0] = "0"
  data_str3[1] = 0
  buf = data_in1
  buf1 = Hi(data_in1)
  if TestBit(buf1,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  Word2StrWithZeros(buf, data_str4)
  Str_Cat(data_str3, data_str4)
end sub

sub procedure LongInt2StrWithZeros(dim data_in1 as longint, dim byref data_str3 as string[11])
dim
  data_str4 as string[10]
  buf as longword
  buf1 as byte

  data_str3[0] = "0"
  data_str3[1] = 0
  buf = data_in1
  buf1 = Highest(data_in1)
  if TestBit(buf1,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  LongWord2StrWithZeros(buf, data_str4)
  Str_Cat(data_str3, data_str4)
end sub

sub procedure Short2Str(dim data_in1 as short, dim byref data_str3 as string[4])
dim
  data_str4 as string[3]
  buf as byte

  data_str3[0] = 0
  data_str3[1] = 0
  buf = data_in1
  if TestBit(buf,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  Byte2StrWithZeros(buf, data_str4)
  Str_Cut_Chr(data_str4, "0")
  if Str_Len(data_str4) = 0 then

      data_str4[0] = "0"
      data_str4[1] = 0
    end if
  Str_Cat(data_str3, data_str4)
end sub

sub procedure Int2Str(dim data_in1 as integer, dim byref data_str3 as string[6])
dim
  data_str4 as string[5]
  buf as word
  buf1 as byte

  data_str3[0] = 0
  data_str3[1] = 0
  buf = data_in1
  buf1 = Hi(data_in1)
  if TestBit(buf1,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  Word2StrWithZeros(buf, data_str4)
  Str_Cut_Chr(data_str4, "0")
  if Str_Len(data_str4) = 0 then

      data_str4[0] = "0"
      data_str4[1] = 0
    end if
  Str_Cat(data_str3, data_str4)
end sub

sub procedure LongInt2Str(dim data_in1 as longint, dim byref data_str3 as string[11])
dim
  data_str6 as string[10]
  buf as longword
  buf1 as byte

  data_str3[0] = 0
  data_str3[1] = 0
  buf = data_in1
  buf1 = Highest(data_in1)
  if TestBit(buf1,7) = 1 then

      buf = not(buf) + 1
      data_str3[0] = "-"
    end if
  LongWord2StrWithZeros(buf, data_str6)
  Str_Cut_Chr(data_str6, "0")
  if Str_Len(data_str6) = 0 then

      data_str6[0] = "0"
      data_str6[1] = 0
    end if
  Str_Cat(data_str3, data_str6)
end sub

sub procedure Byte2Str(dim data_in1 as byte, dim byref data_str3 as string[3])

  Byte2StrWithZeros(data_in1, data_str3)
  Str_Cut_Chr(data_str3, "0")
  if Str_Len(data_str3) = 0 then

      data_str3[0] = "0"
      data_str3[1] = 0
    end if
end sub

sub procedure Word2Str(dim data_in1 as word, dim byref data_str3 as string[5])

  Word2StrWithZeros(data_in1, data_str3)
  Str_Cut_Chr(data_str3, "0")
  if Str_Len(data_str3) = 0 then

      data_str3[0] = "0"
      data_str3[1] = 0
    end if
end sub

sub procedure LongWord2Str(dim data_in1 as longword, dim byref data_str3 as string[10])

  LongWord2StrWithZeros(data_in1, data_str3)
  Str_Cut_Chr(data_str3, "0")
  if Str_Len(data_str3) = 0 then

      data_str3[0] = "0"
      data_str3[1] = 0
    end if
end sub

sub procedure Float2Str(dim data_in4 as float, dim byref data_str4 as string[17], dim digit as byte) ' olnly 4 digits after "."
dim
  buf_f as longint
  txt10 as string[10]


  data_str4[0] = 0
  buf_f = longint(data_in4)
  LongInt2Str(buf_f, txt10)
  if digit = 0 then goto end_float
  end if
  data_in4 = data_in4 - float(buf_f)
  if data_in4 < 0.0 then

      data_in4 = data_in4 * -100000.0
      if txt10[0] <> "-" then data_str4 = "-"
      end if
  else data_in4 = data_in4 * 100000.0
  end if
'  data_in4 := data_in4 + 0.9;
  Str_Cat(data_str4, txt10)
  Word2StrWithZeros(longword(longint(data_in4) div 10), txt10)
  if digit > 4 then digit = 4
  end if
  Mem_Cpy(word(@txt10), word(@txt10)+1, digit)
  txt10[digit] = 0
  Str_Cat(data_str4, ".")
end_Float:
  Str_Cat(data_str4, txt10)
end sub

sub procedure Float2Str_5(dim data_in4 as float, dim byref data_str4 as string[17], dim digit as byte) ' olnly 5 digits after "."
dim
  buf_f as longint
  txt10 as string[10]


  data_str4[0] = 0
  buf_f = longint(data_in4)
  LongInt2Str(buf_f, txt10)
  if digit = 0 then goto end_Float1
  end if
  data_in4 = data_in4 - float(buf_f)
  if data_in4 < 0.0 then

      data_in4 = data_in4 * -100000.0
      if txt10[0] <> "-" then data_str4 = "-"
      end if
  else data_in4 = data_in4 * 100000.0
  end if
'  data_in4 := data_in4 + 0.9;
  Str_Cat(data_str4, txt10)
  LongWord2StrWithZeros(longint(data_in4), txt10)
  if digit > 5 then digit = 5
  end if
  Mem_Cpy(word(@txt10), word(@txt10)+5, digit)
  txt10[digit] = 0
  Str_Cat(data_str4, ".")
end_Float1:
  Str_Cat(data_str4, txt10)
end sub

sub function Str2Float(dim byref data_str4 as string[17]) as float
dim
  txt10 as string[10]
  lungime,lungime1 as word
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  result = 0.0
  lungime = Str_Len(data_str4)
  if (lungime = 0) or (lungime > 17) then goto end_label
  end if
  lungime1 = Str_Chr(data_str4,".")
  if lungime1 = 0 then goto end_label
  end if
  if lungime > (lungime1 + 6) then lungime = lungime1 + 6
  end if
  txt10 = "00000"
  FSR0 = word(@data_str4) + lungime1
  POSTINC0 = 0
  FSR2 = word(@txt10)
  inc(lungime1)
  while lungime1 < lungime

      POSTINC2 = POSTINC0
      inc(lungime1)
    wend
    if data_str4[0] = "-" then result = float(Str2Long(data_str4)) - float(Str2LongWord(txt10))/100000.0
    else result = float(Str2Long(data_str4)) + float(Str2LongWord(txt10))/100000.0
    end if
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Ip2Str(dim byref ip_in as byte[4], dim byref str_out as string[15])
dim
  txt3 as string[3]
  S_FSR0,S_FSR2 as word
  k,j as byte
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  str_out = ""
  FSR0 = word(@ip_in)
  FSR2 = word(@str_out)
  j = 0
  while j < 4

      k = POSTINC0
      S_FSR0 = FSR0
      S_FSR2 = FSR2
      Byte2Str(k,txt3)
      FSR2 = S_FSR2
      FSR0 = word(@txt3)
      while INDF0 <> 0  POSTINC2 = POSTINC0
      wend
      if j < 3 then POSTINC2 = "."
      end if
      inc(j)
      FSR0 = S_FSR0
    wend
  INDF2 = 0
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Mac2Str(dim byref mac_in as byte[6], dim byref str1_out as string[12])
dim
  txt2 as string[2]
  S_FSR0,S_FSR2 as word
  k,j as byte
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR0 = word(@mac_in)
  FSR2 = word(@str1_out)
  j = 0
  while j < 6

      k = POSTINC0
      S_FSR0 = FSR0
      S_FSR2 = FSR2
      Byte2Hex(k,txt2)
      FSR2 = S_FSR2
      POSTINC2 = txt2[0]
      POSTINC2 = txt2[1]
      FSR0 = S_FSR0
      inc(j)
    wend
  INDF2 = 0
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str2Ip(dim byref str_in as string[15], dim byref ip_out as byte[4])
dim
  i,j,k,g as byte
  txt3 as string[3]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@ip_out)
  POSTINC2 = 0
  POSTINC2 = 0
  POSTINC2 = 0
  INDF2 = 0
  i = 0
  j = 0
  FSR0 = word(@str_in)
  while INDF0 <> 0

      if POSTINC0 = "." then inc(j)
      end if
      inc(i)
    wend
  if (j <> 3) then goto end_label
  end if
  if (i < 7) then goto end_label
  end if
  if (i > 15) then goto end_label
  end if
  FSR2 = word(@ip_out)
  i = 0
  k = 0
  while k < 4

      j = 0
      while str_in[i] <> "."

          g = str_in[i]
          if g = 0 then break
          end if
          if j > 2 then goto end_label
          end if
          txt3[j] = g
          inc(j)
          inc(i)
        wend
      txt3[j] = 0
      inc(i)
      POSTINC2 = Str2Byte(txt3)
      inc(k)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str2Mac(dim byref str1_in as string[12], dim byref mac_out as byte[6])
dim
  i as byte
  txt2 as string[2]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR0 = word(@mac_out)
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  INDF0 = 0
  if Str_Len(str1_in) <> 12 then goto end_label
  end if
  FSR2 = word(@str1_in)
  FSR0 = word(@mac_out)
  i = 0
  while i < 6

      txt2[0] = POSTINC2
      txt2[1] = POSTINC2
      txt2[2] = 0
      POSTINC0 = Hex2Byte(txt2)
      inc(i)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str2Ip_(dim byref str_in as string[15], dim addr_ip_out as word)
dim
  i,j,k,g as byte
  txt3 as string[3]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = addr_ip_out
  POSTINC2 = 0
  POSTINC2 = 0
  POSTINC2 = 0
  INDF2 = 0
  i = 0
  j = 0
  FSR0 = word(@str_in)
  while INDF0 <> 0

      if POSTINC0 = "." then inc(j)
      end if
      inc(i)
    wend
  if (j <> 3) then goto end_label
  end if
  if (i < 7) then goto end_label
  end if
  if (i > 15) then goto end_label
  end if
  FSR2 = addr_ip_out
  i = 0
  k = 0
  while k < 4

      j = 0
      while str_in[i] <> "."

          g = str_in[i]
          if g = 0 then break
          end if
          if j > 2 then goto end_label
          end if
          txt3[j] = g
          inc(j)
          inc(i)
        wend
      txt3[j] = 0
      inc(i)
      POSTINC2 = Str2Byte(txt3)
      inc(k)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure Str2Mac_(dim byref str1_in as string[12], dim addr_mac_out as word)
dim
  i as byte
  txt2 as string[2]
  SS_FSR0, SS_FSR2 as word


  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR0 = addr_mac_out
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  POSTINC0 = 0
  INDF0 = 0
  if Str_Len(str1_in) <> 12 then goto end_label
  end if
  FSR2 = word(@str1_in)
  FSR0 = addr_mac_out
  i = 0
  while i < 6

      txt2[0] = POSTINC2
      txt2[1] = POSTINC2
      txt2[2] = 0
      POSTINC0 = Hex2Byte(txt2)
      inc(i)
    wend
end_label:
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function BcdToDec(dim number as byte) as byte
dim
  SS_FSR2 as word

  SS_FSR2 = FSR2
  FSR2 = word(@number)
  asm
    movlw $0F
    andwf INDF2,w
  end asm
  result = WREG
  asm
    swapf INDF2,f
    movlw $0F
    andwf INDF2,f
    movlw 10
    mulwf INDF2
  end asm
  result = result + PRODL
  FSR2 = SS_FSR2
end sub

sub function BcdToDec16(dim number as word) as word
dim
  Number1,Number2 as word
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  FSR2 = word(@number)
  asm
    movlw $0F
    andwf INDF2,w
  end asm
  result = WREG
  asm
    swapf INDF2,f
    movlw $0F
    andwf INDF2,f
    movlw 10
    mulwf INDF2
  end asm
  result = result + PRODL
  asm
    movlw $0F
    andwf PREINC2,w
    movwf R11
    movlw 100
    mulwf R11
  end asm
  FSR0 = word(@Number2)
  POSTINC0 = PRODL
  INDF0 = PRODH
  asm
    swapf INDF2,f
    movlw $0F
    andwf INDF2,f
    movlw 100
    mulwf INDF2
  end asm
  FSR0 = word(@Number1)
  POSTINC0 = PRODL
  INDF0 = PRODH
  result = result + Number1 * 10 + Number2
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub function DecToBcd(dim number as byte) as byte
dim
  n1,n2 as byte
  SS_FSR2 as word

  SS_FSR2 = FSR2
  n1 = number div 10
  n2 = number - n1*10
  FSR2 = word(@result)
  INDF2= n1
  asm
    swapf INDF2,f
  end asm
  WREG = n2
  asm
    iorwf INDF2,f
  end asm
  FSR2 = SS_FSR2
end sub

sub function DecToBcd16(dim number as word) as word
dim
  n1,n2,n3,n4,n6 as byte
  n5 as word
  SS_FSR0, SS_FSR2 as word

  SS_FSR0 = FSR0
  SS_FSR2 = FSR2
  n1 = word(number div 1000)
  n5 = number - n1*1000
  n2 = word(n5 div 100)
  n6 = n5 - n2*100
  n3 = n6 div 10
  n4 = n6 - n3*10

  FSR2 = word(@result)
  INDF2= n3
  asm
    swapf INDF2,f
  end asm
  WREG = n4
  asm
    iorwf INDF2,f
    movf POSTINC2,w
  end asm
  INDF2= n1
  asm
    swapf INDF2,f
  end asm
  WREG = n2
  asm
    iorwf INDF2,f
  end asm
  FSR0 = SS_FSR0
  FSR2 = SS_FSR2
end sub

sub procedure CpyFlash2Mem(dim Faddr as longword, dim Maddr_,nb as word)
dim
  i as word

  TBLPTRL = Lo(Faddr)
  TBLPTRH = Hi(Faddr)
  TBLPTRU = Higher(Faddr)
  FSR2 = Maddr_
  i = 0
  while i < nb

      asm
        TBLRD*+
      end asm
      POSTINC2 = TABLAT
      inc(i)
    wend
end sub

sub function CpyFlashString2Mem(dim Faddr as longword, dim Maddr_ as word) as word

  result = 0
  TBLPTRL = Lo(Faddr)
  TBLPTRH = Hi(Faddr)
  TBLPTRU = Higher(Faddr)
  FSR2 = Maddr_
  while true

      asm
        TBLRD*+
      end asm
      if TABLAT = 0 then exit
      end if
      POSTINC2 = TABLAT
      inc(result)
    wend
end sub

sub function CmpFlashWithMem(dim Faddr as longword, dim Maddr_,nb as word) as byte
dim
  i as word

  TBLPTRL = Lo(Faddr)
  TBLPTRH = Hi(Faddr)
  TBLPTRU = Higher(Faddr)
  FSR2 = Maddr_
  i = 0
  while i < nb

      asm
        TBLRD*+
      end asm
      result = POSTINC2 - TABLAT
      if result <> 0 then exit
      end if
      inc(i)
    wend
end sub

sub function CmpFlashStringWithMem(dim Faddr as longword, dim Maddr_ as word) as byte

  TBLPTRL = Lo(Faddr)
  TBLPTRH = Hi(Faddr)
  TBLPTRU = Higher(Faddr)
  FSR2 = Maddr_
  while true

      asm
        TBLRD*+
      end asm
      result = POSTINC2 - TABLAT
      if result <> 0 then exit
      end if
      if TABLAT = 0 then exit
      end if
    wend
end sub

sub function CmpFlashWithFlash(dim Faddr1,Faddr2,nb as longword) as byte
dim
  i as longword

  i = 0
  while i < nb

      TBLPTRL = Lo(Faddr1)
      TBLPTRH = Hi(Faddr1)
      TBLPTRU = Higher(Faddr1)
      asm
        TBLRD*+
      end asm
      result = TABLAT
      TBLPTRL = Lo(Faddr2)
      TBLPTRH = Hi(Faddr2)
      TBLPTRU = Higher(Faddr2)
      asm
        TBLRD*+
      end asm
      result = result - TABLAT
      if result <> 0 then exit
      end if
      inc(i)
      inc(Faddr1)
      inc(Faddr2)
    wend
end sub

sub procedure ConvertWithAutoScale(dim v_in as longint, dim byref str_res as string[13], dim decimals as word)
dim
  str_buf as string[32]
  i as word
  j as longword
  negative as byte

  negative = 0
  if v_in < 0 then

      negative = 1
      v_in = v_in * (-1)
    end if
  LongWord2StrWithZeros(v_in,str_buf)
  if decimals > 9 then decimals = 9
  end if
  if decimals < 1 then decimals = 1
  end if
  Str_Insert_Chr(str_buf,".",(10-decimals))
  j = 10
  i = 0
  while i < decimals

      j = j * 10
      inc(i)
    wend
  i = i + 2
  while i < 11

      if v_in < j then break
      end if
      j = j * 10
      inc(i)
    wend
  if i < 11 then Str_Cut_Left(str_buf,(11-i))
  end if
  i = Str_Len(str_buf)
  if i > 12 then i = 12
  end if
  Mem_Cpy(@str_res,@str_buf,i)
  str_res[i] = 0
  if negative <> 0 then Str_AppendPre(str_res,"-")
  end if
end sub

end.
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

Post Reply

Return to “mikroBasic PRO for PIC General”