/*
Function: I2CStart
Return:
Arguments:
Description: Send a start condition on I2C Bus
*/
void I2CStart()
{
SSPCON2bits.SEN = 1; /* Start condition enabled */
//while(SSPCON2bits.SEN); /* automatically cleared by hardware */
//LATCbits.LATC0 = 1; /* wait for start condition to finish */
}
/*
Function: I2CStop
Return:
Arguments:
Description: Send a stop condition on I2C Bus
*/
void I2CStop()
{
SSPCON2bits.PEN = 1; /* Stop condition enabled */
//while(SSPCON2bits.PEN); /* Wait for stop condition to finish */
LATCbits.LATC0 = 1; /* PEN automatically cleared by hardware */
}
/*
Function: I2CRestart
Return:
Arguments:
Description: Sends a repeated start condition on I2C Bus
*/
void I2CRestart()
{
SSPCON2bits.RSEN = 1; /* Repeated start enabled */
//while(SSPCON2bits.RSEN); /* wait for condition to finish */
}
/*
Function: I2CAck
Return:
Arguments:
Description: Generates acknowledge for a transfer
*/
void I2CAck()
{
SSPCON2bits.ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
SSPCON2bits.ACKEN = 1; /* Ack data enabled */
//while(SSPCON2bits.ACKEN); /* wait for ack data to send on bus */
}
/*
Function: I2CNck
Return:
Arguments:
Description: Generates Not-acknowledge for a transfer
*/
void I2CNak()
{
SSPCON2bits.ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
SSPCON2bits.ACKEN = 1; /* Ack data enabled */
//while(SSPCON2bits.ACKEN); /* wait for ack data to send on bus */
}
/*
Function: I2CWait
Return:
Arguments:
Description: wait for transfer to finish
*/
void I2CWait()
{
while ((SSPCON2 == 0x1F ) || ( SSPSTAT == 0x04 ) );
/* wait for any pending transfer */
}
/*
Function: I2CSend
Return:
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)
{
SSPBUF = dat; /* Move data to SSPBUF */
//while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
/*
Function: I2CRead
Return: 8-bit data read from I2C bus
Arguments:
Description: read 8-bit data from I2C bus
*/
unsigned char I2CRead(void)
{
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
SSPCON2bits.RCEN = 1; /* Enable data reception */
//while(!SSPSTATbits.BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
I2CWait(); /* wait to check any pending transfer */
return temp; /* Return the read data from bus */
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum