Everything is working with this code all 16 bits are being output on the serial, I need to know how I can collect those bits and convert them to a decimal number.
int SS1 = 10; // set slave select pin
int CLK = 13; // set clock pin
int MOUT = 11; // set master out, slave in pin
int val = 0;
byte rcl = B10000101; // Recall Eeprom data
byte wren = B10000100; // Write Enable Latch
byte add = B11100111; // READ
/* SETUP */
void setup() {
// setup function begins here
Serial.begin(9600);
Serial.println("Start");
pinMode(SS1, OUTPUT); // set CS pin to output
pinMode(CLK, OUTPUT); // set SCK pin to output
pinMode(MOUT, OUTPUT); // set MOSI pin to output
digitalWrite(SS1, LOW); // hold slave select 1 pin LoW, so that chip is not selected to begin with
}
//////////////////////////////////////////////////////////////////////////////////////////
void spi_transfer(byte working) {
// function to actually bit shift the data byte out
for(int i = 1; i <= 8; i++) { // setup a loop of 8 iterations, one for each bit
if (working > 127) { // test the most significant bit
digitalWrite (MOUT,HIGH); // if it is a 1 (ie. B1XXXXXXX), set the master out pin high
}
else {
digitalWrite (MOUT, LOW); // if it is not 1 (ie. B0XXXXXXX), set the master out pin low
}
digitalWrite (CLK,HIGH); // set clock high, the eeprom will read the bit into its register
working = working << 1;
digitalWrite(CLK,LOW); // set clock low, the pot IC will stop reading and prepare for the next iteration (next significant bit
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void spi_read(byte working) {
; // function to actually bit shift the data byte out
for(int i = 1; i <= 7; i++) { // setup a loop of 7 iterations, one for each bit
if (working > 127) { // test the most significant bit
digitalWrite (MOUT,HIGH); // if it is a 1 (ie. B1XXXXXXX), set the master out pin high
}
else {
digitalWrite (MOUT, LOW); // if it is not 1 (ie. B0XXXXXXX), set the master out pin low
}
digitalWrite (CLK,HIGH); // set clock high, the pot IC will read the bit into its register
working = working << 1;
digitalWrite(CLK,LOW); // set clock low, the pot IC will stop reading and prepare for the next iteration (next significant bit
}
digitalWrite (CLK,HIGH);
pinMode(MOUT, INPUT); // set MOSI pin to Input
digitalWrite(CLK,LOW);
val = digitalRead(MOUT); // Reading the first bit
Serial.println (val);
for(int i = 1; i <= 15; i++) { // reading the other 15 bits
digitalWrite (CLK,HIGH); //brings the next byte
val = digitalRead(MOUT); // read the byte
Serial.println (val); // Print the byte = HELP HERE PLEASE !!!!!
digitalWrite (CLK,LOW);
}
pinMode(MOUT, OUTPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void spi_out(int SS, byte cmd_byte) { // SPI tranfer out function begins here
digitalWrite (SS, HIGH); // set slave select high to enable
work = cmd_byte; // let the work byte equal the cmd_byte, defined in the argument in the main loop
spi_transfer(work); // transfer the work byte, which is equal to the cmd_byte, out using spi
digitalWrite(SS, LOW); // set slave select low
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void spi_out1(int SS, byte cmd_byte) { // SPI tranfer out function begins here
digitalWrite (SS, HIGH); // set slave select high
work = cmd_byte; // let the work byte equal the cmd_byte, defined in the argument in the main loop
spi_read(work); // transfer the work byte, which is equal to the cmd_byte, out using spi
digitalWrite(SS, LOW); // set slave select low
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () {
spi_out(SS1, rcl ); // send out data
spi_out(SS1, wren ); // send out data
spi_out1(SS1, add ); // send out data
delay(500000); // delay
}