?? i2c.c
字號:
/*****************************************************************
*文件名:I2C.c
*描述:提供I2C的底層驅動,可選的400K或100K速度。
*******************************************************************/
#define I2C_C
#include "I2C.h"
CONST uchar cucBit[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
/******************************************************************
*函數性質:私有
*入口:無
*出口:無
*功能:實現延時功能
********************************************************************/
static void DelayIntr(void)
{
#if I2C_SPEED == I2C_100K
NOP();NOP();NOP();NOP();
#elif I2C_SPEED == I2C_400K
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
#endif
}
/*****************************************************************
*函數性質:公共
*入口:無
*出口:無
*功能:啟動I2C。
********************************************************************/
void I2cStart(void)
{
SET_SCL();
SET_SDA();
DelayIntr();
CLR_SDA();
DelayIntr();
CLR_SCL();
}
/***********************************************************************
*函數性質:公共
*入口:無
*出口:無
*功能:停止I2C。
******************************************************************/
void I2cStop(void)
{
CLR_SCL();
CLR_SDA();
DelayIntr();
SET_SCL();
DelayIntr();
SET_SDA();
}
/*****************************************************************
*函數性質:公共
*入口:無
*出口:應答標志,true:檢測到應答標志,false:無應答標志
*功能:檢測從機的應答標志
****************************************************************/
bool WaitAck(void)
{
uchar ucErrTime = 255; //因故障接收方無ACK,超時值。
CLR_SCL();
SET_SDA();
DelayIntr();
SET_SCL();
DelayIntr();
PTCDD_PTCDD1=0; //IO置為接收態
while(READ_SDA())
{
ucErrTime--;
if (ucErrTime == 0)
{
I2cStop();
return false;
}
}
PTCDD_PTCDD1=1; //IO置發送態發送態
CLR_SCL();
return true;
}
/******************************************************************
*函數性質:公共
*入口:無
*出口:無
*功能:發送應答標志
******************************************************************/
void SendAck(void)
{
CLR_SCL();
CLR_SDA();
DelayIntr();
SET_SCL();
DelayIntr();
CLR_SCL();
}
/*****************************************************************
*函數性質:公共
*入口:無
*出口:無
*功能:發送非應答標志
*****************************************************************/
void SendNotAck(void)
{
CLR_SCL();
SET_SDA();
DelayIntr();
SET_SCL();
DelayIntr();
CLR_SCL();
}
/******************************************************************
*函數性質:公共
*入口:待發送的字符
*出口;發送成功標志,true 發送成功 false 發送失敗
*功能:向I2C接口發送一個字符
******************************************************************/
bool I2cSend(uchar ucData)
{
uchar i;
for (i = 0; i < 8; i++)
{
CLR_SCL();
DelayIntr();
if ( (ucData & cucBit[i]) != 0)
{
SET_SDA();
}
else
{
CLR_SDA();
}
DelayIntr();
SET_SCL();
DelayIntr();
}
CLR_SCL();
return true;
}
/********************************************************************
*函數性質:公共
*入口:無
*出口:從I2C接口接收到的數據
*功能:從I2C接口接收一個數據。
********************************************************************/
uchar I2cReceive(void)
{
uchar ucData = 0x00;
uchar i;
SET_SDA();
for ( i = 0; i < 8; i++)
{
CLR_SCL();
DelayIntr();
SET_SCL();
DelayIntr();
PTCDD_PTCDD1=0;
if (READ_SDA())
{
ucData |= cucBit[i];
}
}
PTCDD_PTCDD1=1;
CLR_SCL();
return ucData;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -