vasiliugeo
Newbie level 4
pickit2 i2c
Hy to everybody, I'm new here. I'm woerking right now at my diploma project: I want to make a I2C communication between a Explorer 16 board from Microchip which has a dsPic33fj256gp710(I want to be the master one) and a pickit2 which has a pic16f887(the slave one). I wrote the folowing code for the master(dsPic33) but when I'm using the osciloscope is nothing to see, not even the clock. Please help me somehow.
include <p33FJ256GP710.h>
unsigned char Hbyte,Lbyte, x;
#define SCL1 TRISGbits.TRISG2 //cu ac. selectam I sau O
#define SDA1 TRISGbits.TRISG3 //-------//--------
#define SCL_VAL PORTGbits.RG2 // VALORILE BITILOR DE IESIRE SDA SI SCL
#define SDA_VAL PORTGbits.RG3
#define trisd3 TRISAbits.TRISA0 //
#define d3 PORTAbits.RA0 //
void i2c1_init(void);
void transmit(void);
void i2c1_init()
{
SCL1 = 0; //SELECTAM SDA SI SCL CA FIIND IESIRI
SDA1 = 0;
SCL_VAL = 0; //SELECTAM VALORILE LUI SCL SI SDA IN HIGH
SDA_VAL = 0;
trisd3 = 0;
//ODCG = 0xFFFF; //open drain control register
I2C1BRG = 0x016B;// SETTING THE CLOCK RATES 100KHZ
I2C1CON = 0x8000; //Enable I2C1 module; A10M=0 7-bit address
// I2C1ADD = 0x70; // 7-bit I2C slave address must be initialised here.
//(0x08-0x77 Valid 7-bit addresses)
I2C1ADD = 0x05;
I2C1CONbits.STREN = 1;
Hbyte = 0x00;
Lbyte = 0x08;
}
int main(void)
{
while (PORTDbits.RD6 ) continue;
i2c1_init();
while(1)
{
// i2c1_init();
/* wait here until switch S3 is pressed */
// I2C1CONbits.STREN = 1;
// I2C1CONbits.SCLREL = 0;
// for (x=0; x< 100; x++);
I2C1STATbits.S = 1; //setari inainte de start condition
I2C1STATbits.P = 0;
I2C1CONbits.SEN = 1; // Start condition
while(!_MI2C1IF) continue; //MI2CxIF interrupt is generated at completion of the Start condition.
// while(!_MI2C1IF);
_MI2C1IF = 0;
I2C1TRN = 0x0070; // slave adress with the R/W bit=0 am ales adresa la intamplare
while(I2C1STATbits.TBF)continue; //TBF=0 Transmit complete, I2CxTRN is empty(verific daca s-a incarcat reg I2C1TRN)
I2C1STATbits.TBF = 0;
while(I2C1STATbits.ACKSTAT) continue;//verific daca s-a receptionat de catre slave primul byte
I2C1TRN = Hbyte; //incarc registrul de trensmisie al masterului cu partea high a adresei deunde vreau sa citesc
while (I2C1STATbits.ACKSTAT) continue;//verific daca s-a receptionat aceast byte
I2C1TRN = Lbyte; //incarc registrul de transmisie cu partea low
while(I2C1STATbits.ACKSTAT) continue; //verific daca a fost receptionat
I2C1CON = I2C1CON&0xFFE0;
I2C1STATbits.S = 1; // generate a repeated start bus event
I2C1STATbits.P = 0;
I2C1CONbits.RSEN = 1; //setam bitul de restart
I2C1TRN = 0x0071; //slave adress with the R/W bit=1(read);
while(I2C1STATbits.ACKSTAT) continue;
/*
I2C1CON = I2C1CON&0xFFE0; //The lower 5 bits of the I2CxCON register must be ?0? before attempting to set
//the RCEN bit.This ensures the master logic is inactive.
I2C1CONbits.RCEN = 1; //Receive Enable bit
//while(!I2C1STATbits.RBF); //asteptam ca sa se receptioneze datele de la slave
I2C1CON = I2C1CON&0xFFE0;
I2C1CONbits.ACKEN = 1; //enables generation of a master Acknowledge sequence.
I2C1CONbits.ACKDT = 0; //Send ACK during Acknowledge
I2C1CON = I2C1CON&0xFFE0;
I2C1CONbits.PEN = 1; //stop contidion
while(!_MI2C1IF) continue; //The module generates the MI2CxIF interrup at the end of stop event
*/
d3= 1;
}
}
Sorry for my english
Hy to everybody, I'm new here. I'm woerking right now at my diploma project: I want to make a I2C communication between a Explorer 16 board from Microchip which has a dsPic33fj256gp710(I want to be the master one) and a pickit2 which has a pic16f887(the slave one). I wrote the folowing code for the master(dsPic33) but when I'm using the osciloscope is nothing to see, not even the clock. Please help me somehow.
include <p33FJ256GP710.h>
unsigned char Hbyte,Lbyte, x;
#define SCL1 TRISGbits.TRISG2 //cu ac. selectam I sau O
#define SDA1 TRISGbits.TRISG3 //-------//--------
#define SCL_VAL PORTGbits.RG2 // VALORILE BITILOR DE IESIRE SDA SI SCL
#define SDA_VAL PORTGbits.RG3
#define trisd3 TRISAbits.TRISA0 //
#define d3 PORTAbits.RA0 //
void i2c1_init(void);
void transmit(void);
void i2c1_init()
{
SCL1 = 0; //SELECTAM SDA SI SCL CA FIIND IESIRI
SDA1 = 0;
SCL_VAL = 0; //SELECTAM VALORILE LUI SCL SI SDA IN HIGH
SDA_VAL = 0;
trisd3 = 0;
//ODCG = 0xFFFF; //open drain control register
I2C1BRG = 0x016B;// SETTING THE CLOCK RATES 100KHZ
I2C1CON = 0x8000; //Enable I2C1 module; A10M=0 7-bit address
// I2C1ADD = 0x70; // 7-bit I2C slave address must be initialised here.
//(0x08-0x77 Valid 7-bit addresses)
I2C1ADD = 0x05;
I2C1CONbits.STREN = 1;
Hbyte = 0x00;
Lbyte = 0x08;
}
int main(void)
{
while (PORTDbits.RD6 ) continue;
i2c1_init();
while(1)
{
// i2c1_init();
/* wait here until switch S3 is pressed */
// I2C1CONbits.STREN = 1;
// I2C1CONbits.SCLREL = 0;
// for (x=0; x< 100; x++);
I2C1STATbits.S = 1; //setari inainte de start condition
I2C1STATbits.P = 0;
I2C1CONbits.SEN = 1; // Start condition
while(!_MI2C1IF) continue; //MI2CxIF interrupt is generated at completion of the Start condition.
// while(!_MI2C1IF);
_MI2C1IF = 0;
I2C1TRN = 0x0070; // slave adress with the R/W bit=0 am ales adresa la intamplare
while(I2C1STATbits.TBF)continue; //TBF=0 Transmit complete, I2CxTRN is empty(verific daca s-a incarcat reg I2C1TRN)
I2C1STATbits.TBF = 0;
while(I2C1STATbits.ACKSTAT) continue;//verific daca s-a receptionat de catre slave primul byte
I2C1TRN = Hbyte; //incarc registrul de trensmisie al masterului cu partea high a adresei deunde vreau sa citesc
while (I2C1STATbits.ACKSTAT) continue;//verific daca s-a receptionat aceast byte
I2C1TRN = Lbyte; //incarc registrul de transmisie cu partea low
while(I2C1STATbits.ACKSTAT) continue; //verific daca a fost receptionat
I2C1CON = I2C1CON&0xFFE0;
I2C1STATbits.S = 1; // generate a repeated start bus event
I2C1STATbits.P = 0;
I2C1CONbits.RSEN = 1; //setam bitul de restart
I2C1TRN = 0x0071; //slave adress with the R/W bit=1(read);
while(I2C1STATbits.ACKSTAT) continue;
/*
I2C1CON = I2C1CON&0xFFE0; //The lower 5 bits of the I2CxCON register must be ?0? before attempting to set
//the RCEN bit.This ensures the master logic is inactive.
I2C1CONbits.RCEN = 1; //Receive Enable bit
//while(!I2C1STATbits.RBF); //asteptam ca sa se receptioneze datele de la slave
I2C1CON = I2C1CON&0xFFE0;
I2C1CONbits.ACKEN = 1; //enables generation of a master Acknowledge sequence.
I2C1CONbits.ACKDT = 0; //Send ACK during Acknowledge
I2C1CON = I2C1CON&0xFFE0;
I2C1CONbits.PEN = 1; //stop contidion
while(!_MI2C1IF) continue; //The module generates the MI2CxIF interrup at the end of stop event
*/
d3= 1;
}
}
Sorry for my english