i2c_slave.c
来自「ADuC系列芯片的源代码事例」· 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 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 + =
减小字号Ctrl + -
显示快捷键?