Bits and variables

Hi all.
I want to create a custom library for the PS/2 interface (no, i do NOT want to use one that is already made), but i've faced a problem: How do i put bits into a variable? Like, i receive 101010101 (last bit is partity) and i want to put the first 8 in a variable, but i receive them in series, so i cannot do that like if it was parallel. Any ideas, other than creating variables for each bit?
C code is welcome.
Best Regards,
Moondeck

Look at the bitWrite() “function”.

Thanks, i am working on understanding how it works, any other ideas?

any other ideas?

If you can’t understand simple bit manipulation, or use a function that has already been written, then this project is NOT for you.

So just because i dont understand it, it makes a reason to stop learning things? Maybe i dont understand it because English is not my native language?

PaulS:
If you can’t understand simple bit manipulation, or use a function that has already been written, then this project is NOT for you.

Regardless, Im guna help

assuming you have a way of reading a single bit rather than an entire byte…

byte result = 0; //last bit will be discarded
for(byte i = 0; i < 9; i++) //9 bits, 1 parity
{
  result <<= 1; //shift bits to the left to make room for new bit
  result += readBit(); //or however you read your bits, must be a 1 or a 0
}

Viola…

If you dont understand this, then you should stop and learn more about tutorials
Also look up “bitwise operation”. Some common ones are OR, AND, XOR, NOT

moondeck:
Like, i receive 101010101 (last bit is partity) and i want to put the first 8 in a variable,

How does that bit pattern get into your Arduino and what format is it?
For example is it a series of bytes with 1s and 0s?

...R

Robin2:
How does that bit pattern get into your Arduino and what format is it?
For example is it a series of bytes with 1s and 0s?

...R

I read it on rising edge of the clock signal for the keyboard. I read it bit after bit.

Some prefer to shift, I prefer what I call “to walk the bits”.

Start with a byte of 8 bits.
Shift it and set bit number 0 (the bit on the right) or 7 (the bit on the left).
Or walk the bits and set them with bitWrite as PaulS wrote.

http://arduino.cc/en/Reference/bitWrite

byte inbyte;

for( i=0; i<=7; i++)   // walk the bits
{
  int databit = .... // read the input according to the clock, perhaps interrupt ?
  bitWrite( inbyte, i, databit);
}

Ps991 in reply #5 gave the shifting example, this is the bit walking example. Try to make one yourself, and show it to us. We guide from there.

How fast is the signal ? Is an interrupt needed for the clock ? Perhaps the databit can be read in the interrupt that is generated by the clock interrupt.