📄 i2c_slave.c
字号:
/***************************************************************************
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 My_IRQ_Function();
int i = 0, dat[9];
int main()
{
GP1CON = 0x22; // I2C on P1.0 and P1.1
IRQ = My_IRQ_Function; // Specify Interrupt Service Rountine
IRQEN = 0x200; // I2C0 Slave Interupt
I2C0CFG = 0x1; // Slave Enable
I2C0ID0 = 0xA0; // Slave ID
I2C0STX = 0x77;
while (1)
{
};
return 0;
}
/*************************************************/
/*************************************************/
/************ IRQ Service Routine *************/
/*************************************************/
/*************************************************/
void My_IRQ_Function()
{
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -