Unofficial Nextion/TJC User Forum

Generate CRC16 Xmodem

With this you can generate Xmodem CRC16 you just put it into a timer put the text into va6.txt and enable the timer which I named crcgen
Note ASCII.TXT contains ASCII text ! to ~ character 33 to 126 if you use other characters you will need to add them to ASCII.TXT and change the char.val+=32 if you use those before 33

//generate CRC16
//va6.txt contains command
strlen va6.txt,count.val //get length of string
NUM1.val=0 //start a first character
crc.val=0
k.val=count.val
n1.val=count.val
count.val=0
while(k.val>0)
{
//substr ,,,
char.val=0
substr va6.txt,TXT1.txt,count.val,1
NUM1.val=1
l.val=0
//spstr ,,,
spstr ASCII.txt,SPLIT.txt,TXT1.txt,0
strlen SPLIT.txt,num1.val
char.val=num1.val
char.val+=32
n4.val=char.val //character number
char.val=char.val<<8
crc.val=crc.val^char.val
i.val=8
while(i.val>0)
{
j.val=crc.val&0x8000
if(j.val==0x8000)
{
crc.val=crc.val<<1
crc.val=crc.val^0x1021
}else
{
crc.val=crc.val<<1 //left crc shift 1
}
crc.val=crc.val&0xFFFF
i.val-=1
}
count.val+=1
NUM1.val+=1 //increment by 1
k.val-=1 // decrement by 1
}
covx num1.val,tex10.txt,0,0
n0.val=crc.val
num1.val=crc.val
crc.val=crc.val>>8
prints va6.txt,0
prints crc.val,1
prints num1.val,1
printh 0D
crcgen.en=0

2 Likes

This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).