关于ARM的IIC串行通信的几个问题!请高手解答 急~~~
[复制链接]
程序的主要目的是将0~0xff这256个数按顺序写入到EEPROM(AT24C16)的内部存储单元中,然后再依次将他们读出,并通过实验室板的串口UART0输出到PC机上运行的超级终端上。
程序如下:
#include
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "2410IIC.h"
static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;
//===================================================================
// SMDK2410 IIC configuration
// GPE15=IICSDA, GPE14=IICSCL
// "Interrupt mode" for IIC block
//===================================================================
//******************[ Test_Iic ]**************************************
void Test_Iic(void)
{
unsigned int i,j,save_E,save_PE;
static U8 data[256];
Uart_Printf("[ IIC Test(Interrupt) using AT24C16 ]\n");
save_E = rGPECON;
save_PE = rGPEUP;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON |= 0xa00000; //GPE15:IICSDA , GPE14:IICSCL
//Enable interrupt
pISR_IIC = (unsigned)IicInt;
rINTMOD=0x0;
rINTMSK &= ~(BIT_IIC);
//initialize iic Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
// If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
rIICCON = 0xaf; //(1<<7) | (0<<6) | (1<<5) | (0xf);
rIICADD = 0x10; //2410 slave address = [7:1]
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)
Uart_Printf("\nPlease input the start number will be writed to EEPROM: ");
j = Uart_GetIntNum();
Uart_Printf("\n");
Uart_Printf("Start number will be %d\n",j);
//Write the data to EEPROM
Uart_Printf("Write test data into AT24C16\n");
for(i=0;i<256;i++)
{
Wr24C080(0xa0,(U8)i,j);
j++;
Uart_Printf(".");
}
Uart_Printf("\n");
//Clear the DataBuffer
for(i=0;i<256;i++)
data = 0;
//Read the data from EEPROM
Uart_Printf("Read test data from AT24C16\n");
for(i=0;i<256;i++)
Rd24C080(0xa0,(U8)i,&(data));
// printf read data
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
Uart_Printf("%2x ",data[i*16+j]);
Uart_Printf("\n");
}
rINTMSK |= BIT_IIC;
rGPEUP = save_PE;
rGPECON = save_E;
}
//*************************[ Wr24C080 ]****************************
void Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
_iicMode = WRDATA
_iicPt = 0;
_iicData[0] = (U32)addr;// 我把U8改成了U32
_iicData[1] = data;
_iicDataCount = 2;
// send control byte
rIICDS = slvAddr; //0xa0 移位寄存器,写使能
rIICSTAT = 0xf0; //MasTx,Start 控制状态寄存器
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=2);//???????为什么一定要这条。
_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
}
//end send
rIICSTAT = 0xd0; //Stop MasTx condition 1101
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
//**********************[ Rd24C080 ] ***********************************
void Rd24C080(U32 slvAddr,U32 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 = addr;//slvAddr;
rIICSTAT = 0xb0; //MasRx,Start
rIICCON = 0xaf; //Resumes IIC operation.
while(_iicDataCount!=-1);
*data = _iicData[1];
}
以下问题不明白:
1 _iicDataCount = 2;while(_iicDataCount!=2);//???????为什么要有这条语句。跟static volatile int _iicDataCount;有关系?另外还有 _iicStatus =0x100; while(_iicStatus==0x100);
2声明的这几个 静态全局变量作用在哪里呢?
3,写入函数void Wr24C080(U32 slvAddr,U32 addr,U8 data)怎么把数据写进去了?跪求解答~~~~~~~~~~~~~