CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

1 wire Dallas CRC for PCD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Manu59114



Joined: 22 Jan 2018
Posts: 34
Location: North of France

View user's profile Send private message

1 wire Dallas CRC for PCD
PostPosted: Wed Oct 03, 2018 11:55 am     Reply with quote

Hello to all

I use with success the 1 wire CRC code for a serial communication between 2 PIC18. Very fast code to calculate the CRC !a

Now i would like to interface a board using a dsPIC33EP512MU810 (PCD)

and i was thinking that i used the code from ckielstra found on this topic:


https://www.ccsinfo.com/forum/viewtopic.php?p=60194

As we can see, the code was defined for PCM and PCH but not for PCD

Code:

#if defined(__PCM__)                     // For example (and tested on) PIC16F74, PIC16F887
    #byte FSR = GETENV("SFR:FSR")
    #byte INDF = GETENV("SFR:INDF")
 #elif defined(__PCH__)                  // For example (and tested on) PIC18F4520
    #byte POSTINC0 = GETENV("SFR:POSTINC0")
 #else
    #Error You need to setup the above for your PIC Chip
 #endif


Is there someone that used this code for PCD?
What would be the code for GETENV for PCD?

Many thanks for your reply!
Wish you all the best!
Manu
_________________
CCS + ICD3 + PIC18F46K80
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Oct 03, 2018 12:18 pm     Reply with quote

You would not use the same method.
On the DsPIC, the actual XOR instruction, can use any register as it's source address (or destination), and increment either. So unlike the lesser PIC's where you have to load with indirect addressing, then do the maths, on these the maths and the fetch can all be done in one instruction!.. So you just load a register with the address to use as the source, and the fetch and first XOR, become. So perhaps W2 as the working register, and W3 as the address, gives:

XOR.B W2, [W3++], W2

would replace all of:
Quote:

#if defined(__PCM__)
movf INDF, W // load W with next databyte
incf FSR // do a 8-bit increment - increment indirection register to point to next databyte - for the next time
#elif defined(__PCH__)
movf POSTINC0, W // load w with next databyte and increment indirection register to point to next databyte - for the next time
#else
#Error You need to setup the above for your PIC Chip
#endif
xorwf crc, F
Manu59114



Joined: 22 Jan 2018
Posts: 34
Location: North of France

View user's profile Send private message

PostPosted: Wed Oct 03, 2018 1:47 pm     Reply with quote

Thanks Ttelmah,

I will test tomorrow!


MAnu
_________________
CCS + ICD3 + PIC18F46K80
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Oct 04, 2018 12:37 am     Reply with quote

I've put together a basic PCD version of this:
Code:

#INCLUDE "33EP512MU810.H"
#device PASS_STRINGS=IN_RAM

#fuses NOWDT
#use delay(internal=64MHz)

#PIN_SELECT U1TX=PIN_D1
#PIN_SELECT U1RX=PIN_D0
#use RS232(UART1, baud=9600)

#include <string.h>
unsigned int8 crc;
//-----------------------------------------------------------------------------
// This is the crc8 function for use on the PIC16 and PIC18 etc. microprocessors
unsigned int8 Calc_Crc8(unsigned int8 *p_data, unsigned int8 datalength, unsigned int8 crc)
{
#asm
   MOV p_data, W3
   CLR WREG
   MOV.B crc, WREG
   MOV WREG,W2
   CLR WREG
   MOV.B datalength,WREG
   DEC WREG, W4
   DO W4, fwd
      XOR.B W2, [W3++], W2
      CLR W5
      BTSC W2, #0
      XOR.B #0x5e, W5                 
      BTSC W2, #1
      XOR.B #0xbc, W5
      BTSC W2, #2
      XOR.B #0x61, W5
      BTSC W2, #3
      XOR.B #0xc2, W5
      BTSC W2, #4
      XOR.B #0x9d, W5
      BTSC W2, #5
      XOR.B #0x23, W5
      BTSC W2, #6
      XOR.B #0x46, W5
      BTSC W2, #7
      XOR.B #0x8c, W5
      NOP
      MOV W5, W2
   fwd:
   MOV W2, WREG
   MOV.B WREG,crc                   
#ENDASM

   return crc;
}

void main()
{

   unsigned int8 length;
   char message[] = "Hello world.";

   // Example when you have to calculate the CRC over a message in one go.   
   length = strlen(message);
   crc = Calc_Crc8(message, length, 0);
   printf("CRC-1 = %u\n", crc);
   

   // Example when you have to calculate the CRC in multiple steps.
   crc = 0;             // Set the CRC start value. 1-wire protocol uses 0, other protocols often start with 0xFF.
   crc = Calc_Crc8("Hello",  5, crc);
   crc = Calc_Crc8(" ",      1, crc);
   crc = Calc_Crc8("world.", 6, crc);
   printf("CRC-2 = %u\n", crc);
}

// Generated output:
// CRC-1 = 105
// CRC-2 = 105


Hopefully you will find this does what is needed. Smile

It'd actually be slightly simpler if done using int16's. 'byte wide' transfers can only be done into WREG, so I have to do double transfers to load these values and return this. However only a couple of extra instructions. Smile
Manu59114



Joined: 22 Jan 2018
Posts: 34
Location: North of France

View user's profile Send private message

PostPosted: Tue Oct 09, 2018 8:18 am     Reply with quote

Ttelmah

I want to say
Code:

While(true)
{
Printf("THANK YOU!!! Ttelmah for your great code!!!\r");
Printf("it's work like a charme for PCD\r");
Printf("dsPIC33EP512MU810\r");
}

MAnu
_________________
CCS + ICD3 + PIC18F46K80
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Oct 09, 2018 8:32 am     Reply with quote

Glad I got it right. Smile
guy



Joined: 21 Oct 2005
Posts: 291

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 18, 2018 9:01 pm     Reply with quote

I'm guessing that a CRC lookup table is quicker?
It could be found in the DRIVERS folder, inside the file ds1993.c
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Oct 19, 2018 12:57 am     Reply with quote

No, actually it's slower...

The algorithm used is very efficient, and takes about 30% less time than performing the same operation by a lookup (on the PIC24 at least).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group