Fig. 2: Equation Of TWI Clock Frequency to initialize the Master in AVR
void TWI_init_master(void) // Function to initialize master
{
TWBR=0x01; // Bit rate
TWSR=(0<<TWPS1)|(0<<TWPS0); // Setting prescalar bits
// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
}
void TWI_start(void)
{
// Clear TWI interrupt flag, Put start condition on SDA, Enable TWI
TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT))); // Wait till start condition is transmitted
while((TWSR & 0xF8)!= 0x08); // Check for the acknowledgement
}
void TWI_read_address(unsigned char data)
{
TWDR=data; // Address and read instruction
TWCR=(1<<TWINT)|(1<<TWEN); // Clear TWI interrupt flag,Enable TWI
while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte received
while((TWSR & 0xF8)!= 0x40); // Check for the acknoledgement
}
void TWI_write_data(unsigned char data)
{
TWDR=data; // put data in TWDR
TWCR=(1<<TWINT)|(1<<TWEN); // Clear TWI interrupt flag,Enable TWI
while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
while((TWSR & 0xF8) != 0x28); // Check for the acknoledgement
}
Fig.5: TWI STOP Condition Bit
void TWI_stop(void)
{
// Clear TWI interrupt flag, Put stop condition on SDA, Enable TWI
TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
while(!(TWCR & (1<<TWSTO))); // Wait till stop condition is transmitted
}
Fig. 6: Flow Chart of MASTER as Transmitter in TWI Interfacing using AVR
void TWI_read_address(unsigned char data)
{
TWDR=data; // Address and read instruction
TWCR=(1<<TWINT)|(1<<TWEN); // Clear TWI interrupt flag,Enable TWI
while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte received
while((TWSR & 0xF8)!= 0x40); // Check for the acknoledgement
}
void TWI_read_data(void)
{
TWCR=(1<<TWINT)|(1<<TWEN); // Clear TWI interrupt flag,Enable TWI
while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
while((TWSR & 0xF8) != 0x58); // Check for the acknoledgement
recv_data=TWDR;
PORTB=recv_data;
}
Fig. 7: Flow Chart of MASTER as Receiver in TWI Interfacing using AVR
void TWI_init_slave(void) // Function to initilaize slave
{
TWAR=0x20; // Fill slave address to TWAR
}
void TWI_match_read_slave(void) //Function to match the slave address and slave dirction bit(read)
{
while((TWSR & 0xF8)!= 0x60) // Loop till correct acknoledgement have been received
{
// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag
}
}
void TWI_read_slave(void)
{
// Clear TWI interrupt flag,Get acknowlegement, Enable TWI
TWCR= (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag
while((TWSR & 0xF8)!=0x80); // Wait for acknowledgement
recv_data=TWDR; // Get value from TWDR
PORTB=recv_data; // send the receive value on PORTB
}
void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write)
{
while((TWSR & 0xF8)!= 0xA8) // Loop till correct acknoledgement have been received
{
// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag
}
}
void TWI_write_slave(void) // Function to write data
{
TWDR= write_data; // Fill TWDR register whith the data to be sent
TWCR= (1<<TWEN)|(1<<TWINT); // Enable TWI, Clear TWI interrupt flag
while((TWSR & 0xF8) != 0xC0); // Wait for the acknowledgement
}
Fig. 9: Slave as Transmitter in TWI Interfacing using AVR
Project Source Code
###
// Program for Master Mode// Check Code2 for Slave Mode Program#include#include#includevoid TWI_start(void);void TWI_repeated_start(void);void TWI_init_master(void);void TWI_write_address(unsigned char);void TWI_read_address(unsigned char);void TWI_write_data(unsigned char);void TWI_read_data(void);void TWI_stop(void);unsigned char address=0x20, read=1, write=0;unsigned char write_data=0x01, recv_data;int main(void){_delay_ms(2000);DDRB=0xff;TWI_init_master(); // Function to initialize TWIwhile(1){if(write_data==0x00)write_data=1;TWI_start(); // Function to send start conditionTWI_write_address(address+write); // Function to write address and data direction bit(write) on SDATWI_write_data(write_data); // Function to write data in slaveTWI_stop(); // Function to send stop condition_delay_ms(10); // Delay of 10 mili secondTWI_start();TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDATWI_read_data(); // Function to read data from slaveTWI_stop();_delay_ms(1000);write_data = write_data * 2;}}void TWI_init_master(void) // Function to initialize master{TWBR=0x01; // Bit rateTWSR=(0<// Setting prescalar bits // SCL freq= F_CPU/(16+2(TWBR).4^TWPS)}void TWI_start(void){// Clear TWI interrupt flag, Put start condition on SDA, Enable TWITWCR= (1< while(!(TWCR & (1<while((TWSR & 0xF8)!= 0x08); // Check for the acknowledgement}void TWI_repeated_start(void){// Clear TWI interrupt flag, Put start condition on SDA, Enable TWITWCR= (1< while(!(TWCR & (1<while((TWSR & 0xF8)!= 0x10); // Check for the acknoledgement}void TWI_write_address(unsigned char data){TWDR=data; // Address and write instructionTWCR=(1<while (!(TWCR & (1<while((TWSR & 0xF8)!= 0x18); // Check for the acknoledgement}void TWI_read_address(unsigned char data){TWDR=data; // Address and read instructionTWCR=(1<while (!(TWCR & (1<while((TWSR & 0xF8)!= 0x40); // Check for the acknoledgement}void TWI_write_data(unsigned char data){TWDR=data; // put data in TWDRTWCR=(1<while (!(TWCR & (1<while((TWSR & 0xF8) != 0x28); // Check for the acknoledgement}void TWI_read_data(void){TWCR=(1<while (!(TWCR & (1<while((TWSR & 0xF8) != 0x58); // Check for the acknoledgementrecv_data=TWDR;PORTB=recv_data;}void TWI_stop(void){// Clear TWI interrupt flag, Put stop condition on SDA, Enable TWITWCR= (1< while(!(TWCR & (1<}###
Project Source Code
###
// Program for Slave mode#include#includevoid TWI_init_slave(void);void TWI_match_read_slave(void);void TWI_read_slave(void);void TWI_match_write_slave(void);void TWI_write_slave(void);unsigned char write_data,recv_data;int main(void){DDRB=0xff;TWI_init_slave(); // Function to initilaize slavewhile(1){TWI_match_read_slave(); //Function to match the slave address and slave dirction bit(read)TWI_read_slave(); // Function to read datawrite_data=~recv_data; // Togglem the receive dataTWI_match_write_slave(); //Function to match the slave address and slave dirction bit(write)TWI_write_slave(); // Function to write data}}void TWI_init_slave(void) // Function to initilaize slave{TWAR=0x20; // Fill slave address to TWAR}void TWI_write_slave(void) // Function to write data{TWDR= write_data; // Fill TWDR register whith the data to be sentTWCR= (1<while((TWSR & 0xF8) != 0xC0); // Wait for the acknowledgement}void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write){while((TWSR & 0xF8)!= 0xA8) // Loop till correct acknoledgement have been received{// Get acknowlegement, Enable TWI, Clear TWI interrupt flagTWCR=(1< while (!(TWCR & (1<}}void TWI_read_slave(void){// Clear TWI interrupt flag,Get acknowlegement, Enable TWITWCR= (1< while (!(TWCR & (1<// Wait for TWINT flag while((TWSR & 0xF8)!=0x80); // Wait for acknowledgementrecv_data=TWDR; // Get value from TWDRPORTB=recv_data; // send the receive value on PORTB}void TWI_match_read_slave(void) //Function to match the slave address and slave dirction bit(read){while((TWSR & 0xF8)!= 0x60) // Loop till correct acknoledgement have been received{// Get acknowlegement, Enable TWI, Clear TWI interrupt flagTWCR=(1< while (!(TWCR & (1<}}###
Circuit Diagrams
Project Components
Project Video
Filed Under: AVR, AVR., Electronic Projects
Filed Under: AVR, AVR., Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.