tyassin
Advanced Member level 1
Hello,
I have the code shown below which will receive one byte from a slave AVR to a master AVR processor.
The for loop call each slave with its address and the slave responds with the "SlaveToMasterByte".
I have tried now for a while and I find this a little difficult.
My question is how do I expand this code so it can receive multiple bytes from each slave?
Thank you
I have the code shown below which will receive one byte from a slave AVR to a master AVR processor.
The for loop call each slave with its address and the slave responds with the "SlaveToMasterByte".
I have tried now for a while and I find this a little difficult.
My question is how do I expand this code so it can receive multiple bytes from each slave?
Thank you
Code:
//----------------------------------------------------Master transmitter-------------------------------------------------------------------
for (Slave = 3; Slave <= 19; Slave += 2){
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //Send start condition
while (!(TWCR & (1<<TWINT)));
TWDR = Slave;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWSRStatus = (TWSR & 0xF8);
TWCR = (1<<TWINT) | (1<<TWEN);
if (TWSRStatus == 0x38){
MasterArbitrationLost();
}
else if (TWSRStatus == 0x40){
while (!(TWCR & (1<<TWINT)));
TWSRStatus = (TWSR & 0xF8);
SlaveToMasterByte = TWDR;
TWCR = (1<<TWINT) | (1<<TWEN);
if (TWSRStatus == 0x58)
{
//Place code here
}
else if(TWSRStatus == 0x48)
{
//Place code here
}
}
}
//-----------------------------------------------Slave receiver----------------------------------------------------------------------------
else {
TWCR = (1<<TWEA) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
SlaveToMasterByte = 255;
TWDR = SlaveToMasterByte;
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}