ubuntuman
Newbie level 4
Two PIC 18f4620 , 1 as master.2nd is slave I2C problem
The complete Master code . HS oscillator mode .4MHZ
The complete Slave code . HS oscillator mode .4MHZ
The problem is each time I send 0xAA from the master to the slave after putting 0xAA to slave SSPADD, I get NOTACK signal. I don't know where is the thing who doesn't work.
Here you are, screenShot for the WaveForm i get
The complete Master code . HS oscillator mode .4MHZ
Code:
const long int crystalOscillator =4000000;
unsigned short int temp=0;
//#####################################################################################
void I2C_Master_Init(const unsigned long int c)
{
SSPCON1 = 0b00101000;
SSPCON2 = 0;
SSPADD = (crystalOscillator/(4*c))-1;
SSPSTAT = 0b11000000;
TRISC.B3 = 1; //Setting as input as given in datasheet
TRISC.B4 = 1; //Setting as input as given in datasheet
}
void I2C_Master_Wait()
{
while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
}
void I2C_Master_Start()
{
I2C_Master_Wait();
SSPCON2.SEN = 1;
}
void I2C_Master_RepeatedStart()
{
I2C_Master_Wait();
SSPCON2.RSEN = 1;
}
void I2C_Master_Stop()
{
I2C_Master_Wait();
SSPCON2.PEN = 1;
}
void I2C_Master_Write(unsigned short int d)
{
I2C_Master_Wait();
SSPBUF = d;
}
unsigned short int I2C_Master_Read(unsigned short int a)
{
unsigned short int temp;
I2C_Master_Wait();
SSPCON2.RCEN = 1;
I2C_Master_Wait();
temp = SSPBUF;
I2C_Master_Wait();
if(a==1){SSPCON2.ACKDT=1;}
else{SSPCON2.ACKDT=0;}
SSPCON2.ACKEN = 1;
return temp;
}
void main()
{
I2C_Master_Init(100000); //Initialize I2C Master with 100KHz clock
//I2C1_Init(50000);
OSCTUNE.PLLEN=0;
Delay_ms(500);
TRISB=0;
PORTB=0;
while(1)
{
I2C_Master_Start();
I2C_Master_Write(0xAA); //Write data
//I2C_Master_Write(0x00);
I2C_Master_Stop();
Delay_ms(5000);
}
}
The complete Slave code . HS oscillator mode .4MHZ
Code:
//##############################################################################
unsigned short int zed=0;
volatile unsigned short int new =0 ,dataRead=0 , dataWrite=0;
unsigned char dataReadCh[7] , dataWriteCh[7];
//##############################################################################
void interrupt()
{
if(PIR1.SSPIF == 1)
{
SSPCON1.CKP = 0;
new=1;
zed = SSPBUF;
PIR1.SSPIF = 0;
SSPCON1.CKP = 1;
if ((SSPCON1.SSPOV) || (SSPCON1.WCOL))
{
zed = SSPBUF; // Read the previous value to clear the buffer
SSPCON1.SSPOV = 0; // Clear the overflow flag
SSPCON1.WCOL = 0; // Clear the collision bit
SSPCON1.CKP = 1;
}
if(!SSPSTAT.D_A && !SSPSTAT.R_W)
{
while(!SSPSTAT.BF){};
while(SSPSTAT.BF){dataRead = SSPBUF;}
SSPCON1.CKP = 1;
}
else if(SSPSTAT.D_A && SSPSTAT.R_W)
{
while(SSPSTAT.BF){zed = SSPBUF;}
SSPBUF = dataWrite ;
SSPCON1.CKP = 1;
while(SSPSTAT.BF){};
}
PIR1.SSPIF = 0;
}
}
//##############################################################################
void I2C_Slave_Init(unsigned short int address)
{
SSPSTAT = 0b11000000;
SSPADD = address;
SSPCON1 = 0b00110110;
SSPCON2 = 0b00000001;
//TRISC.B3 = 1; //Setting as input as given in datasheet
//TRISC.B4 = 1; //Setting as input as given in datasheet
TRISC=0b00011000;
LATC= 0b00011000;
INTCON = 0b00000000;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIE1.SSPIE = 1;
PIR1.SSPIF = 0;
while(SSPSTAT.BF){zed = SSPBUF;SSPCON1.SSPOV = 0;SSPCON1.WCOL = 0;}
}
//##############################################################################
void main()
{
OSCTUNE.PLLEN=0;
ADCON1=0b00001111;
TRISA.B0 = 0; //PORTD as output
PORTA.B0=0;
I2C_Slave_Init(0xAA); //Initialize as a I2C Slave with address 0x30
while(1)
{
if(new==1)
{
PORTA.B0=1;
new=0;
}
}
}
The problem is each time I send 0xAA from the master to the slave after putting 0xAA to slave SSPADD, I get NOTACK signal. I don't know where is the thing who doesn't work.
Here you are, screenShot for the WaveForm i get
Last edited by a moderator: