• AVR Freaks

AnsweredHot!SPI parity check

Author
parkcoding
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
2020/02/03 00:26:33 (permalink)
0

SPI parity check

Hi everyone~!
 
I'm trying to use SPI.
 
so now I made code using XOR for Parity
 
Is that correct?
 
I want to make simple code...

I think this is the best.
 
Please advise if there is another way or if something is wrong.
 
I attached image file.
 
Thx and good luck!
 
 
 
post edited by parkcoding - 2020/02/03 00:42:22

Attached Image(s)

#1
pcbbc
Super Member
  • Total Posts : 1522
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: SPI parity check 2020/02/03 03:28:17 (permalink) ☼ Best Answerby parkcoding 2020/02/03 03:41:46
+2 (2)
"Better" in what way?
There's a hell of a lot of shifting there.  As I think we've told you before before multi bit shifts on 8-bit PICs are very inefficient.
 
You might try something like...
uint8_t Parity(uint16_t parity)
{
    uint8_t result = parity ^ (parity >> 8);
    result ^= result >> 4;
    result ^= result >> 2;
    result ^= result >> 1;
    return ~result & 1;
}

Which should involve a lot less shifts.  (Note: code untested - it's up to you to debug as necessary).
#2
parkcoding
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: SPI parity check 2020/02/03 03:41:24 (permalink)
0
yeah, that's right!
 
I got the answer finally.
 
I want to get example or reference like that code.
 
I will try it
 
thx Mr.pcbbc
#3
1and0
Access is Denied
  • Total Posts : 10356
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: SPI parity check 2020/02/03 11:03:24 (permalink) ☄ Helpfulby parkcoding 2020/02/03 19:46:40
+1 (1)
Replace this
    result ^= result >> 4;

with this
    result ^= (result >> 4) | (result << 4);

should shave a word and cycle. ;)
#4
parkcoding
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: SPI parity check 2020/02/03 19:46:59 (permalink)
0
thx a lot Mr.1and0
 
It would be great!
#5
ric
Super Member
  • Total Posts : 25632
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SPI parity check 2020/02/03 20:48:12 (permalink)
+1 (1)
Did you try it yet?
 

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!
#6
ric
Super Member
  • Total Posts : 25632
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SPI parity check 2020/02/10 15:12:43 (permalink)
+1 (1)
Still no update?
 

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!
#7
parkcoding
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: SPI parity check 2020/02/13 16:14:34 (permalink)
0
Hi~ Mr.ric
 
Finally I did it 
 
Here is my code
 

 
char NCV78763_Parity_Check(unsigned int Parity)
{
    unsigned int Parity_Standard = 0b0000000000000001;
    unsigned int Parity_Buffer = Parity;
    char Parity_Count = 15;
    char Parity_Result = Parity_Buffer & Parity_Standard;
 
    while (Parity_Count-- != 0)
    {
        if (((Parity_Buffer >>= 1) & Parity_Standard) != 0)
        {
            Parity_Result++;
        }
    }
     if((Parity_Result & Parity_Standard) != 0)
    {
          return 0;
     }
          else
    {
          return 1;
     }
}
 

 
thx!
#8
ric
Super Member
  • Total Posts : 25632
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SPI parity check 2020/02/13 16:24:10 (permalink)
0
Where did that code come from?
I guess it will work, but it's way less efficient than the examples you were given above.
 

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!
#9
parkcoding
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: SPI parity check 2020/02/13 16:59:50 (permalink)
0
I made it myself referring to Datasheet.
 
In datasheet
 
Parity = not(CMD xor A3 xor.... xor A0 xor D9 xor.... xor D0)
 
it was written in Datasheet
 
and
 
I made code at first like this
 

 
char NCV78763_Parity_Check(unsigned int Parity)
{
    unsigned int Parity_Buffer = Parity;
    char Parity_Result = 0;

    Parity_Result = Parity_Buffer ^ (Parity_Buffer << 1); /*CMD and A3*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 2); /*A2*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 3); /*A1*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 4); /*A0*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 6); /*D9*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 7); /*D8*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 8); /*D7*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 9); /*D6*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 10); /*D5*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 11); /*D4*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 12); /*D3*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 13); /*D2*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 14); /*D1*/
    Parity_Result = Parity_Result ^ (Parity_Buffer << 15); /*D0*/

    Parity_Result = ~Parity_Result;
    Parity_Result = Parity_Result >> 15;

    return Parity_Result;
}

 
and I applied it to those upper code
 
:)
#10
ric
Super Member
  • Total Posts : 25632
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SPI parity check 2020/02/13 18:12:09 (permalink) ☄ Helpfulby parkcoding 2020/02/16 21:19:52
+3 (3)
You will learn a bit more about how parity works if you study the other suggestions, and understand how they work.
You can do it one bit at a time, but there are more efficient ways to do it, as demonstrated above.
 

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!
#11
1and0
Access is Denied
  • Total Posts : 10356
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: SPI parity check 2020/02/14 08:34:10 (permalink) ☄ Helpfulby parkcoding 2020/02/16 21:19:56
+2 (2)
parkcoding
It would be great!

I guess it was not all that great, as you've clearly chosen the inefficient route.  ;)
 
#12
Jump to:
© 2020 APG vNext Commercial Version 4.5