📄 i2c.c
字号:
vDelay ( I2C_DELAY );
}
/************** Send Byte *******************/
#define I2C_DELAY_for_writting 8
void I2C_SendByte_for_writting ( unsigned char ucData )
{
register unsigned char i;
//while ( (cI2C_Status != I2C_OK));// & i<250) // error -> do not send Byte, BUS is not free
// this error check can be embedded in the "for" loop
// to cover errors that occured during transmision
for ( i=0; i<8; i++ )
{
if ( ucData & 0x80 ) // bit 7 - check and -> if(bit==1) ...
{ // set data line to the MSB (bit 7)
I2C_PORT_DIR &= ~SDA; // SDA pin = 1
} else {
I2C_PORT_DIR |= SDA; // SDA pin = 0
}
vDelay ( I2C_DELAY_for_writting );
I2C_SCL_High_for_CP_writting (); // SCL pin = 1
// vDelay ( I2C_DELAY_for_writting );
I2C_PORT_DIR |= SCL; // SCL pin = 0
// vDelay ( I2C_DELAY_for_writting );
ucData = ucData << 1; // next bit will be 6 ... 0
}
// vDelay ( I2C_Delay );
I2C_PORT_DIR &= ~SDA; // SDA pin = high - listen for ACK
//vDelay ( I2C_DELAY_for_writting );
I2C_SCL_High_for_CP_writting (); // SCL pin = high
//vDelay ( I2C_DELAY_for_writting );
if ( I2C_PORT_IN & SDA ) // bit check and -> if(bit==1) SDA is high = NACK
{
cI2C_Status = I2C_NACK; // NACK
}
// vDelay ( I2C_Delay );
I2C_PORT_DIR |= SCL; // SCL pin = 0
// vDelay ( I2C_DELAY_for_writting );
}
/************** Get Byte ***************/
unsigned char I2C_GetByte ( unsigned char ucLastByteFlag )
{
register unsigned char i,
ucRxData; // received byte
if ( cI2C_Status != I2C_OK ) // error -> do not read Byte
return(cI2C_Status);
ucRxData = 0;
for ( i = 0; i < 8 ; i++ )
{
I2C_SCL_High (); // SCL pin = high
vDelay ( I2C_DELAY );
ucRxData = ucRxData << 1; // shifts the recieved bits
if ( I2C_PORT_IN & SDA ) // is SDA = 1
{
ucRxData |= 0x01; // sets bit 0
}
I2C_PORT_DIR |= SCL; // SCL pin = 0
vDelay ( I2C_DELAY );
}
if ( ucLastByteFlag == 0 )
{
I2C_PORT_DIR &= ( SDA ); // SDA pin = low => ACK
I2C_SCL_High (); // SCL pin = high
vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
} else
{ // last byte -> send NACK
I2C_PORT_DIR &= ( ~SDA ); // SDA pin = high => NACK
I2C_SCL_High (); // SCL pin = high
vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
}
return ( ucRxData );
}
unsigned char I2C_GetByte_for_CP ( unsigned char ucLastByteFlag )
{
register unsigned char i,
ucRxData; // received byte
if ( cI2C_Status != I2C_OK ) // error -> do not read Byte
return(cI2C_Status);
ucRxData = 0;
for ( i = 0; i < 8 ; i++ )
{
I2C_SCL_High_for_CP_writting (); // SCL pin = high
// vDelay ( I2C_DELAY );
ucRxData = ucRxData << 1; // shifts the recieved bits
if ( I2C_PORT_IN & SDA ) // is SDA = 1
{
ucRxData |= 0x01; // sets bit 0
}
I2C_PORT_DIR |= SCL; // SCL pin = 0
// vDelay ( I2C_DELAY );
}
if ( ucLastByteFlag == 0 )
{
I2C_PORT_DIR &= ( SDA ); // SDA pin = low => ACK
I2C_SCL_High_for_CP_writting (); // SCL pin = high
//vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
} else
{ // last byte -> send NACK
I2C_PORT_DIR &= ( ~SDA ); // SDA pin = high => NACK
I2C_SCL_High_for_CP_writting (); // SCL pin = high
//vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
}
return ( ucRxData );
}
/************** Get Two Bytes ***************/
unsigned char I2C_Get_two_Bytes ( unsigned char ucLastByteFlag )
{
register unsigned int i,
ucRxData; // received byte
if ( cI2C_Status != I2C_OK ) // error -> do not read Byte
return(cI2C_Status);
ucRxData = 0;
for ( i = 0; i < 16 ; i++ )
{
I2C_SCL_High (); // SCL pin = high
// vDelay ( I2C_DELAY );
ucRxData = ucRxData << 1; // shifts the recieved bits
if ( I2C_PORT_IN & SDA ) // is SDA = 1
{
ucRxData |= 0x01; // sets bit 0
}
I2C_PORT_DIR |= SCL; // SCL pin = 0
vDelay ( 10 );
}
if ( ucLastByteFlag == 0 )
{
I2C_PORT_DIR &= ( SDA ); // SDA pin = low => ACK
I2C_SCL_High (); // SCL pin = high
vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
} else
{ // last byte -> send NACK
I2C_PORT_DIR &= ( ~SDA ); // SDA pin = high => NACK
I2C_SCL_High (); // SCL pin = high
vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
}
return ( ucRxData );
}
/************************************************************************/
/*********************************************/
void I2C_DelayWriteByteNum_for_cp ( unsigned char ucDevAddr,
unsigned char ucDataAddr,
unsigned char const *pucData,
unsigned char ucNumBytes,
unsigned int uivDelay )
{
char i;
I2C_MASK_IE_PORT &= ~I2C_IRQ_MASK; // forbid IRQ
vDelay ( uivDelay );
// move IRQ mask here ???
I2C_SendStart();
if ( cI2C_Status == I2C_OK )
{
I2C_SendByte( ucDevAddr );
I2C_SendByte( ucDataAddr );
for ( i = 0; i < ucNumBytes; i++ ) // does not check cI2C_Status for bytes
{
I2C_SendByte_for_writting( *(pucData+i ) );
}
}
// vDelay(300);
I2C_SendStop();
ucI2C_Err = cI2C_Status;
I2C_MASK_IE_PORT |= I2C_IRQ_MASK; // allow IRQ
vDelay(8000);
}
void I2C_Send_pure_Byte ( unsigned char ucData )
{
register unsigned char i;
if ( cI2C_Status != I2C_OK ) // error -> do not send Byte, BUS is not free
return; // this error check can be embedded in the "for" loop
// to cover errors that occured during transmision
for ( i=0; i<8; i++ )
{
if ( ucData & 0x80 ) // bit 7 - check and -> if(bit==1) ...
{ // set data line to the MSB (bit 7)
I2C_PORT_DIR &= ~SDA; // SDA pin = 1
} else {
I2C_PORT_DIR |= SDA; // SDA pin = 0
}
vDelay ( I2C_DELAY );
I2C_SCL_High (); // SCL pin = 1
vDelay ( I2C_DELAY );
I2C_PORT_DIR |= SCL; // SCL pin = 0
vDelay ( I2C_DELAY );
ucData = ucData << 1; // next bit will be 6 ... 0
}
}
void I2C_Send_Byte_end ( void )
{
// vDelay ( I2C_Delay );
I2C_PORT_DIR &= ~SDA; // SDA pin = high - listen for ACK
vDelay ( 10 );
I2C_SCL_High (); // SCL pin = high
vDelay ( 10 );
if ( I2C_PORT_IN & SDA ) // bit check and -> if(bit==1) SDA is high = NACK
{
cI2C_Status = I2C_NACK; // NACK
}
// vDelay ( I2C_Delay );
I2C_PORT_DIR |= SCL; // SCL pin = 0
vDelay ( I2C_DELAY );
}
unsigned char ucI2C_Read_first_Byte ( unsigned char ucDevAddr,
unsigned char ucDataAddr)
{
unsigned char ucData;
I2C_MASK_IE_PORT &= ~I2C_IRQ_MASK; // forbid IRQ
_NOP();
I2C_SendStart();
if ( cI2C_Status == I2C_OK )
{
I2C_SendByte( ucDevAddr ); // write address !!
I2C_SendByte( ucDataAddr );
I2C_SendStop();
I2C_SendStart();
I2C_SendByte( ucDevAddr | I2C_READ ); // read address
ucData = I2C_GetByte( I2C_LAST_BYTE ); // return value
}
return ucData;
}
unsigned char continue_to_read()
{ unsigned char ucData;
I2C_SendStop();
vDelay ( I2C_DELAY_for_writting );
I2C_SendStart();
vDelay ( I2C_DELAY_for_writting );
I2C_SendByte_for_writting( 0x21 ); // read address
ucData = I2C_GetByte_for_CP( I2C_LAST_BYTE ); // return value
vDelay ( I2C_DELAY_for_writting );
return ucData;
}
void read_end(void)
{
I2C_SendStop();
vDelay ( I2C_DELAY_for_writting );
ucI2C_Err = cI2C_Status;
I2C_MASK_IE_PORT |= I2C_IRQ_MASK; // allow IRQ
vDelay ( I2C_DELAY_for_writting );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -