• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### AnsweredHot!How to make CRC - Reverse code

Page: 12 > Showing page 1 of 2
Author
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
0

# How to make CRC - Reverse code

Hi

I'm trying to make CRC-8 code

in google, I know that I have to make bit-reverse before CRC calculate if CRC initial is not 0x00 or 0xFF.

so, I did make bit-reverse code but wouldn't make CRC function.

here are bit-reserve code

`   unsigned int Bit_Reverse(unsigned int Normal){    unsigned int Reserve = 0;     Normal = (((Normal & 0xAAAAAAAA) >> 1) | ((Normal & 0x55555555) << 1));     Normal = (((Normal & 0xCCCCCCCC) >> 2) | ((Normal & 0x33333333) << 2));     Normal = (((Normal & 0xF0F0F0F0) >> 4) | ((Normal & 0x0F0F0F0F) << 4));    Normal = (((Normal & 0xFF00FF00) >> 8) | ((Normal & 0x00FF00FF) << 8));    Reserve = ((Normal >> 16) | (Normal << 16));    return Reserve;} `

here is part of A function

`   void A(unsigned int Buffer){         ....       Data_Reverse = Bit_Reverse(Buffer);    Data_CRC = CRC_Check(Data_Reverse);     ....}   `

and here is CRC-8 code(using reverse)

`   unsigned char CRC_Check(unsigned int CRC_DATA){    unsigned int CRC_Init = 0x9C;    unsigned int CRC_Poly = 0x39;        /*WHAT SHOULD I DO*/     /*CRC code.....*/} `

Thx geniuses for always teaching me well.
post edited by parkcoding - 2020/02/18 01:13:13

oliverb
Super Member
• Total Posts : 282
• Reward points : 0
• Joined: 2009/02/16 13:12:38
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 01:28:44 (permalink) ☄ Helpfulby parkcoding 2020/02/18 01:33:44
+1 (1)

This might help you a bit, it is an extract from a document giving an example of a CRC16 calculator that might be used in a memory-restricted MODBUS implementation. If you have 256 bytes available then CRC8 can be implemented byte-at-a-time as a look-up table. Alternatively if you want speed and small size a byte-at-a-time assembly language implementation is possible.

Calculating the CRC A 16 bit CRC register needs to be initialised with a “seed” of 0xFFFF before a packet is sent or received.Then for each byte the following procedure is followed:Exclusive-or the byte with the CRC registerThen do the following 8 times:
• Right-shift the CRC
• If the bit shifted out is non-zero then add 0xA001 to the CRC
After sending a packet the CRC is sent low byte first then high byteAfter receiving a packet a valid CRC “cancels out” the value in the CRC register leaving zero. A non-zero value indicates a corrupted transmission. It is possible to refine the process for greater performance.The modbus specification gives a procedure that uses two 256 byte lookup tables to avoid the need to loop through the bits. The procedure is optimised for speed not memory on an 8 bit CPU. The following is a simple looping implementation that calculates a CRC for data pointed to by the pointer “Msg”
unsigned int CRC16(unsigned char *Msg , unsigned int DataLen)
{
unsigned int CRC = 0xFFFF ; /* CRC byte initialized */
unsigned char Loop ;
while (DataLen--) /* pass through message buffer */
{
CRC = CRC ^ (*(Msg++)) ; /* calculate the CRC */
for(Loop=0; Loop<8; Loop++)
{
if (CRC & 1)
{
CRC >>= 1;
CRC ^= 0xA001;
}
else
{
CRC >>= 1;
}
}
}
return (CRC);
}

post edited by oliverb - 2020/02/18 01:32:12
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 01:31:49 (permalink)
0
thx Mr.oliverb

so...

can I replace 0xFF with 0x39?

