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

📄 i2c.h

📁 DSP的I2C模块C程序
💻 H
字号:
/*********************************************************************************
* I2C.H	ver1.0	     							                            *
* Copyright	2008 by Hunan Geoson Hi-Tech Ltd.
* All rights reserved. Property of Hunan Geosun Hi-Tech Ltd.			                *
* Write by:	Zhili.Zeng
* Email:	zzl290@163.com								                                *
*********************************************************************************/ 


/*********************************************************************************
Bit Polling Mode.
Writting and reading operation on I2C device. 	
*********************************************************************************/


/********************************************************************************
Define  Registers Related to I2C;
********************************************************************************/		
int *I2COAR0;	//0x01B40000
int *I2CSAR0;   //0x01B4001C
int *I2CIER0;	//0x01B40004
int *I2CPSC0;   //0x01B40030
int *I2CCLKL0;	//0x01B4000C
int *I2CCLKH0;	//0x01B40010
int *I2CMDR0;	//0x01B40024
int *I2CSTR0;	//0x01B40008
int *I2CCNT0;	//0x01B40014
int *I2CDXR0;	//0x01B40020
int *I2CDRR0;	//0x01B40018			
					


/*********************************************************************************
The function below is used to set I2C module.

**********You may modify the value of I2CPSC\I2CCLKH\I2CCLKL before use.**********
****These registers are used to set data transfer rate,from 10kbps to 400kbps.****
 
*********************************************************************************/
void I2C_Init(void)
{
	/*Specify the address of the I2C related registers*/
	I2COAR0		=(int *)0x01B40000;
	I2CSAR0		=(int *)0x01B4001C;
	I2CIER0		=(int *)0x01B40004;
	I2CPSC0		=(int *)0x01B40030;
	I2CCLKL0	=(int *)0x01B4000C;
	I2CCLKH0	=(int *)0x01B40010;
	I2CMDR0		=(int *)0x01B40024;
	I2CSTR0		=(int *)0x01B40008;
	I2CCNT0		=(int *)0x01B40014;
	I2CDXR0		=(int *)0x01B40020;
	I2CDRR0		=(int *)0x01B40018;

    *I2COAR0	=	0x10;	//Set master address  ?????????????
    /*assume this address is 0x10*/
    
	*I2CIER0	=	0;		//Unenable I2C interrupts
	*I2CPSC0	=	0x0e;	//Fp=CLK/(I2CPSC+1)
	/*Fp must between the  frequency of 6.7 MHz and 13.3 MHz.*/
	*I2CCLKL0	=	0x0e;	
	*I2CCLKH0	=	0x0e;	//Fi2c=Fp/((I2CCLKL+d)+(I2CCLKH+d))
	/*	d equals 7 when I2CPSC equals 0,
		d equals 6 when I2CPSC equals 1,
		d equals 5 in other conditions. */
	
	*I2CMDR0	=	0x4620;	//Set i2c mode.Free\Master\Send\Nonrepeat\Byte Mode
				//The function bits of I2CMDR
				//FREE-14	STT-13	STP-11	MST-10	TRX-9
				//XA-8	RM-7	DLB-6	IRS-5	
				//STB-4	FDF-3	BC -  2-0
}


/*********************************************************************************
The function below is used to write data to a i2c device.
add is the memory you want to access.
DataSend contains the data you want to send.
length is the number you want to access a time.
**********************************************************************************
*******************************             **************************************
*******************************   Warning   **************************************
*******************************             **************************************
**********************************************************************************
		The low five bits of memory address added length 
		must smaller than 32 when writtint 24LC64.
***********	 (add&0x1F + length) <32 	in function I2C_WriteData().  ************
*********************************************************************************/
void I2C_WriteData(unsigned short add,unsigned char *DataSend,unsigned char length)
{	
	
	*I2CSAR0	=	I2C_SLAVE_ADDRESS;		//Set slave address
	*I2CCNT0	=	length+2;		//The number of bytes to send is length 

	while(*I2CSTR0&0x1000);			//Wait for i2c bus free
	*I2CMDR0	=	0x6e20;	 //Set i2c mode.Free\STT\STP\Master\Send\Nonrepeat\Byte Mode
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	(add&0xff00)>>8;	//Send the high eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	add&0x00ff;	//Send the low eight bits of address
	while(length--)
	{
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	*DataSend++;		//Send a byte
	}
	//while(*I2CSTR0&0x1000);			//Wait for i2c bus free

}



