⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 优龙FS2410板的I2C通信源码
💻 C
字号:
#include "def.h"
#include "2410addr.h"
#include "2410lib.h"
#define WRDATA      (1)
#define POLLACK     (2)
#define RDDATA      (3)
#define SETRDADDR   (4)
#define IICBUFSIZE 0x20

static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;

void Wr24C040 (U32 slvAddr, U8 addr, U8 data)
{
_iicMode=WRDATA;
_iicPt=0;
_iicData [0] = (U8) addr;
_iicData [1] =data;
_iicDataCount=2;
rIICDS=slvAddr; //0xa0
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has beencleared.
while (_iicDataCount!=-1);
_iicMode=POLLACK;
while (1)
{
rIICDS=slvAddr;
_iicStatus=0x100;
rIICSTAT=0xf0; //MasTx,Start
rIICCON=0xaf; //resumes IIC operation.
while (_iicStatus==0x100);
if (! (_iicStatus&0x1))
break; // when ACK is received
}
rIICSTAT=0xd0; //stop MasTx condition
rIICCON=0xaf; //resumes IIC operation.
Delay (1); //wait until stop condtion is in effect.
//write is completed.
}

void Rd24C040 (U32 slvAddr, U8 addr, U8 *data)
{
_iicMode=SETRDADDR;
_iicPt=0;
_iicData [0] = (U8) addr;
_iicDataCount=1;
rIICDS=slvAddr;
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while (_iicDataCount != -1);
_iicMode=RDDATA;
_iicPt=0;
_iicDataCount=1;
rIICDS=slvAddr;
rIICSTAT=0xb0; //MasRx,Start
rIICCON=0xaf; //resumes IIC operation.
while (_iicDataCount  != -1);
*data=_iicData [1];
}

void Test_Iic2 (void)
{
unsigned int i, j, save_E, save_PE;
static U8 data [256];
Uart_Printf ("IIC Test (Polling) using AT24C02\n");
save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON &= ~0xf0000000;
rGPECON |= 0xa0000000; //GPE15:IICSDA, GPE14: IICSCL
//Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16    ;
rIICCON = (1<<7) | (1<<6) | (1<<5) |(0xf) ; //0xe0
rIICADD = 0x10; //2410 slave address = [7:1]
rIICSTAT = 0x10; //IIC bus data output enable (Rx/Tx)
Uart_Printf ("Write test data into AT24C02\n");
for (i=0; i<256; i++)
Wr24C040(0xa0, (U8) i, i);
for (i=0; i<256; i++)
data[i] = 0;
Uart_Printf ("Read test data from AT24C02\n");
for (i=0; i<256; i++)
Rd24C040 (0xa0, (U8) i, & (data[i]));
for (i=0; i<8; i++)
{
for (j=0; j<8; j++)
Uart_Printf ("%2x ", data [i*8+j]);
Uart_Printf ("\n");
}
Uart_Printf ("OK! Write data is same to Read data! \n");
rGPEUP = save_PE;
rGPECON = save_E;
}

void IIC_test (void)
{
Test_Iic2 (); //Polling mode
puts ("Press any key to continue...\n");
getch ();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -