//
// SPI_write

void SPI_write (byte registerByte, byte commandByte) {

        digitalWrite(CSB, LOW); //turn on device

        // 6 bits register address
        for (int i=5; i>=0; i--){

                // write bit
                if (int(1<<i & registerByte) == 0) digitalWrite(DATAOUT, LOW); else digitalWrite(DATAOUT, HIGH);
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }

        digitalWrite(DATAOUT, HIGH); // Indicate that we are Writing
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);
        digitalWrite(DATAOUT, LOW); // LOW
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);


        // write 8 bits register content
        for (int i=7; i>=0; i--){

                // write bit
                if (int(1<<i & commandByte) == 0) digitalWrite(DATAOUT, LOW); else digitalWrite(DATAOUT, HIGH);
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }


        digitalWrite(CSB, HIGH); //turn off device

}



//
// SPI_read8

int SPI_read8 (byte registerByte) {

        digitalWrite(CSB, LOW); //turn on device

        // 6 bits register address
        for (int i=5; i>=0; i--){

                // write bit
                if (int(1<<i & registerByte) == 0) digitalWrite(DATAOUT, LOW); else digitalWrite(DATAOUT, HIGH);
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }

        digitalWrite(DATAOUT, LOW); // Indicate that we are Reading
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);

        // read 8 bits register content
        int returnValue = 0;
        for (int i=7; i>=0; i--){

                // read bit
                returnValue += digitalRead (DATAIN) << i;
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }


        digitalWrite(CSB, HIGH); //turn off device

        return returnValue;

}



//
// SPI_read16

int SPI_read16 (byte registerByte) {

        digitalWrite(CSB, LOW); //turn on device

        // 6 bits register address
        for (int i=5; i>=0; i--){

                // write bit
                if (int(1<<i & registerByte) == 0) digitalWrite(DATAOUT, LOW); else digitalWrite(DATAOUT, HIGH);
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }

        digitalWrite(DATAOUT, LOW); // Indicate that we are Reading
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);
        digitalWrite(SCKCLOCK, HIGH);
        digitalWrite(SCKCLOCK, LOW);


        // read 8 bits register content
        int returnValue = 0;
        for (int i=15; i>=0; i--){

                // read bit
                returnValue += digitalRead (DATAIN) << i;
                // cycle clock
                digitalWrite(SCKCLOCK, HIGH);
                digitalWrite(SCKCLOCK, LOW);

        }

        digitalWrite(CSB, HIGH); //turn off device

        return returnValue;

}

This website has been archived and is no longer maintained.