Andre Abelian wrote: > I got basic spi running now I am having hard time to get odd parity bit > 9-th bit working > here is the code any help will appreciate. Below I extracted the part of the code that seems to create parity. A couple general comments: - Try to use a consistent formatting style. It's not that important how exactly it is, but consistency helps a lot with spotting errors when looking at the code. With formatting style I mean placement of braces, spaces etc. - Try to use helpful variable names. "temp" is actually "parity" here; it doesn't do anything "temp". Now a few specific comments: - Is there a reason why you use i as index in the first for loop and count as index in the second for loop? Seems to me that both loops could use the same index. (But see below regarding i and regarding the two loops.) - When debugging, it sometimes is awkward to use variable names with one letter (for example, it's difficult to place the cursor over them for the editors that show the type and the debuggers that show the type and value). I use e.g. "ii" instead of "i". Just as quick to type, and avoids the one-letter name problems. - Is there a reason you need two for loops? Both loops loop over the same 8 bits. You could keep track of the parity while you are shifting out the bits, in the same loop. - Is there a reason you restrict the parity to 7 bits, even though you seem to send 8 bits? (Your data format seems to be PS2, but I'm not familiar with this protocol.) Usually you would either restrict already what gets sent to seven bits and make sure the 8th bit is in fact 0, or you'd calculate the parity over the full 8 bits. - I'm not quite sure about the data format you want to generate (again, I'm not familiar with PS2). Are you sure you don't need a high/low clock pulse after setting the data output to the parity? - Last but not least (actually, that's now the real issue with the parity :), it seems that the parity generation is kind of odd. You write three times to the first bit of parity: the first time when initializing it to 1, the second time with the xor before the second for loop, the third time with the first loop execution of the second for loop. I didn't follow it through, but it just doesn't "feel right" :) -- and this would be what I suggest you go through and clean up. The xor before the second for loop seems superfluous, and the initialization of parity seems wrong. int parity = 1; // 1=ODD, 0=EVEN <<< 1st time int data_copy, count; data_copy = data; data_copy &= 0x7f; // trim to 7 bit ASCII parity ^= (data_copy & 1); // <<< 2nd time for (count = 0; count < 8; ++count) { parity ^= ((data_copy >> count) & 1); // <<< 3rd time } Gerhard > void ps2_out(BYTE data) > { > BYTE i; > int temp=1,data_copy,count; > data_copy=data; > output_low(PS2_CLK_OUT); // start bit > delay_us(35); > for(i=0;i<8;++i) > { > output_bit(PS2_DATA_OUT, shift_left(&data,1,0)); > OUTPUT_HIGH(PS2_CLK_OUT); // > delay_us(35); > output_low(PS2_CLK_OUT); > delay_us(35); > } > data_copy &= 0x7f; //trim to 7 bit ASCII > temp^=((data_copy) & 1); > for (count = 0; count < 8; ++count) {temp ^= ((data_copy >> count) > & 1);} > if (temp==0){output_high(PS2_DATA_OUT);} // even > if (temp==1){output_low(PS2_DATA_OUT);} // odd > output_high(PS2_CLK_OUT); // stop bit > delay_us(200); > > } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist