How to send several bytes using AVR I2C TWI module

Status
Not open for further replies.

tyassin

Advanced Member level 1
Joined
Jul 22, 2004
Messages
467
Helped
86
Reputation
170
Reaction score
41
Trophy points
1,308
Activity points
3,814
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

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);
    }
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…