Problem with I2C code and a slave mode

Status
Not open for further replies.

vasiliugeo

Newbie level 4
Joined
May 15, 2008
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,390
I did a communication between a dspic 33 fj256gp710 as a master (on a explorer 16 board) and a pic 16f887 as a slave (on a pickit 2 board). When I'm runnig the programs(both master and slave) in animate mode it is ok for the first cycle, in next cycle thoose does not syncronize I think. In normal running mode the same thing, they do not syncronize not even for the first cycle. I will post my code here. Please help me, this is for my diploma project. My question is: Should I configure some how the oscilator in the slave?

Slave code:
// citim de la potentiometru o valoare o convertim in D si o salvam in memorie
#include <htc.h>
#include <pic.h>
#include <pic16f887.h>

__CONFIG (EXTIO & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROTECT & DEBUGEN & UNPROTECT);

#define pDirSCL TRISC3
#define pDirSDA TRISC4

#define pSCL RC3
#define pSDA RC4


#define pa0 RB0

int i,j;

unsigned char *p;

void InitI2CSlave(void);
void WriteI2C(char data);
char ReadI2C(void);
void initA2D(void);


unsigned char addr = 0x5A,value = 0x75,a;
unsigned char temp;
int main()
{


InitI2CSlave();
initA2D();
TRISD = 0x00;
PORTD = 0x00;

TRISA = 0xFF;
ADCON1 = 0x00;
ANSEL = 0xFF;
ADCON0 = 0x41;



while (1)
{

GODONE=1;
// while(!GODONE);


temp =SSPSTAT;
temp = temp & 0x2D; //mascam bitii neimportanti si depunem aceasta val in temp


switch (temp)
{
case 0x09://(Write operation, last byte was an address, buffer is full)
while(SSPOV)
{
SSPOV = 0;
} //no overflow
SSPOV = 0;
ReadI2C();
RD0 = 1;
break;
case 0x29://(Master Write, Last Byte was Data)
while(SSPOV)
{
SSPOV = 0;
} //no overflow
ReadI2C();
RD2 = 1;
break;
case 0x0C://(State 3: Master Read, Last Byte was an Address)
CKP = 0; //Holds clock low (clock stretch).
WriteI2C(ADRESH);
RD4 =1;
break;
case 0x2C://State 4: Master Read, Last Byte was Data
CKP = 0;//Holds clock low (clock stretch).
WriteI2C(ADRESH);
RD6 =1;
break;
case 0x28://State 5: Master NACK
RD7 = 1;
break;
}


}
}
void initA2D(void)
{
TRISA = 0xFF;
ADCON1 = 0x00;
ANSEL = 0xFF;
ADCON0 = 0x41;
}

void InitI2CSlave(void)
{
//TRISB0 = 1;
TRISC3=1; // set SCL and SDA pins as inputs
TRISC4=1;

pSCL = 1;
pSDA = 1;


SSPADD = 0x02; //slave address
//Enable clock
//I2C Slave mode, 7-bit address
SSPCON = 0x36;
//Enable SSP module

SSPIE = 1; //Enable SPI interrups

//Global interrupt enable
PEIE = 1;
GIE = 1;
SSPBUF = 0x00;

}

void WriteI2C(unsigned char data)
{
do
{
WCOL = 0;
SSPBUF = data;
} while(WCOL);

// Release the clock.

CKP = 1;
}

char ReadI2C(void)
{
return(SSPBUF);
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…