CRC is really difficult :(

I will continue to review your help.

thx!
post edited by parkcoding - 2020/02/18 01:33:33
oliverb
Super Member
• Total Posts : 282
• Reward points : 0
• Joined: 2009/02/16 13:12:38
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 01:49:44 (permalink) ☄ Helpfulby parkcoding 2020/02/18 16:02:00
+1 (1)
I really really struggle with the theory, but the implementation can be simple.

All the example does is repeatedly shift bits off to the left, then if the bit that was shifted off is a "1" then exclusive-or the word with a constant that defines the CRC. Replacing 0xA001 with an 8 bit value turns it into a CRC-8 calculator.

The value is a "magic" value that determines which CRC is calculated, for example if you put in 0x8000 it just rotates the bits in a loop.

When I first saw them described it was in electronic terms, using a shift-register and exclusive-or gates.

pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 01:59:09 (permalink) ☄ Helpfulby parkcoding 2020/02/18 16:02:02
+1 (1)
Not for the first time I feel we have to remind you “int” is 16-bit on 8-bit pic, yet your reverse code assumes 32-bit int.  Why not use the stdint.h types so you know where you stand?

There’s a very good explanation and some sample 8-bit code here.

oliverb
Super Member
• Total Posts : 282
• Reward points : 0
• Joined: 2009/02/16 13:12:38
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 02:17:00 (permalink) ☄ Helpfulby parkcoding 2020/02/18 16:02:05
+1 (1)
FWIW I'm fairly sure you don't actually need to reverse bit order at runtime. If you had an application that used the opposite bit ordering to MODBUS you'd just rewrite the function to shift bits left instead of right and test the top bit, then reverse the bits in the constant.

As it is only the constant that gets reversed there should be no need for a bit reverse function.

Actually I see from that webpage that the standard implementation shifts to the left, so it looks as if the MODBUS one has been reversed to be a LSB-first system. Presumably this is to match the async serial format it was intended for.

post edited by oliverb - 2020/02/18 02:23:44
pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 02:22:19 (permalink) ☄ Helpfulby parkcoding 2020/02/18 16:02:08
+1 (1)
in google, I know that I have to make bit-reverse before CRC calculate if CRC initial is not 0x00 or 0xFF.
so, I did make bit-reverse code but wouldn't make CRC function.

And where in “Google” you got that from I do not know. But it’s wrong.
CRC is very simple, although it’s not “one algorithm”, it’s a whole class of them.
The above presented sample, for example, is for a 16-bit CRC with seed value of 0xFFFF and feedback of 0xA001.
Really it is not too hard to adjust for any particular CRC you require just by plugging the nececessary seed and feedback values.

Which particular 8-bit CRC variant were you going for?

parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 16:08:49 (permalink)
0
thx Mr.oliverb and Mr.pcbbc

I did try again but I still don't know....:(

My seed is 0x39 and poly is 0x9C

and I want 0x00 feedback result.

I made code for CRC but It is weird...

How's it?

code-1
` unsigned short calculateCRC(unsigned int crcdata){       unsigned int i,j;    unsigned short crc = 0;       for(i = 0 ;  i < 4 ; i++)    {        crc = crc ^ ((crcdata) << 8);        crcdata++;           for(j = 0; j < 8; j++)       {            if(crc & 0x80)           {                 crc = (crc << 1) ^ 0x39;            }            else          {                  crc = crc << 1;            }        }    }    return crc; }   `

code-2

`   unsigned char CRC_Check(unsigned int Data){    unsigned int CRC_Poly = 0x9C;     unsigned int CRC_Init = 0x39;    /*I don't know*/     CRC_Init = CRC_Init ^ CRC_Poly;    Data = (Data << 1);     CRC_Init = CRC_Init & 0xFF;     return ((unsigned char)CRC_Init); }   `

I should try as you advised.

http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html

this is what I already saw but I can't found CRC example what has specific initial CRC value(seed)....

but I was impressed by your kindness.

thx!
post edited by parkcoding - 2020/02/18 16:20:38
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 19:16:47 (permalink)
0
I made CRC-8-DARC table using calculator

`unsigned int CRC_Table[256] ={ 0x00, 0x39, 0x72, 0x4B, 0xE4, 0xDD, 0x96, 0xAF,  0xF1, 0xC8, 0x83, 0xBA, 0x15, 0x2C, 0x67, 0x5E,  0xDB, 0xE2, 0xA9, 0x90, 0x3F, 0x06, 0x4D, 0x74,  0x2A, 0x13, 0x58, 0x61, 0xCE, 0xF7, 0xBC, 0x85,  0x8F, 0xB6, 0xFD, 0xC4, 0x6B, 0x52, 0x19, 0x20,  0x7E, 0x47, 0x0C, 0x35, 0x9A, 0xA3, 0xE8, 0xD1,  0x54, 0x6D, 0x26, 0x1F, 0xB0, 0x89, 0xC2, 0xFB,  0xA5, 0x9C, 0xD7, 0xEE, 0x41, 0x78, 0x33, 0x0A,  0x27, 0x1E, 0x55, 0x6C, 0xC3, 0xFA, 0xB1, 0x88,  0xD6, 0xEF, 0xA4, 0x9D, 0x32, 0x0B, 0x40, 0x79,  0xFC, 0xC5, 0x8E, 0xB7, 0x18, 0x21, 0x6A, 0x53,  0x0D, 0x34, 0x7F, 0x46, 0xE9, 0xD0, 0x9B, 0xA2,  0xA8, 0x91, 0xDA, 0xE3, 0x4C, 0x75, 0x3E, 0x07,  0x59, 0x60, 0x2B, 0x12, 0xBD, 0x84, 0xCF, 0xF6,  0x73, 0x4A, 0x01, 0x38, 0x97, 0xAE, 0xE5, 0xDC,  0x82, 0xBB, 0xF0, 0xC9, 0x66, 0x5F, 0x14, 0x2D,  0x4E, 0x77, 0x3C, 0x05, 0xAA, 0x93, 0xD8, 0xE1,  0xBF, 0x86, 0xCD, 0xF4, 0x5B, 0x62, 0x29, 0x10,  0x95, 0xAC, 0xE7, 0xDE, 0x71, 0x48, 0x03, 0x3A,  0x64, 0x5D, 0x16, 0x2F, 0x80, 0xB9, 0xF2, 0xCB,  0xC1, 0xF8, 0xB3, 0x8A, 0x25, 0x1C, 0x57, 0x6E,  0x30, 0x09, 0x42, 0x7B, 0xD4, 0xED, 0xA6, 0x9F,  0x1A, 0x23, 0x68, 0x51, 0xFE, 0xC7, 0x8C, 0xB5,  0xEB, 0xD2, 0x99, 0xA0, 0x0F, 0x36, 0x7D, 0x44,  0x69, 0x50, 0x1B, 0x22, 0x8D, 0xB4, 0xFF, 0xC6,  0x98, 0xA1, 0xEA, 0xD3, 0x7C, 0x45, 0x0E, 0x37,  0xB2, 0x8B, 0xC0, 0xF9, 0x56, 0x6F, 0x24, 0x1D,  0x43, 0x7A, 0x31, 0x08, 0xA7, 0x9E, 0xD5, 0xEC,  0xE6, 0xDF, 0x94, 0xAD, 0x02, 0x3B, 0x70, 0x49,  0x17, 0x2E, 0x65, 0x5C, 0xF3, 0xCA, 0x81, 0xB8,  0x3D, 0x04, 0x4F, 0x76, 0xD9, 0xE0, 0xAB, 0x92,  0xCC, 0xF5, 0xBE, 0x87, 0x28, 0x11, 0x5A, 0x63 };`

and I'm still...

`unsigned char CRC_Check(unsigned int CRC_DATA){    unsigned int CRC_Init = 0x9C;    unsigned int CRC_Poly = 0x39;    unsigned int CRC_Table = 0;    int i = 0;     /* iterate over all byte values 0 - 255 */    for (i = 0; i < 256 ; i++)    {         CRC_Init = ((CRC_Init & CRC_Poly) << 8) ^ ((&CRC_Table[i] ^ CRC_DATA) & CRC_Init);     }         return CRC_Init;} `

so difficult :(
dan1138
Super Member
• Total Posts : 3505
• Reward points : 0
• Joined: 2007/02/21 23:04:16
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 19:22:22 (permalink)
0
The Original Poster has weak search-fu.

See: https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks

And: https://en.wikipedia.org/wiki/Cyclic_redundancy_check

And there are MANY other and perhaps better resources to be found.
pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 19:23:36 (permalink) ☄ Helpfulby parkcoding 2020/02/18 19:28:56
0
the seed is just the value you put in the CRC before you’ve processed any data. Sometimes it is zero, other times it is not. In this example (c# code) the seed is 0 (crc = 0) and the polynomial is 0x1D:
`public static byte Compute_CRC8_Simple(byte[] bytes){    const byte generator = 0x1D;    byte crc = 0; /* start with 0 so first byte can be 'xored' in */    foreach (byte currByte in bytes)    {        crc ^= currByte; /* XOR-in the next input byte */        for (int i = 0; i < 8; i++)        {            if ((crc & 0x80) != 0)            {                crc = (byte)((crc << 1) ^ generator);            }            else            {                crc <<= 1;            }        }    }    return crc;}`

Your first example is closest, but the data you are passing in is an unsigned int?? And then you are adding 1 to it in the main loop?? Thats all wrong, and it looks like you are confused about what a pointer is, and how they work.

Try passing in a pointer to a byte array (uint8_t*) of bytes to be CRCed. Then you might get somewhere. Oh and dereference the pointer when you XOR it into the crc. Then perhaps adding 1 to crcdata makes sense...

Youd also do well to stop scattering unsigned int/char around willy nilly and switch to the types defined in stdint.h. Then you might begin to think about your data typing rather than arbitrarily choosing long/int/char seemingly at random.

post edited by pcbbc - 2020/02/18 19:25:31
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 19:27:13 (permalink)
0
I found the function what can calculate CRC_table.

but it is more difficult than before.

(it is not C code)

haha

`public byte Compute(byte[] bytes) {    byte crc = crcModel.Initial;    foreach (byte b in bytes)    {        byte curByte = (crcModel.InputReflected ? CrcUtil.Reflect8(b) : b);        /* XOR-in next input byte */        byte data = (byte)(curByte ^ crc);        /* get current CRC value = remainder */        crc = (byte)(crcTable[data]);      }      crc = (crcModel.ResultReflected ? CrcUtil.Reflect8(crc) : crc);      return (byte)(crc ^ crcModel.FinalXor); } `

parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 19:44:36 (permalink)
0
thx for Mr.Pcbbc

Thank you for explaining the concept of CRC seed.

but what is "foreach(byte curryByte in bytes)" ?

come to think of it.... it look like "for()" statement..? :)

here is translated code.

` unsigned char CRC_Check(unsigned int CRC_DATA){    unsigned int CRC_Init = 0x9C;    unsigned int CRC_Poly = 0x39;    int i = 0;     /*foreach (byte currByte in bytes)*/    {          CRC_Init ^= CRC_DATA;         for(i = 0 ; i < 8 ; i++)        {             if((CRC_Init & 0x80) != 0)             {                   CRC_Init = (CRC_Init << 1) ^ CRC_Poly;              }              else             {                    CRC_Init <<= 1;             }         }     }    return CRC_Init;}`

pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 20:42:56 (permalink) ☄ Helpfulby parkcoding 2020/02/18 21:05:44
+1 (1)
You didn’t take my other advice to pass in a pointer to the data though, did you?

Yes, foreach is a for statement, but one which iterates over the length of the array of bytes.  In C you will need to pass into the function a pointer and a length variable, because in C you cannot use foreach, nor get the length of an array from the pointer.

Edit: Also you didn’t translate very well did you? You‘re still using signed/unsigned int/char seemingly at random.
What part of “byte“ in C# is the same as unsigned char in C (or better uint8_t) is hard to understand?

Also your loop counter, i, can be uint8_t type as well. As I said you’re clearly not thinking about variable sizing or typing.

post edited by pcbbc - 2020/02/18 20:52:15
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/18 21:27:18 (permalink)
0
Mr.Pcbbc

what is array in your code?

here is translated code

`   void NCV78247_SPI_32bit_Write(unsigned int NCV78247_DATA){    uint8_t CRC_Init = 0x9C;    uint8_t CRC_Poly = 0x39;    uint8_t i = 0;     for(i = 0; i < bytes.length(); i++) /*here*/    {        CRC_Init = CRC_Init ^ NCV78247_DATA;        for(i = 0 ; i < 8 ; i++)       {            if((CRC_Init & 0x80) != 0)           {               CRC_Init = (CRC_Init << 1) ^ CRC_Poly;            }            else           {               CRC_Init <<= 1;           }        }    }      SPI_32bit_Send((unsigned int) CRC_Init);}   `

doen't there array anywhere in code you gave?

or

is the code you gave me for CRC-TABLE..?

in for state, ' i < "length of bytes array" '....

I think...

`   void NCV78247_SPI_32bit_Write(unsigned int NCV78247_DATA){    uint8_t CRC_Init = 0x9C;    uint8_t CRC_Poly = 0x39;    uint8_t i = 0;    uint8_t k = 0;    for(k = 0; k < 31; k++) /*because NCV78247's length is 32 */    {        CRC_Init = CRC_Init ^ NCV78247_DATA;        for(i = 0 ; i < 8 ; i++)       {            if((CRC_Init & 0x80) != 0)           {               CRC_Init = (CRC_Init << 1) ^ CRC_Poly;            }            else           {               CRC_Init <<= 1;           }        }    }     SPI_32bit_Send((unsigned int) CRC_Init);}   `

thx Mr.pcbbc

post edited by parkcoding - 2020/02/18 21:44:38
pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/19 04:35:22 (permalink) ☼ Best Answerby parkcoding 2020/02/19 17:44:44
+1 (1)
`public byte Compute(byte[] bytes)`

Here is the array in C# code:  "byte[]" = type specifier for an array of bytes.
You can pass arrays into methods as parameters in C#.  You can not do this is C.
Instead you must pass a pointer and a length.

You seem to have very little clue about writing C and/or are not thinking through the code you are writing.  Instead you are "coding by copy and paste" until you hit upon something that "works".  I would suggest going back a step and re-reading the sections on arrays and pointer in K&R.

A more correct function prototype for this function would be:
`void NCV78247_SPI_32bit_Write(uint8_t len, uint8_t* data)`

In case you have forgotten, in C you can dereference a byte pointer with *ptr:
`uint8_t next_byte = *data;`

And you can dereference and advance the pointer with:
`uint8_t next_byte = *data++;`

Or if it helps you understand, that is the equivalent of:
`uint8_t next_byte = *data;data = data + 1;`

`void NCV78247_SPI_32bit_Write(unsigned int NCV78247_DATA){    uint8_t CRC_Init = 0x9C;    uint8_t CRC_Poly = 0x39;    uint8_t i = 0;    uint8_t k = 0;    for(k = 0; k < 31; k++) /*because NCV78247's length is 32 */    // Not it's NOT length of 32.  It's an unsigned int.    // a) An unsigned int is 16-bits in 8-bit PIC XC8.    // b) You are doing the outer loop one for each BIT.  You should be doing it once for each BYTE.    //     The code to do each bit is already in the inner i = 0 to 7 loop.    {        // This next line is just wrong.  It's taking the low byte of the 16-bit value NCV78247_DATA        // *every time* round the outer loop.  You want the next byte of data each time round the loop,        // not the same byte of data each time.  The "correct" way to do that is pass in a pointer to an        // array of bytes and a length of the data to be CRCed as in the function prototype I gave above.        CRC_Init = CRC_Init ^ NCV78247_DATA;        for(i = 0 ; i < 8 ; i++)       {            if((CRC_Init & 0x80) != 0)           {               CRC_Init = (CRC_Init << 1) ^ CRC_Poly;            }            else           {               CRC_Init <<= 1;           }        }    }     SPI_32bit_Send((unsigned int) CRC_Init);}`

pcbbc
Super Member
• Total Posts : 1691
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/19 04:51:30 (permalink) ☄ Helpfulby parkcoding 2020/02/19 17:44:49
+1 (1)
Oh, and this line....
`SPI_32bit_Send((unsigned int) CRC_Init);`

From the name, this is sending a 32-bit value?
I know the answers to the following questions.  I am posting them here to get you to think about data types:
What type/size is CRC_Init?
What type/size is unsigned int in XC8?
Why do you think that I think this might be wrong?
parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/19 17:48:59 (permalink)
0
thx! Mr.pcbbc..

Thank you very much for teaching me! :)

I will study what you pointed out cuz I'm newbie...

You are reallllllllllllllllly genius.

parkcoding
Senior Member
• Total Posts : 143
• Reward points : 0
• Joined: 2019/03/13 16:17:10
• Location: 0
• Status: offline
Re: How to make CRC - Reverse code 2020/02/20 20:01:11 (permalink)
0
Mr.Pcbbc

` void NCV78247_SPI_32bit_Write(unsigned int NCV78247_DATA) <<< here{    uint8_t CRC_Init = 0x9C;    uint8_t CRC_Poly = 0x39;    uint8_t i = 0;    uint8_t k = 0;`

[/code]

I did set NCV78247_DATA is 3byte.  ex) NCV78247_DATA = 0xFFF;

Does that work regardless of size of NCV78247_DATA?

or I need to shift NCV78247_DATA and others?

thx!
post edited by parkcoding - 2020/02/20 20:02:57
ric
Super Member
• Total Posts : 27074
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
Re: How to make CRC - Reverse code 2020/02/20 20:24:23 (permalink) ☄ Helpfulby parkcoding 2020/02/20 21:04:26
+1 (1)
parkcoding
I did set NCV78247_DATA is 3byte.  ex) NCV78247_DATA = 0xFFF;

That is NOT three bytes.
It is three nibbles = 1.5 bytes

Does that work regardless of size of NCV78247_DATA?

An "unsigned int" is 16 bits. Your value is only 12 bits, so fits fine into a 16 bit variable.

You have been advised multiple times to use standard variable types (e.g. uint16_t) rather than the traditional C types, as you seem to keep forgetting how big integers are in XC8.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
Page: 12 > Showing page 1 of 2