PDA

View Full Version : code conversion



dj.lambert
- 21st September 2010, 09:57
Hi All

i have a checksum calculation routine that works in Visual Basic i am trying to convert to PicBasic.

the source for the checksum are the first 32 locations in a AT93C46 EEprom (16 bit mode).

i am sure i am doing something stupid but all i get with the Picversion is FFFF,FFFF(sometimes FFFF,7FFF) independent of the eeprom contents.
after spending 5 days on this i hope some fresh eyes will spot my error.

thanks in advance

dj.lambert


VB code:

<code>
Private Sub Checksumme(dat$)
' Checksumme berechnen
Dim x, d, cs$
adrx1 = 1: adrx2 = 31: GoSub summe1x
cs$ = Right$("00" + Hex$(summe), 2)
adrx1 = 9: adrx2 = 18: GoSub summe1x
cs$ = Right$("00" + Hex$(summe), 2) + cs$
'Stop
cs = cs$

Label5.Caption = "Checksum : " & cs
If writecsum = True Then
WriteCheckSum cs$
'Stop
End If
writecsum = False
Exit Sub

summe1x:

summe = &HA5
For x = adrx1 To adrx2 Step 1
d = Mid$(dat$, x * 5 + 1, 4)
'Stop
d = Val("&H" + Mid$(dat$, x * 5 + 1, 4))
summe = (summe + (d And &HFF)) And &HFF
If (summe And 1) = 1 Then
summe = summe \ 2 + &H80
Else
summe = summe \ 2
End If
summe = (summe + (d And &HFF00) \ &H100) And &HFF
If (summe And 1) = 1 Then
summe = summe \ 2 + &H80
Else
summe = summe \ 2
End If

Next
summe = summe And &HFF

Return

End Sub

</code>

PicBasic code:

<code>
CheckSum:
adrx1 = 1
adrx2 = 31
gosub summe1x

adrx1 = 9
adrx2 = 18
gosub summe1x

return


Summe1X:
summe = $A5
for addr = adrx1 to adrx2 step 1
d= b3a(addr)
temp=d
serout2 PortB.1, 84, [hex summe, hex d, 10,13]
summe = (summe + (templo)) and $FF
serout2 PortB.1, 84, [hex summe, 10, 13]
if (summe and 1) = 1 then
summe = summe / 2 + $80
else
summe = summe / 2
endif
serout2 PortB.1, 84, [hex summe, 10, 13]
summe = (summe + (temphi) / $100) and $FF
serout2 PortB.1, 84, [hex summe, 10, 13]
if (summe and 1) = 1 then
summe = summe / 2 + $80
else
summe = summe / 2
endif
serout2 PortB.1, 84, [hex summe, 10, 13]

next
serout2 PortB.1, 84, [hex summe, 10, 13]
summe = summe and $FF
serout2 PortB.1, 84, [hex summe, 10, 13]




return



</code>
variable declarations for PB:

B3A var word(32)
temp var word
tempLo var temp.byte0
tempHi var temp.byte1
adrx1 var byte
adrx2 var byte
CheckSumme var word
Summe var word
d var word

Jerson
- 21st September 2010, 11:33
Some observations

1 - the VB code is reading 4 hex characters from locations (x*5 + 1) at a time. This is assuming VB is set to "option base 1"
so, for index 1 it reads from 6, for index 2 it reads from 11 etc
Your PB code is not doing that

2 - the VB code is expecting HEX characters and converts them to a value using the val("&H" ......) operation.
Your PB code is treating the values in the EEPROM as proper word values which they are not

dj.lambert
- 21st September 2010, 13:50
Hi Jerson

thanks for the reply

the 1st point i have removed the lines for that and forgot to put them back in (i removed them to make it a bit simpler to follow).

the second point is i think where the problem with the conversion is occurring.
i get compile errors if i try to force the variables to hex.

dj.lambert

Jerson
- 21st September 2010, 14:13
Hi Dj

You do not need to force the values to Hex, instead you need to convert the HEX values to word. Since PBP does not support a function like VB does, you need to build this code. A simple routine to convert a hex character to byte would be



' expects a HEX character with capitalized A-F values
Hex2Bin:
Hexchar = Hexchar - "0" ' remove the value of ascii 0 (30H)
if Hexchar > 9 then Hexchar = Hexchar - 7 ' convert A-F to 0a-0Fh
return


Now, you would do this 4 times once for each nibble in the word you are converting and get your Hex word like this



WordVal = 0 ' start with a val of 0
read a hex character to HexChar
gosub Hex2Bin
WordVal = WordVal * 16 + HexChar
read a hex character to HexChar
gosub Hex2Bin
WordVal = WordVal * 16 + HexChar
read a hex character to HexChar
gosub Hex2Bin
WordVal = WordVal * 16 + HexChar
read a hex character to HexChar
gosub Hex2Bin
WordVal = WordVal * 16 + HexChar


Now, WordVal would contain the 16 bit representation of the 4 character Hex String