# [PIC]Question About Crc8 Picbasic Implementation

N

#### Nexting

##### Guest
Hi To All:

im Trying to build a vending unit that uses serial data to get general information counters,status,serial an anotherInfo. IÂ´m trying to implement a crc routine based in this Picbasic crc8 routine.
Code:
CRC8:
FOR CRC_IDX = 0 to 7
IF (CRC.7 ^ CRC_IN.7) THEN
CRC = (CRC << 1) ^ CRC_Poly 'equals($31) ELSE CRC = CRC << 1 eNDIF CRC_IN = CRC_IN << 1 NEXT CRC_IDX RETURN But this only works when do you have the hex string value previously defined on the code this is not my case,for example when i need interrogate the serial id i send the value in hex ($23,$47) the right reply for the unit is reply ($23,$47) serial number($01,$08,$24,$36,$49) and crc8(?)this data not always the same and could change in new units. my question is this the most simple way to build a crc? could be implemented when the data is out over Hserout? or perhaps is more complex. Sorry for extend this explanation but is not to clear for me. Really appreciatte any advice about this issue.

Best Regards

Nexting

#### betwixt

##### Super Moderator
Staff member
I'm not sure I understand the question.

The algorithm is the same regardless of the format the digits are presented. I think this is written in Oshonsoft BASIC so the only reference with a '$' before it is a comment. I think it should work whatever the number format is. Brian. #### FvM ##### Super Moderator Staff member The CRC algorithm as functional as such, question is how it's used in the application, if CRC is initialized correctly, how you feed CRC_IN. This can't be seen from your code snippet. N #### Nexting ##### Guest Hi Thanks for answers sorry maybe some confuse about just i put the crc calculation i find a complete example to try to explain better. Code: CRC VAR BYTE CRC_IDX VAR BYTE CRC_IN VAR BYTE CRC_Poly CON$31

CRC8:
FOR CRC_IDX = 0 to 7
IF (CRC.7 ^ CRC_IN.7) THEN
CRC = (CRC << 1) ^ CRC_Poly
ELSE
CRC = CRC << 1
ENDIF
CRC_IN = CRC_IN << 1
NEXT CRC_IDX
RETURN

X      VAR BYTE

CRC = 0
FOR X = 0 TO 8
LOOKUP X,[$AD,$42, $CB,$16, $05,$06, $00,$6C, \$0E], CRC_IN 'pre defined value
HSEROUT [HEX2 CRC_IN]
GOSUB CRC8
NEXT X
HSEROUT [HEX2 CRC]
when Testing This code i obtain on the output AD 42 CB 16 05 06 00 6C 0E and (CRC 87),my case is when receive in hex 23,47 the transmits sends hex 23 47 01 08 24 36 49 and crc8(?), however in te above example the value is stored on a lookup routine and i canÂ´t store the value the value on then because lookup function donÂ´t let you use variable array this always need to be constant. in my project the ouput data is changing constantly, i want to know if i can implemement this crc8 routine to process the ouput string in real time and could add the calculate crc on the output data?, or if maybe i can apply a better method. Again Thanks For the time an comments.

Best Regards

Nexting

#### betwixt

##### Super Moderator
Staff member
Basic language is horrible - such a non-standard version of it is even worse!

I can't work out what "HSEROUT [HEX2 CRC_IN]" is supposed to do. The reference manual says:
HSEROUT statement is used for serial transmission. HSEROUT statement may have multiple arguments separated by ','. One can use string variables and constants, numeric variables and byte numeric constants. If '#' sign is used before the name of a variable then its decimal string representation is sent to the serial port.
So what is 'HEX2' and what do the square brackets indicate or do they just appear in the output stream?

Brian.

#### FvM

##### Super Moderator
Staff member
You need to review the PICBASIC manual. HEX2 means two hex digit formatting.

In my view, the original question hasn't much to do with CRC algorithm. It's more general how to implement a specific program flow. Unfortunately, I don't understand what's the exact problem. Of course, the CRC8 routine can be used with variable output data. But you didn't tell about your application, the actual data source and so on.

N

#### Nexting

##### Guest
Hello:

Thanks For answers, I don,t looking about Hserin or Hserout Formating, this is a simple example to try to explain my need, my question is about crc routine implementation about insert the crc when serial information is coming in or going out. In the above example in the lookup the data are fixed and crc calculate that stream in my case the data is not the same always and need calculate while data is incoming or out. The properly question is that the crc routine posted in the first post could done that ?.

Thanks Again

Best Regards

#### FvM

##### Super Moderator
Staff member
The properly question is that the crc routine posted in the first post could done that ?