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

📄 iic.c

📁 8019测试源码for arm s3c44b0
💻 C
字号:
/****************************************************************************
【文  件  名  称】IIC.c
【功  能  描  述】三星S3C44B0X IIC
【程  序  版  本】2.0
【创建人及创建日期】龚俊//2002年12月3日13:23
【修改人及修改日期】龚俊//2003年3月22日17:14
****************************************************************************/

//***************************************************************************
#include <string.h>
#include "..\inc\option.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"
#include "..\inc\iic.h"

#define WRDATA	    (1)
#define POLLACK     (2)
#define RDDATA	    (3)
#define SETRDADDR   (4)

#define IICBUFSIZE 0x20

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

void __irq IicInt(void);
//***************************************************************************

/****************************************************************************
【功能说明】向IIC总线上指定地址的从器件特定地址写入数据
****************************************************************************/
void Wr24LCXXX(U32 slvAddr,U32 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 been cleared.
    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.
}
//***************************************************************************

/****************************************************************************
【功能说明】从IIC总线上指定地址的从器件特定地址中读出数据
****************************************************************************/
void Rd24LCXXX(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=slvAddr;
    rIICSTAT=0xb0; //MasRx,Start
    rIICCON=0xaf;  //resumes IIC operation.   
    while(_iicDataCount!=-1);

    *data=_iicData[1];
}
//***************************************************************************

/****************************************************************************
【功能说明】IIC中断服务程序
****************************************************************************/
void __irq IicInt(void)
{
    U32 iicSt,i;
    rI_ISPC=BIT_IIC;

    iicSt=rIICSTAT; 
    if(iicSt&0x8){} // when bus arbitration is failed.
    if(iicSt&0x4){} // when a slave address is matched with IICADD
    if(iicSt&0x2){} // when a slave address is 0000000b
    if(iicSt&0x1){} // when ACK isn't received

    switch(_iicMode)
    {
		case POLLACK:
		    _iicStatus=iicSt;
		    break;
	
		case RDDATA:
		    if((_iicDataCount--)==0)
		    {
				_iicData[_iicPt++]=rIICDS;
			    
				rIICSTAT=0x90;  //stop MasRx condition 
				rIICCON=0xaf;   //resumes IIC operation.
				Delay(1);	//wait until stop condtion is in effect.
						//too long time... 
				//The pending bit will not be set after issuing stop condition.
				break;    
		    }	     
		    _iicData[_iicPt++]=rIICDS;
					//The last data has to be read with no ack.
		    if((_iicDataCount)==0)
				rIICCON=0x2f;	//resumes IIC operation with NOACK.  
		    else 
				rIICCON=0xaf;	//resumes IIC operation with ACK
		    break;
	
		case WRDATA:
		    if((_iicDataCount--)==0)
		    {
				rIICSTAT=0xd0;	//stop MasTx condition 
				rIICCON=0xaf;	//resumes IIC operation.
				Delay(1);	//wait until stop condtion is in effect.
				//The pending bit will not be set after issuing stop condition.
				break;    
		    }
		    rIICDS=_iicData[_iicPt++];  //_iicData[0] has dummy.
		    for(i=0;i<10;i++);	    //for setup time until rising edge of IICSCL
			    rIICCON=0xaf;	    //resumes IIC operation.
		    break;
	
		case SETRDADDR:
		    //Uart_Printf("[S%d]",_iicDataCount);
		    if((_iicDataCount--)==0)
		    {
				break;  //IIC operation is stopped because of IICCON[4]    
		    }
		    rIICDS=_iicData[_iicPt++];
		    for(i=0;i<10;i++);  //for setup time until rising edge of IICSCL
			    rIICCON=0xaf;	    //resumes IIC operation.
		    break;
	
		default:
		    break;	  
    }
}
//***************************************************************************

/****************************************************************************
【功能说明】IIC总线测试程序
****************************************************************************/
void Test_Iic(void)
{
    unsigned int i,j,save_F,save_PF;
    static U8 data[256];

    Uart_Printf("\n【IIC总线测试-24LCXXX读写】\n");

    save_F=rPCONF;
    save_PF=rPUPF;
    rPCONF |=0xa;	//PF0:IICSCL, PF1:IICSDA
    rPUPF |=0x3;	//pull-up disable

    pISR_IIC=(unsigned)IicInt;
    rINTMSK=~(BIT_GLOBAL|BIT_IIC);

    rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xf);
    //Enable interrupt, IICCLK=MCLK/16, Enable ACK
    //40Mhz/16/(15+1) = 257Khz	
    rIICADD=0x10;   // S3C44B0X slave address
    rIICSTAT=0x10;

    for(i=0;i<256;i++)
		Wr24LCXXX(0xa0,(U8)i,i);
    for(i=0;i<256;i++)
		data[i]=0;
    Uart_Printf("已经将0到FF一共256个数按顺序写入24LCXXX!\n");

    Uart_Printf("下面是按顺序从24LCXXX中读出的数据:\n");
    for(i=0;i<256;i++)
		Rd24LCXXX(0xa0,(U8)i,&(data[i])); 

    for(i=0;i<16;i++)
    {
		for(j=0;j<16;j++)
		    Uart_Printf("%2x ",data[i*16+j]);
		Uart_Printf("\n");
    }
	Uart_Printf("\n");
    
    rPCONF=save_F;
    rPUPF=save_PF;
}
//***************************************************************************

⌨️ 快捷键说明

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