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.

I2C doesnt seem to be initialise..

Status
Not open for further replies.

InNeedOfHelp

Full Member level 3
Full Member level 3
Joined
Dec 5, 2012
Messages
169
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Visit site
Activity points
2,205
Code:
#define	TC74_READ	   0x9B
#define	TC74_WRITE	   0x9A  


void i2c_Init(void)
{

   // Initialise I2C MSSP
   // Master 100KHz
   TRISCbits.TRISC3=1;         // set SCL and SDA pins as inputs
   TRISCbits.TRISC4=1;

   SSPCON1 = 0b00101000 ;  	// I2C enabled, Master mode
   //SSPCON2 = 0x00;
   // I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) 
   SSPADD = 9;    		// 100Khz @ 	4Mhz Fosc

   SSPSTATbits.SMP =1; 	// Slew rate disabled

}

// i2c_Wait - wait for I2C transfer to finish
void i2c_Wait(void)
{
    while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
}

// i2c_Start - Start I2C communication
void i2c_Start(void)
{
    i2c_Wait();
    SSPCON2bits.SEN=1;
}

// i2c_Restart - Re-Start I2C communication
void i2c_Restart(void)
{
    i2c_Wait();
    SSPCON2bits.RSEN=1;
}

// i2c_Stop - Stop I2C communication
void i2c_Stop(void)
{
    i2c_Wait();
    SSPCON2bits.PEN=1;
}

// i2c_Write - Sends one byte of data
void i2c_Write(unsigned char data)
{
    i2c_Wait();
    SSPBUF = data;
}

// i2c_Address - Sends Slave Address and Read/Write mode
// mode is either I2C_WRITE or I2C_READ
void i2c_Address(unsigned char address, unsigned char mode)
{
    unsigned char l_address;

    l_address=address<<1;
    l_address+=mode;
    i2c_Wait();
    SSPBUF = l_address;
}

// i2c_Read - Reads a byte from Slave device
unsigned char i2c_Read(unsigned char ack)
{
    // Read data from slave
    // ack should be 1 if there is going to be more data read
    // ack should be 0 if this is the last byte of data read
    unsigned char i2cReadData;

    i2c_Wait();
    SSPCON2bits.RCEN=1;
    i2c_Wait();
    i2cReadData = SSPBUF;
    i2c_Wait();
    if ( ack ) 
		SSPCON2bits.ACKDT=0;	        // Ack
    else       
		SSPCON2bits.ACKDT=1;	        // NAck
    SSPCON2bits.ACKEN=1;                    // send acknowledge sequence

    return( i2cReadData );
}

hi guys,

i need some help here doing I2C initialization.. can anyone help me please..

im using c-18 compiler using pic18f4520
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top