/*********************************************************************************
The function below is used to read data from a i2c device.
add is the memory you want to access.
DataRec contains the data you read.
length is the number you want to access a time.
*********************************************************************************/
void I2C_ReadData(unsigned short add,unsigned char *DataRec,unsigned char length)
{

	*I2CSAR0	=	I2C_SLAVE_ADDRESS;//Set slave address
	*I2CCNT0	=	2;		
	while(*I2CSTR0&0x1000);	//Wait for i2c bus free
	*I2CMDR0	=	0x6620;	//Set i2c mode.Free\STT\STP\Master\Send\Nonrepeat\Byte Mode

	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	(add&0xff00)>>8;	//Send the high eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	add&0x00ff;	//Send the low eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	
	*I2CCNT0	=	length;		//The number of bytes to receive is length
	*I2CMDR0	=	0x6c20;//Set i2c mode.Free\STT\STP\Master\Receive\Nonrepeat\Byte Mode

	while(length--)
	{
		while(!(*I2CSTR0&0x0008));//Wait for data receive ready
		*DataRec++=*I2CDRR0;//Save the received data to DataRec
	}
	//while(*I2CSTR0&0x1000);			//Wait for i2c bus free
	
}



/*********************************************************************************
The function below is used to write a byte to a i2c device.
add is the memory you want to access.
ByteSend contains the byte you want to send.
*********************************************************************************/
void I2C_WriteByte(unsigned short add,unsigned char *ByteSend)
{	
	
	*I2CSAR0	=	I2C_SLAVE_ADDRESS;		//Set slave address
	*I2CCNT0	=	3;		//The number of bytes to send is 1 

	while(*I2CSTR0&0x1000);			//Wait for i2c bus free
	*I2CMDR0	=	0x6e20;	 //Set i2c mode.Free\STT\STP\Master\Send\Nonrepeat\Byte Mode
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	(add&0xff00)>>8;	//Send the high eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	add&0x00ff;	//Send the low eight bits of address

	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	*ByteSend;		//Send a byte

	//while(*I2CSTR0&0x1000);			//Wait for i2c bus free

}



/*********************************************************************************
The function below is used to read a byte from a i2c device.
add is the memory you want to access.
ByteRec contains the byte you read.
*********************************************************************************/
void I2C_ReadByte(unsigned short add,unsigned char *ByteRec)
{

	*I2CSAR0	=	I2C_SLAVE_ADDRESS;//Set slave address
	*I2CCNT0	=	2;		
	while(*I2CSTR0&0x1000);	//Wait for i2c bus free
	*I2CMDR0	=	0x6620;	//Set i2c mode.Free\STT\STP\Master\Send\Nonrepeat\Byte Mode

	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	(add&0xff00)>>8;	//Send the high eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	*I2CDXR0	=	add&0x00ff;	//Send the low eight bits of address
	while(!(*I2CSTR0&0x0010));	//Wait for I2CDXR is ready
	
	*I2CCNT0	=	1;		//The number of bytes to receive is 1
	*I2CMDR0	=	0x6c20;//Set i2c mode.Free\STT\STP\Master\Receive\Nonrepeat\Byte Mode

	while(!(*I2CSTR0&0x0008));//Wait for data receive ready
	*ByteRec=*I2CDRR0;//Save the received data to ByteRec

	//while(*I2CSTR0&0x1000);			//Wait for i2c bus free
	
}


/*********************************************************************************
    End of I2C.H
*********************************************************************************/

⌨️ 快捷键说明

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