
#include <16F1619.h>
#device PASS_STRINGS = IN_RAM
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#fuses NOWDT

 
 
#use delay(internal=32000000)
#priority INT_EXT
#use rs232(baud=9600,parity=N,bits=8,rcv=PIN_B5,xmit=PIN_b6,stream=PORT1)
#define START 0x01
#define END 0x0D
//Send: FF 10 03 E9 00 01 02 00 02 4A 0C
//unsigned int8 massage[10]={0xff,0x10,0x03,0xe9,0x00,0x01,0x02,0x00,0x02};
//unsigned int8 massage[10]={0xff,0x01,0x00,0x00,0x00,0x08};
unsigned int8 massage[10]={0x01,0x04,0x00,0x01,0x00,0x02};
//unsigned int8 massage[10]={0x02,0x04,0x00,0x01,0x00,0x01};
//unsigned int8 massage[10]={0x01,0x04,0x02,0x01,0x31};

//unsigned int8 massage[10]= {0x09, 0xFF , 0x00 , 0x00 , 0x07 , 0x01 , 0x7F , 0x00 , 0xFF , 0xFF};

unsigned int16 getcrc;
unsigned int8 bytelow,bytehigh;
unsigned int16 swap_byte;

unsigned int16 ModBus_CRC16( unsigned char * Buffer, unsigned short Length )
{
//unsigned int8 CRCHi = 0xFF;
//unsigned int8 CRCLo = 0xFF;
//int8 Index;
//unsigned int16 ret;
signed int8 n;
n=length;

unsigned int16 cur_crc=0xFFFF;
//cur_crc=0xFFFF;
do
{
signed int8 i = 8;
 
cur_crc = cur_crc ^ *Buffer++;

do
{
if (0x0001 & cur_crc)
{
cur_crc >>= 1;
cur_crc ^= 0xA001;
}
else
{
cur_crc >>= 1;
}
i--;
}
while (i>0);
 n--; }
while (n>0);
return cur_crc;



}


void main()
{
  getcrc= ModBus_CRC16(massage,sizeof(massage));//printf("test.............\r\n");
  bytehigh=getcrc;
  getcrc=getcrc>>=8;
  bytelow=getcrc;
  getcrc=make16(bytehigh,bytelow);
   printf("%04LX\r\n",getcrc);

   while(TRUE)
  { 

   }
}  






 

