i2c_slave.c

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

C
96
字号
/***************************************************************************

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

 Date          : Sept. 2005

 File          : I2C_Slave.c

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

 Description   : I2C Slave to demonstrate with I2C_Master.
				 
				 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 IRQ_Handler() __irq;

int i = 0, dat[9];


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

	IRQEN = 0x200;					// I2C0 Slave Interupt
	
	I2C0CFG = 0x01;		  			// Slave Enable
	I2C0ID0 = 0xA0;					// Slave ID
	I2C0STX = 0x77;
	
	
	while (1)
	{
	}; 

 	return 0;
}



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


void IRQ_Handler() __irq
{
	// Slave Recieve
	if ((I2C0SSTA & 0x08)==0x08)   // Slave Recieve IRQ
	{
	 	   dat[i] = I2C0SRX;
		   i++;
	}

	
	// Slave Transmit
	if ((I2C0SSTA & 0x04)==0x04)   // Slave Transmit IRQ
	{
		 if(i > 4) 			 // Resetting value of i if it has been incremented by RX
		 {
		 	i = 0;
		 }
		 
		 i++;
		 switch (i)
		 {
			case 1:
				I2C0STX = 0x55;
			break;

			case 2:						
				I2C0STX = 0x33;
			break;

			case 3:						
				I2C0STX = 0xAA;
			break;

			case 4:						
				I2C0STX = 0x11;
			break;
		};
	}
}

⌨️ 快捷键说明

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