i2c_master.c

来自「ADuC7020/26是ADI模拟公司开发的ARM7TDMI内核」· C语言 代码 · 共 97 行

C
97
字号
/***************************************************************************

 Author        : ADI - Apps                    www.analog.com/MicroConverter

 Date          : Sept. 2005

 File          : I2C_Master.c

 Hardware      : Applicable to ADuC702x rev H or I silicon
                 Currently targetting ADuC7026.

 Description   : I2C master	to demonstrate with I2C_Slave.c
				 
				 Operates in two modes, read & write (called recieve and 
				 transmit here). At the begining of an I2C transmission, the 
				 Master sends an address. The LSB of this address determines 
				 if the Master is to read (1) or write (0).

		
***************************************************************************/

#include<ADuC7020.h>				   

void delay(int);
void IRQ_Handler() __irq;

#define count 0x4;					// Number of bytes to be recieved - 1
int i = 0, dat[5];					// Size of dat should be (count + 1)


int main()
{
 	
 	GP1CON = 0x22;					// I2C on P1.0 and P1.1

	I2C0CFG = 0x82;		  			// Master Enable & Enable Generation of Master Clock
	
	// I2C-Master setup
 	I2C0DIV = 0xCFCF;				// 0x3232 = 400kHz
									// 0xCFCF = 100kHz	

	IRQEN = 0x400;					// I2C0 Master Interupt

	// Transmit
	I2C0ADR = 	0xA0;	 			// set i2c address	 (LSB = 0, Master Write)
	I2C0MTX =   0x55;					// send i2c byte address
	 
	
	delay(4000);	 
		 
	// Recieve
	i = 0;
	I2C0CNT = 	count;				// Number of bytes to be read from slave
	I2C0ADR = 	0xA1;	 			// set i2c address	 (LSB = 1, Master Read)	
	
	while (1)
	{
	};

 	return 0;
}




void delay (int length)
{
	while (length >0)
    	length--;
}



/*************************************************/
/*************************************************/
/************	IRQ Service Routine  *************/
/*************************************************/
/*************************************************/

void IRQ_Handler() __irq
{
	// Transmit
	if(((I2C0MSTA & 0x4) == 0x4) && (i < 8))	// Master Transmit IRQ	
	{
		i++;								    // Transmits numbers 1-8
		I2C0MTX = i;
	}		


	// Recieve
	if((I2C0MSTA & 0x8) == 0x8)				    // Master Recieve IRQ
	{
		dat[i] = I2C0MRX;
		i++;
	}
}

⌨️ 快捷键说明

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