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.

I2C master and slave problem (PIC16F877A)

Status
Not open for further replies.

cheehow

Newbie level 3
Joined
Aug 4, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,330
HI, everyone i have met some problem during the interfacing between master and slave connection. i m using PIC C compiler (CCS C compiler) to write the codes. my project is to make the communication between two MCU (PIC16F877A) by using I2C.

the following are the code for master:

#include "C:\Users\Tan Chee How\Desktop\testing I2C\master.h"

#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#USE I2C (MASTER, SDA=Device_SDA, SCL=Device_SCL)

int x;
BYTE Data;

void main()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);

WHILE(TRUE)
{
if(INPUT(PIN_D1))
OUTPUT_HIGH(PIN_B6);
delay_ms(500);
OUTPUT_LOW(PIN_B6);
delay_ms(500);
OUTPUT_HIGH(PIN_B7);
delay_ms(500);
OUTPUT_LOW(PIN_B7);

for(x=0; x<10; x++)
{
i2c_start();
i2c_write(0xA0);
i2c_write(x);
i2c_write(10-x);
i2c_stop();
delay_ms(100);

i2c_start();
i2c_write(0xA1);
Data=i2c_read(0);
i2c_stop();
delay_ms(100);
}
}

}



and the slave codes:

#include "C:\Users\Tan Chee How\Desktop\testing I2C\slave.h"

#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP



BYTE address, buffer[0x10];

void ssp_interupt ()
{
BYTE incoming, state;

state = i2c_isr_state();

if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
buffer[address] = incoming;
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}

void main ()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

while (TRUE) {}
}

therefore, it doesn't work while downloaded into the MCU and test it in hardware. so, can anyone help me see whether which part of my programming having problem or mistake and give me the reply as soon as possible.
my hardware part is jz two MCU connected wif the pin that reserve for I2C and simple power up MCU connection. besides, in order to show the I2C commincation, i added in the LED light for port B and push button for port D1. when i press the push button of master MCU, the slave LED suppose will be lightning up.

please help me to figure out what's the problem .... thx you very much !!
 

cheehow

Newbie level 3
Joined
Aug 4, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,330
please help me ...... cant solve this problem ..... =(

by the way, i have modified the above code .... can somebody help me to detect where is the problem occurred ?? i have no idea ...
the hardware part still remain the same but the master code, i have rearranged the sequence ....

the following are the master codes:

#include "C:\Users\Tan Chee How\Desktop\testing I2C\master.h"

#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#USE I2C (MASTER, SDA=Device_SDA, SCL=Device_SCL)
#USE fast_io(C)
#USE standard_io(B)
#USE fast_io(D)
int x;
BYTE Data;

void main()
{
SET_TRIS_C(0X00);
SET_TRIS_D(0XFF);
OUTPUT_B(0X00);


WHILE(TRUE)
{

for(x=0x00; x<0x0A; x++)
{
i2c_start();
i2c_write(0xA0);
i2c_write(x);
i2c_write(0X0A-x);
i2c_stop();
delay_ms(100);

i2c_start();
i2c_write(0xA1);
Data=i2c_read(0);
i2c_stop();
delay_ms(100);
}

if(INPUT(PIN_D1))
OUTPUT_HIGH(PIN_B6);
delay_ms(500);
OUTPUT_LOW(PIN_B6);
delay_ms(500);
OUTPUT_HIGH(PIN_B7);
delay_ms(500);
OUTPUT_LOW(PIN_B7);

}

}

and the following are the slave code .... it is taken from the example from ccs c compliler (EX_SLAVE.C)

#include "C:\Users\Tan Chee How\Desktop\testing I2C\slave.h"

#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP



BYTE address, buffer[0x10];

void ssp_interupt ()
{
BYTE incoming, state;

state = i2c_isr_state();

if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
buffer[address] = incoming;
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}

void main ()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

while (TRUE) {}
}

please help me ... i have modified many times already .. but it still cannot work .... please give me a solution =(
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top