Thanks Dag, you've light me ! regards, Vasile On Fri, 1 Feb 2002, Dag Bakken wrote: > //read byte from SPI (mode 3) as a master > BYTE rdSPI(void) > { > register BYTE BitCnt = 8; // Do 8 times > register BYTE Shift; // Input data register > do // Top of loop > { > SPI_SCK_LO(); // SPI clock output low > Shift <<= 1; // C left shift one bit > if(IS_SPI_SDI_LO()) // Check level of data input > Shift &= ~1; // Set bit zero to zero > else > Shift |= 1; // Set bit zero to one > SPI_SCK_HI(); // SPI clock output high > } while (--BitCnt); // Do all 8 bits > return(Shift); // Return the input value > } > > The &= operator is a bitwise and > The |= operator is a bitwise or > The ~ operator is a bitwise negate > > The last one will work differently on different variables depending on > the bit-count. In this example (8 bit), the expression ~1 is equal to > 254. > The code in itself looks like it's converted asm-code with the shift > and test performed. A smoother way would be like this: > > //read byte from SPI (mode 3) as a master > BYTE rdSPI(void) > { > register BYTE BitCnt = 8; // Do 8 times > register BYTE Shift; // Input data register > do // Top of loop > { > SPI_SCK_LO(); // SPI clock output low > Shift <<= 1; // C left shift one bit > Shift.0=SPI_SDI_LEVEL(); // Set data > SPI_SCK_HI(); // SPI clock output high > } while (--BitCnt); // Do all 8 bits > return(Shift); // Return the input value > } > > This is of course dependent on the compiler, if it supports the > bit-addressing withing registers. > > Dag S > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads