#include "i2c_lib.h"
//------------------------------------------------------------------------------
/*
* Description: Initialize I2C in master mode, Sets the required baudrate
*/
void I2CInit(void)
{
// BF RCinprocess_TXcomplete; UA dontupdate; SMP High Speed;
// P stopbit_notdetected; S startbit_notdetected; R_nW write_noTX;
// CKE Idle to Active; D_nA lastbyte_address;
SSP1STAT = 0x00;
// SSPEN enabled; WCOL no_collision; SSPOV no_overflow;
// CKP Idle:Low, Active:High; SSPM FOSC/4_SSPxADD;
SSP1CON1 = 0x28;
// BOEN disabled; AHEN disabled; SBCDE disabled; SDAHT 100ns; DHEN disabled;
// ACKTIM ackseq; PCIE disabled; SCIE disabled;
SSP1CON3 = 0x00;
// Baud Rate Generator Value: SSPADD 39;
SSP1ADD = 0x27;
/* Byte sent or received */
// clear the master interrupt flag
PIR1bits.SSP1IF = 0;
// enable the master interrupt
PIE1bits.SSP1IE = 1;
}
//------------------------------------------------------------------------------
/*
* Description: Send a start condition on I2C Bus
*/
void I2CStart()
{
SSP1CON2bits.SEN = 1; /* Start condition enabled */
while (SSP1CON2bits.SEN); /* automatically cleared by hardware */
/* wait for start condition to finish */
}
//------------------------------------------------------------------------------
/*
* Description: Send a stop condition on I2C Bus
*/
void I2CStop()
{
SSP1CON2bits.PEN = 1; /* Stop condition enabled */
while (SSP1CON2bits.PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
//------------------------------------------------------------------------------
/*
* Description: Sends a repeated start condition on I2C Bus
*/
void I2CRestart()
{
SSP1CON2bits.RSEN = 1; /* Repeated start enabled */
while (SSP1CON2bits.RSEN); /* wait for condition to finish */
}
//------------------------------------------------------------------------------
/*
* Description: Generates acknowledge for a transfer
*/
void I2CAck()
{
SSP1CON2bits.ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
while (SSP1CON2bits.ACKEN); /* wait for ack data to send on bus */
}
//------------------------------------------------------------------------------
/*
* Description: Generates Not-acknowledge for a transfer
*/
void I2CNak()
{
SSP1CON2bits.ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
while (SSP1CON2bits.ACKEN); /* wait for ack data to send on bus */
}
//------------------------------------------------------------------------------
/*
* Description: wait for transfer to finish
*/
void I2CWait()
{
while ((SSP1CON2 & 0x1F) || (SSP1STAT & 0x04));
/* wait for any pending transfer */
}
//------------------------------------------------------------------------------
/*
* Arguments: dat - 8-bit data to be sent on bus data can be either address/data byte
* Description: Send 8-bit data on I2C bus
*/
void I2CSend(unsigned char dat)
{
SSP1BUF = dat; /* Move data to SSPBUF */
while (SSP1STATbits.BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
//------------------------------------------------------------------------------
/*
* Return: 8-bit data read from I2C bus
* Description: read 8-bit data from I2C bus
*/
unsigned char I2CRead(void)
{
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
SSP1CON2bits.RCEN = 1; /* Enable data reception */
while (!SSP1STATbits.BF); /* wait for buffer full */
temp = SSP1BUF; /* Read serial buffer and store in temp register */
I2CWait(); /* wait to check any pending transfer */
return temp; /* Return the read data from bus */
}