?? i2c.c
字號:
/*****************************************************************************************
* 模塊名稱: I2C通訊模塊
* 模塊描述: SmartArm2200 I2C操作。CAT1025 EEPROM,實現(xiàn)1到8字節(jié)數(shù)據(jù)的讀寫操作
* 創(chuàng)建人 : hlb
* 創(chuàng)建日期: 2008-5-4
******************************************************************************************
* 修改人 :
* 修改日期:
*****************************************************************************************/
#include <LPC22xx.h>
#include "Public.h"
#include "I2C.h"
__irq void I2C_isr (void);
/*****************************************************************************************
* 常亮定義
*****************************************************************************************/
#define I2C_SLAVE_ADDRESS 0xA0
#define I2C_NONE 0
#define I2C_READ 1
#define I2C_WRITE 2
#define I2C_AA (1 << 2)
#define I2C_SI (1 << 3)
#define I2C_STO (1 << 4)
#define I2C_STA (1 << 5)
#define I2C_I2EN (1 << 6)
/*****************************************************************************************
* 定義用于和I2C中斷傳遞信息的全局變量
*****************************************************************************************/
static volatile U8 I2CAddress; //I2C器件從地址
static volatile U8 I2CRamAddress; //I2C器件內(nèi)部子地址
static volatile U8 I2CNum; //I2C要發(fā)送活著讀取的字節(jié)數(shù)
static volatile U8 *I2CBuf; //數(shù)據(jù)緩沖區(qū)指針
static volatile U8 ReadBuf[8]; //讀緩沖區(qū)
static volatile U8 WriteBuf[8]; //寫緩沖區(qū)
static volatile U8 I2CEnd; //I2C總線結(jié)束標(biāo)志:結(jié)束總線是置1
static volatile U8 I2CAddEn; //子地址控制。
//0--子地址已經(jīng)處理或者不需要子地址
//1--讀操作
//2--寫操作
/*****************************************************************************************
* 函數(shù)名稱: void I2C_Init(U32 FI2C)
* 函數(shù)描述: I2C初始化,允許中斷,分配中斷優(yōu)先級為1
* 參數(shù)描述:FI2C 初始化I2C總線頻率,最大值為400K
*****************************************************************************************/
void I2C_Init(U32 FI2C)
{
U32 tmFI2C;
tmFI2C = FI2C;
if (tmFI2C > 400000) tmFI2C = 400000;
PINSEL0 = (PINSEL0 & 0xFFFFFF0F) | 0x50; //引腳功能選擇
/* 設(shè)置時鐘 */
I2SCLH = (GD.Fpclk / tmFI2C + 1) / 2;
I2SCLL = (GD.Fpclk / tmFI2C) / 2;
/* 設(shè)為主模式 */
I2CONCLR = 0x2C;
I2CONSET = 0x40;
I2CAddress = I2C_SLAVE_ADDRESS;
VICIntSelect = VICIntSelect & (~(1 << 9)); //設(shè)置I2C中斷為普通中斷
VICIntEnable = VICIntEnable | (1 << 9); //允許I2C中斷
VICVectAddr0 = (U32)I2C_isr;
VICVectCntl0 = 0x20 | 0x9;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteByte(U32 Address, U8 Data)
* 函數(shù)描述: 寫一個字節(jié)
*****************************************************************************************/
bool I2C_WriteBytes(U8 SlaveAddress, U8 RamAddress, U8 *P, U8 Len)
{
int i;
I2CAddress = SlaveAddress & 0xFE;
I2CRamAddress = RamAddress;
I2CNum = Len;
if (I2CNum < 1) I2CNum = 1;
if (I2CNum > 8) I2CNum = 8;
for (i = 0; i < I2CNum; i++)
{
WriteBuf[i] = *(P+i);
}
I2CBuf = WriteBuf;
I2CEnd = 0; //置位標(biāo)志
I2CAddEn = 2; //寫操作
I2CONCLR = 0x2C; //清除STA, SI, AA
I2CONSET = 0x60; //使能并發(fā)送起始標(biāo)志
while(I2CEnd == 0); //等待寫入完成
if (I2CEnd == 1)
{
return TRUE;
}
else
{
return FALSE;
}
}
/*****************************************************************************************
* 函數(shù)名稱: U8 I2C_ReadByte(U32 Address)
* 函數(shù)描述: 讀一個字節(jié)
*****************************************************************************************/
bool I2C_ReadBytes(U8 SlaveAddress, U8 RamAddress, U8 *P, U8 Len)
{
int i, tmI2CNum;
I2CAddress = SlaveAddress & 0xFE;
I2CRamAddress = RamAddress;
I2CNum = Len;
if (I2CNum < 1) I2CNum = 1;
if (I2CNum > 8) I2CNum = 8;
tmI2CNum = I2CNum;
I2CBuf = ReadBuf;
I2CEnd = 0;
I2CAddEn = I2C_READ; //讀操作
I2CONCLR = I2C_STA | I2C_SI | I2C_AA; //清除STA, SI, AA
I2CONSET = I2C_I2EN | I2C_STA; //使能并發(fā)送起始標(biāo)志
while(I2CEnd == 0); //等待讀操作完成
if (I2CEnd == 1)
{
for (i = 0; i < tmI2CNum; i++)
{
*(P+i) = ReadBuf[i];
}
return TRUE;
}
else
{
return FALSE;
}
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteU8(U32 Address, U8 Data)
* 函數(shù)描述: 寫U8
*****************************************************************************************/
void I2C_WriteU8(U32 Address, U8 Data)
{
static U8 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, &D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: U8 I2C_ReadU8(U32 Address)
* 函數(shù)描述: 讀U8
*****************************************************************************************/
U8 I2C_ReadU8(U32 Address)
{
static U8 D;
if (I2C_ReadBytes(I2CAddress, Address, &D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteS8(U32 Address, S8 Data)
* 函數(shù)描述: 寫S8
*****************************************************************************************/
void I2C_WriteS8(U32 Address, S8 Data)
{
static S8 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, (U8 *)&D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: S8 I2C_ReadS8(U32 Address)
* 函數(shù)描述: 讀S8
*****************************************************************************************/
S8 I2C_ReadS8(U32 Address)
{
static S8 D;
if (I2C_ReadBytes(I2CAddress, Address, (U8 *)&D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteU16(U32 Address, U16 Data)
* 函數(shù)描述: 寫U16
*****************************************************************************************/
void I2C_WriteU16(U32 Address, U16 Data)
{
static U16 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, (U8 *)&D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: U16 I2C_ReadU16(U32 Address)
* 函數(shù)描述: 讀U16
*****************************************************************************************/
U16 I2C_ReadU16(U32 Address)
{
static U16 D;
if (I2C_ReadBytes(I2CAddress, Address, (U8 *)&D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteS16(U32 Address, S16 Data)
* 函數(shù)描述: 寫S16
*****************************************************************************************/
void I2C_WriteS16(U32 Address, S16 Data)
{
static S16 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, (U8 *)&D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: S16 I2C_ReadS16(U32 Address)
* 函數(shù)描述: 讀S16
*****************************************************************************************/
S16 I2C_ReadS16(U32 Address)
{
static S16 D;
if (I2C_ReadBytes(I2CAddress, Address, (U8 *)&D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteU32(U32 Address, U32 Data)
* 函數(shù)描述: 寫U32
*****************************************************************************************/
void I2C_WriteU32(U32 Address, U32 Data)
{
static U32 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, (U8 *)&D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: U32 I2C_ReadU32(U32 Address)
* 函數(shù)描述: 讀U32
*****************************************************************************************/
U32 I2C_ReadU32(U32 Address)
{
static U32 D;
if (I2C_ReadBytes(I2CAddress, Address, (U8 *)&D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteS32(U32 Address, S32 Data)
* 函數(shù)描述: 寫S32
*****************************************************************************************/
void I2C_WriteS32(U32 Address, S32 Data)
{
static S32 D;
D = Data;
I2C_WriteBytes(I2CAddress, Address, (U8 *)&D, sizeof(D));
}
/*****************************************************************************************
* 函數(shù)名稱: S32 I2C_ReadS32(U32 Address)
* 函數(shù)描述: 讀S32
*****************************************************************************************/
S32 I2C_ReadS32(U32 Address)
{
static S32 D;
if (I2C_ReadBytes(I2CAddress, Address, (U8 *)&D, sizeof(D)))
return D;
else return 0;
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_WriteFloat(U32 Address, Float Data)
* 函數(shù)描述: 寫float
*****************************************************************************************/
void I2C_WriteFloat(U32 Address, float Data)
{
UnionFloat UF;
UF.F = Data;
I2C_WriteU32(Address, UF.D);
}
/*****************************************************************************************
* 函數(shù)名稱: float I2C_ReadFloat(U32 Address)
* 函數(shù)描述: 讀float
*****************************************************************************************/
float I2C_ReadFloat(U32 Address)
{
UnionFloat UF;
UF.D = I2C_ReadU32(Address);
return (UF.F);
}
/*****************************************************************************************
* 函數(shù)名稱: void I2C_isr (void)
* 函數(shù)描述: I2C中斷處理程序
*****************************************************************************************/
__irq void I2C_isr (void)
{
U8 sta;
sta = I2STAT;
switch(sta)
{
case 0x08:
I2DAT = I2CAddress;
I2CONCLR = I2C_STO | I2C_SI; //清起始標(biāo)志,中斷標(biāo)志
break;
case 0x10:
I2DAT = I2CAddress;
I2CONCLR = I2C_STO | I2C_SI;
break;
case 0x18:
if (I2CAddEn == I2C_READ)
{
I2DAT = I2CRamAddress;
I2CONCLR = I2C_STA | I2C_STO | I2C_SI;
}
else if(I2CAddEn == I2C_WRITE)
{
I2DAT = I2CRamAddress;
I2CONCLR = I2C_STA | I2C_STO | I2C_SI;
}
break;
case 0x28:
if (I2CAddEn == I2C_READ)
{
I2CAddress = I2CAddress | 0x1; //改寫從地址為讀
I2CONCLR = I2C_STO | I2C_SI; //
I2CONSET = I2C_STA; //發(fā)送重復(fù)起始條件
}
else
{
if (I2CNum >= 1) //將發(fā)送數(shù)據(jù)字節(jié),接收ACK位
{
I2DAT = *I2CBuf++;
I2CONCLR = I2C_STA | I2C_STO | I2C_SI;
I2CNum--;
}
else
{
I2CONCLR = I2C_STA | I2C_SI;
I2CONSET = I2C_STO;
I2CEnd = 1;
}
}
break;
case 0x20:
case 0x30:
case 0x38:
I2CONCLR = 0x28;
I2CEnd = 0xFF;
break;
case 0x40:
if(I2CNum == 1) //準(zhǔn)備接收數(shù)據(jù),返回非ACK位
{
I2CONCLR = I2C_STA | I2C_STO | I2C_SI | I2C_AA;
}
else //準(zhǔn)備接收數(shù)據(jù),返回ACk位
{
I2CONCLR = I2C_STA | I2C_STO | I2C_SI;
I2CONSET = I2C_AA;
}
break;
case 0x50:
*I2CBuf++ = I2DAT;
I2CNum--;
if(I2CNum == 1)
{
I2CONCLR = I2C_STA | I2C_STO | I2C_SI | I2C_AA;
}
else
{
I2CONCLR = I2C_STA | I2C_STO | I2C_SI;
I2CONSET = I2C_AA;
}
break;
case 0x58:
*I2CBuf++ = I2DAT;
I2CONSET = I2C_STO;
I2CONCLR = I2C_STA | I2C_SI;
I2CEnd = 1;
break;
case 0x48:
I2CONCLR = 0x28;
I2CEnd = 0xFF;
break;
default:
break;
}
VICVectAddr = 0x00;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -