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.

General Doubts in I2C bus

Status
Not open for further replies.

gopintj

Member level 4
Joined
Sep 1, 2010
Messages
77
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Location
Thirukalukalukundra, Kanchipuram, Tamilnadu, India
Activity points
1,953
I would like to transmit and receive data between two 16f877a in which one as Master and One as slave.
In this, i have two doubts.

1. Can i connect the SDA and SCL pins of master directly to SDA and SCL pins of Slave without using an I2C expander?

2. If i connect SDA and SCL of master and slave, i need to give address of slave. How can i give a address to 16f877a?
Please guide me.
 

Check the datasheet for the PIC16F877A page 82
This is a part quoted from the datasheet:
SSPADD register holds the slave device address
when the SSP is configured in I2C Slave mode. When
the SSP is configured in Master mode, the lower
seven bits of SSPADD act as the baud rate generator
reload value.

Also, you can connect the two wires of the master directly to those of the slave. You may need to use external pull-up resistors if you are not using the Master's built-in pull-ups.
 
Hi,

Why do not you keep things even simpler and use only 2 i/o lines by using the Usart serial mode.
Just connect the 2 pairs RX /TX lines using a 1k resistor in series with each line to prevent accidental shorts if you connect the wrong way round.
 

@wp100, I would like to learn the processes of I2C. That is the only reason for my involvement in I2C communication.

well, now i understood the uses of SSPADD.
Still i am getting some errors in reading data from slave. i use this following code to read data from slave. Please tell me any mistakes that i do.


while(1)
{
start(); //start of frame
i2c_write(0x70); //Address of the slave
DelayUs(100);
var1=i2c_read(); //data from the slave
DelayUs(100);
lcd_putc(var1);
DelayMs(10);
stop(); //stop of frame
}
}
Please suggest me any idea.
 

You need to have another look at the I2C protocol. It seems you are not checking for ACK's and not issuing a stop or repeat start condition after you write above.

An I2C Tutorial.

lots of source already in this forum also.
 

Joeyla, Thanks for your valuable link. I went through your link and wrote a program of transmission of data from master to slave. I got some output waveform via proteus 7.6.
I transmitted a character 0x55 to slave. In the slave, i loaded that 0x55 to a character named x. Now, i loaded that x into my lcd. Here is the problem. My LCD displays a strange character ')' instead of '0x55'. Is there any problem.?

This is the image file



And here is the code of Slave:
while(1)
{
DelayMs(10);
x=i2c_read();
DelayMs(10);
lcd_putc(x);
}

unsigned char i2c_read(void)
{
unsigned char rx;
DelayUs(10);
rx=SSPBUF;
return rx;
}

Please help me.
 

Your code doesn't implement the right way to operate an I2C slave. You need to poll SSPSTAT BF and DA bits, read SSPBUF when BF is set, ignore address bytes and transfer data bytes to the display.
 

could you please explain me with a sample code?
I really got confused of doing this. :-(

Also, this is the value of my SSPADD in master and the slave.
MASTER : SSPADD=(20000/((4*100)+1));
SLAVE : SSPADD=0x70;

Also, tell me when will my slave generate an ACK if i transmit the slave's address byte in SSP?

i2c_write(0x70); // Transmits Address bit of the slave.
i2C_write(0x55); // Transmits Data to the slave if the address matches.

Can you please guide me. I am very new to this protocol.
 
Last edited:

This is my i2c program to send one data to slave. i use pcf8574A an i2c expander. Still i am not getting any acknowledgement from slave, even though the address byte mentioned in the program and the address of the PCF8574A are same. :-(

#include<htc.h>
#include"delay.c"
#include"lcd_driver.c"
__PROG_CONFIG(1,0x0200);
__PROG_CONFIG(2,0x000F);
__PROG_CONFIG(3,0x0003);
__PROG_CONFIG(4,0x0005);
void setup_i2c(void); //function proto types
void i2c_master(void);
void start(void);
void stop(void);
void wait(void);
void restart(void);
void nack(void);
void ack(void);
unsigned char i2c_write(unsigned char);
unsigned char i2c_read(void);

int i=0;
void main()
{
setup_lcd_port();
lcd_init();
lcd_puts("I2C COMMUNICATION");
lcd_clrscr();
setup_i2c(); //i2c port setup
DelayMs(1);
i2c_master(); //i2c as a master device
DelayMs(1);
while(1)
{
start();

i2c_write(0x71);

if(ACKSTAT==1)
{
lcd_puts("ACK NOT RECEIVED");
lcd_goto_pos(1);
stop();
}
else
{
while(!SSPIF)
continue;
SSPIF=0;
lcd_puts("ACK WAS RECEIVED");
i2c_write(0x55);
DelayMs(100);
i2c_write(0xAA);
DelayMs(100);
stop();
}


}

}


void setup_i2c(){
TRISC3=TRISC4=1; //use RC3=SCL,RC4=SDA
}

void i2c_master(void){
SSPCON1=0x28; //wcol=0,sspov=0,sspen=1,ckp=1,ssp mode=1000.
SSPCON2=0x50;
SSPSTAT=0xC0; //smp=1 Input data sampled at end of data output time
SSPADD=(20000/((4*100)+1)); //
}

void wait(void){
while(!SSPIF)
continue;
SSPIF=0;
}

void start(void){
SEN=1;
wait();
}

void stop(void){
PEN=1;
while(!SSPIF)
continue;
SSPIF=0;
}


unsigned char i2c_write(unsigned char wbyte){
WCOL=0;
SSPOV=0;
SSPBUF=wbyte;
wait();
return SSPBUF;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top