Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

making i2c connection

Status
Not open for further replies.

bhanupriya

Newbie level 6
Newbie level 6
Joined
Dec 25, 2011
Messages
13
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,361
Can any one help me to connect two atmega8 MCs using i2c?....Actualy I'm not familiar wid dis i2c & AVR....So plz help me if u knw......
 

Can any one help me to connect two atmega8 MCs using i2c?....Actualy I'm not familiar wid dis i2c & AVR....So plz help me if u knw......

It is not a difficult connection to make. All you have to do is connect the SDA and SCL pins of both MCUs with each other. That is SDA goes to SDA and SCL goes to SCL. In addition you need two pull up resistors on those lines.




Finally check the following post out for some usefull references.
https://www.edaboard.com/threads/233183/#post995252

Hope that helped.
 
But, how to upload the program to atmega8 in linux??....(we are using parallel port for de connection.....)
 

bhanupriya said:
But, how to upload the program to atmega8 in linux??....(we are using parallel port for de connection.....)
Sorry I don't use Linux. :-(
But in any case it would be helpful for someone to know which compiler and programming device you are using.
Cheers!
 
How leds can be connected for the communication b/t avrs in dis fig....**broken link removed** v need transducers for de communication??...if u knw plz help....
Sorry, but the provided link gives me an error and I cannot open it.
 

The simplest method is to delegate one AVR the master and the other the slave device.

Here is an example project implementing an I2C bus between two ATMEGA32 using their TWI interface:





You can modify the example project to include additional sensors as needed.

BigDog
 
I believe there are more optimum values than 10K for the pull up. Like let's say 2K2 (it depends on the supply voltage also). For very short distances and slow communication speed you may get away with this value of 10K. Otherwise it will be difficult to have an operating TWI interface.

page 170:
The number of devices that can be connected to the bus is only limited by the bus
capacitance limit of 400 pF and the 7-bit slave address space. A detailed specification of
the electrical characteristics of the TWI is given in “Two-wire Serial Interface Character-
istics” on page 290. Two different sets of specifications are presented there, one
relevant for bus speeds below 100 kHz, and one valid for bus speeds up to 400 kHz.

Also at page 290 it is written that for speed<=100KHz, Rp=1000ns/Cb=(10^-6)/(400*10^-12)=2K5 (but this value is for bus capacitance 400pF). For greater bus speed, this ohmic value drops to 30% according to the same table.
**broken link removed**

You could have this in mind in case you observe with the oscilloscope misquoted pulses on the TWI lines.

Alexandros
 
https://obrazki.elektroda.pl/84_1325215544.jpg can u snd de c code(master & slave) for dis ckt?..

It is available at the link I provided, under the "Code" and "Code2" tab.

However, here it is for your convenience:


Master Mode TWI
Code:
// Program for Master Mode
// Check Code2 for Slave Mode Program
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
 
void 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=1;
unsigned char write_data=0x01, recv_data;
 
int main(void)
{
_delay_ms(2000);
DDRB=0xff;
TWI_init_master(); // Function to initialize TWI
while(1)
{
if(write_data==0x00)
write_data=1;
TWI_start(); // Function to send start condition
TWI_write_address(address+write); // Function to write address and data direction bit(write) on SDA
TWI_write_data(write_data);     // Function to write data in slave
TWI_stop(); // Function to send stop condition
 
 
_delay_ms(10); // Delay of 10 mili second
 
TWI_start();
TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDA
TWI_read_data(); // Function to read data from slave
TWI_stop();
_delay_ms(1000);
write_data = write_data * 2;
}
 
 
}
 
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_repeated_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 restart condition is transmitted
while((TWSR & 0xF8)!= 0x10); // Check for the acknoledgement
}
 
void TWI_write_address(unsigned char data)
{
TWDR=data; // Address and write instruction
TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
while((TWSR & 0xF8)!= 0x18);  // Check for the acknoledgement
}
 
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
}
 
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;
}
  
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
}


Slave Mode TWI
Code:
// Program for Slave mode
#include<avr/io.h>
#include<util/delay.h>
 
void 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 slave
while(1)
{
TWI_match_read_slave(); //Function to match the slave address and slave dirction bit(read) 
TWI_read_slave(); // Function to read data
write_data=~recv_data; // Togglem the receive data
TWI_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 sent 
TWCR= (1<<TWEN)|(1<<TWINT);   // Enable TWI, Clear TWI interrupt flag 
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 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_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
}
}

Look under the "Description" tab for a detailed explanation of the code and techniques:



BigDog
 
Oh sorry...I found that code...
But can we use 2 ATMEGA8s instead of ATMEGA32s?...If can, what change we need to implement in the code?...
 

...yaa..I tried it with ATMEGA8.But, not working...:sad:
 

Do anyone have de code for ATMEGA8 bootloader?..
 

we didn't find de bootloader(ATMEGA8) source code in de provided reference.....Actually we need de code.....plz help .:sad:
 

Solved the problem of bootloader......... https://obrazki.elektroda.pl/84_1325215544.jpg We tried the same ckt with ATMEGA8.....we have connected only one led to both master and slave and also hav given some modifications to the ckt according to the pin configuration of atmega8.....But only one led(on the master side) is blinking....
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top