Thank you for taking the time to look at my problem.
OK so added below are four functions. The first two are the original that only return one variable and work correctly. The second two functions are wrongly modified to return multiple variables.
The problem I have is spi_transfer_byte() can only return one variable not multiple and secondly get_raw_results() can only receive one data type from spi_transfer_byte().
I can now understand the logic of using a structure because all the results at the bottom of spi_transfer_byte() can be uploaded to the structure so this gets over the one return type, and get_raw_results() can access multiple data in the structure - I think I understand this, but when the spi_transfer_byte() is finished does the structure lose its data and go out of scope?. My problem now is where do I put this structure. Because both of the below functions are in different .c files. Would I create a third .c file to add my structure.
If all of the above is correct, would you use pointers to access the structures.
Thank you in advance for your help.
Code:
// spi.c
// original code
byte spi_transfer_byte(byte read_data){
byte byte_in = 0;
byte bit = 0;
byte result = 0;
for (bit = 0x80; bit; bit >>= 1) {
if(read_data & bit){
PORTA |= BV(SPI_MOSI); // HIGH
}
else{
PORTA &= ~BV(SPI_MOSI); // LOW
}
_delay_us(4);
PORTE |= BV(SPI_CLK); // CLK_High
if (PINA & (1<<SPI_MISO))
byte_in |= bit;
_delay_us(4);
PORTE &= ~BV(SPI_CLK); // CLK_low
}
return byte_in;
}
// raw_data.c
// original code
void get_raw_results(long base_address, int channel_number, unsigned char results[4]) {
long address = base_address+4*(channel_number-1);
initialize_memory_read(address);
results[3]=spi_transfer_byte(0x00); // fault data
results[2]=spi_transfer_byte(0x00); // MSB result byte
results[1]=spi_transfer_byte(0x00); // 2nd result byte
results[0]=spi_transfer_byte(0x00); // LSB result byte
finish_memory_read();
}
Code:
// spi.c
// modified code
byte spi_transfer_byte(byte read_data){
byte bit = 0;
byte byte_in_1, byte_in_2,
byte_in_3, byte_in_4,
byte_in_5, byte_in_6,
byte_in_7 = 0;
for (bit = 0x80; bit; bit >>= 1) {
if(read_data & bit){
PORTA |= BV(SPI_MOSI); // HIGH
}
else{
PORTA &= ~BV(SPI_MOSI); // LOW
}
_delay_us(2);
PORTE |= BV(SPI_CLK); // CLK_High
if (PINA & (1<<SPI_MISO_PT100_SP11))
byte_in_1 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP12))
byte_in_2 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP13))
byte_in_3 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP14))
byte_in_4 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP15))
byte_in_5 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP16))
byte_in_6 |= bit;
if (PINA & (1<<SPI_MISO_PT100_SP17))
byte_in_7 |= bit;
_delay_us(2);
PORTE &= ~BV(SPI_CLK); // CLK_low
}
return byte_in_1;
return byte_in_2;
return byte_in_3;
return byte_in_4;
return byte_in_5;
return byte_in_6;
return byte_in_7;
}
// raw_data.c
// modified code
void get_raw_results(long base_address, int channel_number, unsigned char results[4]) {
long address = base_address+4*(channel_number-1);
initialize_memory_read(address);
results[3]=spi_transfer_byte(0x00); // fault data
results[2]=spi_transfer_byte(0x00); // MSB result byte
results[1]=spi_transfer_byte(0x00); // 2nd result byte
results[0]=spi_transfer_byte(0x00); // LSB result byte
/*
results[3]=spi_transfer_byte(0x00); // fault data
results[2]=spi_transfer_byte(0x00); // MSB result byte
results[1]=spi_transfer_byte(0x00); // 2nd result byte
results[0]=spi_transfer_byte(0x00); // LSB result byte
results[3]=spi_transfer_byte(0x00); // fault data
results[2]=spi_transfer_byte(0x00); // MSB result byte
results[1]=spi_transfer_byte(0x00); // 2nd result byte
results[0]=spi_transfer_byte(0x00); // LSB result byte
// etc
// etc
*/
finish_memory_read();
}