News:

;) This forum is the property of Proton software developers

Main Menu

Indexing a Cdata table

Started by TimB, Nov 27, 2025, 08:45 PM

Previous topic - Next topic

TimB

I'm trying to get a CRC16 routine running right and am little perplexed by the indexing

When I write my one code I'm fine I know what I'm doing (most of the time). But when have to convert code and make it work as intended I can get lost.

I have a CRC16 lookup table and the code says

The lines were

bIndex = (wCRC AND 0xFF) XOR bCurrentByte
       
' 3. Main Calculation Step (16-bit math):
'    Shift the current CRC right by 8 bits
'    XOR the result with the value looked up from the table.
wCRC = (wCRC >> 8 ) XOR CRC16_TABLE[bIndex]





I first converted it to

bIndex = (wCRC & 0xFF) ^ bCurrentByte

wCRC = (wCRC >> 8 ) ^ Cdata CRC16_TABLE[bIndex]

But got errors so then tried

bIndex = wCRC & 0xFF
bIndex = bIndex ^ bCurrentByte

wCRC = wCRC >> 8
wtemp1 = cread CRC16_TABLE [bIndex]
wCRC = wCRC ^ wtemp1


Now it compiled but not sure that its looking up the right data

Now I'm not sure if it should be
wtemp1 = cread CRC16_TABLE [bIndex * 2]

or

wtemp1 = cread16 CRC16_TABLE [bIndex]


or something else

top204

Without seeing the flash memory table, the code below is an educated guess.

It can be written as:

    bIndex = wCRC.Byte0
    bIndex = bIndex ^ bCurrentByte
    wCRC = wCRC >> 8
    wCRC = wCRC ^ CRead16 CRC16_Table[bIndex]

    Dim CRC16_Table As Flash16 = 1, 2, 3 ' ... More Data ..........


Notice the CRead16 command, and the flash memory table data created using Dim As Flash16?

Also, because the Flash memory table is set as 16-bit values, the index will automatically be calculated for each data item being 2 bytes. So the CRead16 CRC16_Table[bIndex] is OK.

TimB


This is the way the flash table is formated



'// ============================================================================
'// CRC-16 Lookup Table (512 bytes in Flash)
'// ============================================================================
'// Store in program memory on PIC18 to save RAM

CRC16_TABLE: Cdata as word    0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,_
    0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,_
    0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,_
    0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,_


And I'm using data = cread16 CRC16_TABLE[index]

Test I have run show the right return