I2C using AVR:
Program for AVR(MOSI):
Master:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
while(1)
{
TWBR=0xFF;
TWCR=(1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x01;
TWDR=0x40;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x18)
PORTD=0x02;
TWDR=0x55;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x28)
PORTD=0x03;
while(1);
}
}
Slave:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
TWAR=0x40;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while(TWCR&(1<<TWINT)==0);
if(TWSR&0xF8!=0x60)
PORTD=0x04;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while(TWCR&(1<<TWINT)==0);
PORTB=TWDR;
}
}
Simulation for MOSI Program:
Program for AVR(MISO):
Master:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWBR=0xFF;
TWCR=((1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x04;
TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
TWDR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x40)
PORTD=0x05;
TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
while((TWCR&(1<<TWINT))==0);
PORTB=TWDR;
if((TWSR&0xF8)!=0x50)
PORTD=0x06;
TWCR=((1<<TWSTO)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
}
}
Slave:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWAR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xA8)
PORTD=0x07;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWDR=0X44;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xB8)
PORTD=0x08;
}
}
Program for AVR(MOSI):
Master:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
while(1)
{
TWBR=0xFF;
TWCR=(1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x01;
TWDR=0x40;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x18)
PORTD=0x02;
TWDR=0x55;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x28)
PORTD=0x03;
while(1);
}
}
Slave:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
TWAR=0x40;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while(TWCR&(1<<TWINT)==0);
if(TWSR&0xF8!=0x60)
PORTD=0x04;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while(TWCR&(1<<TWINT)==0);
PORTB=TWDR;
}
}
Simulation for MOSI Program:
Program for AVR(MISO):
Master:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWBR=0xFF;
TWCR=((1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x04;
TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
TWDR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x40)
PORTD=0x05;
TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
while((TWCR&(1<<TWINT))==0);
PORTB=TWDR;
if((TWSR&0xF8)!=0x50)
PORTD=0x06;
TWCR=((1<<TWSTO)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
}
}
Slave:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWAR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xA8)
PORTD=0x07;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWDR=0X44;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xB8)
PORTD=0x08;
}
}
0 comments:
Post a Comment
if you have any doubt please let me know