📄 iiccore.c
字号:
/*
This file is should not add to project.It should include by other .c/.cpp file.Before include this file
you should add the define for :
iic_ClrSda(), iic_SetSda(),
iic_ClrScl(), iic_SetScl(),
iic_GetSda()
*/
#if 0
#define iic_Delay()
#else
static void iic_Delay()
{
unsigned char i=0;
while(i<I2C_DELAY_TIME) i++;
}
#endif
/*************************************************************
Issue a START condition to I2C
**************************************************************/
static void iic_StartCode()
{
iic_SetSda(); // both = 1
iic_SetScl(); // both = 1
iic_Delay();
iic_ClrSda(); // SDA = 0
iic_Delay();
iic_ClrScl(); // SCL = 0
iic_Delay();
}
/*******************************************************************
Issue a STOP condition to I2C
*******************************************************************/
static void iic_StopCode()
{
iic_ClrSda(); // SDA = 0
iic_Delay();
iic_SetScl(); // SCL = 1
iic_Delay();
iic_SetSda(); // SDA = 1
iic_Delay();
}
/*******************************************************************
Issue a Ack command to I2C
*******************************************************************/
static void iic_SendAck()
{
iic_ClrSda();
iic_Delay();
iic_SetScl();
iic_Delay();
iic_ClrScl();
iic_Delay();
}
/*******************************************************************
Issue a UnAck command to I2C
*******************************************************************/
static void iic_SendUnack()
{
iic_SetSda();
iic_Delay();
iic_SetScl();
iic_Delay();
iic_ClrScl(); // SCL = 0
iic_Delay();
}
/*******************************************************************
Verify ACK command from slave after send a data out to I2C
*******************************************************************/
static unsigned char iic_CheckAck()
{
unsigned char i;
unsigned char ack;
iic_SetSda();
iic_Delay();
iic_SetScl();
iic_Delay();
for(i=0; i< CHECK_I2C_ACK_TIME; i++)
{
ack = iic_GetSda();
if(!ack){
iic_ClrScl();
return TRUE;
}
iic_Delay();
}
iic_StopCode();
return FALSE;
}
/*******************************************************************
Get a data from I2C
*******************************************************************/
static unsigned char iic_GetData()
{
unsigned char val,i;
unsigned char tmp;
val = 0;
//we should set SDA = 1,
//if not the chip can't pull up and we will receive zero.
iic_SetSda(); // SDA = 1
for(i=0;i<8;i++) {
iic_Delay();
val <<= 1;
tmp = iic_GetSda();
if (tmp)
val = val | 0x1;
iic_Delay();
iic_SetScl(); // SCL = 1
iic_Delay();
iic_ClrScl(); // SCL = 0
iic_Delay();
}
return val;
}
/*************************************************************
Send a DATA out to I2C
**************************************************************/
static void iic_SendData(unsigned char val)
{
unsigned char i;
for(i=0;i<8;i++)
{
if( val & 0x80 )
iic_SetSda(); // SDA = 1
else
iic_ClrSda(); // SDA = 0
val <<= 1;
iic_Delay();
iic_SetScl(); // SCL = 1
iic_Delay();
iic_ClrScl(); // SCL = 0
iic_Delay();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -