#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#define EEPROM_SLA 0xA0 //AT24C02
UINT8 data_received[64]= 0; //change to 256 if the rom space is enough
UINT8 data_num = 0;
UINT8 irxdata =0;
UINT8 isubaddr =0;
void I2C_ISR(void) interrupt 6
{
static UINT8 islavecount=0;
switch (I2STAT)
{
case 0x00:
STO = 1;
break;
case 0x60:
AA = 1;
islavecount=0;
break;
case 0x68:
P02 = 0;
while(1);
break;
case 0x80:
if(islavecount==0)
{
isubaddr = I2DAT;
AA = 1;
islavecount++;
data_num = isubaddr;
}
else
{
irxdata = I2DAT;
AA = 1;
data_received[data_num++]=irxdata;
islavecount++;
}
break;
case 0x88:
data_received[data_num] = I2DAT;
data_num = 0;
AA = 1;
break;
case 0xA0:
AA = 1;
break;
case 0xA8:
data_num=isubaddr;
I2DAT = data_received[data_num];
data_num++;
AA = 1;
break;
case 0xB8:
I2DAT = data_received[data_num];
data_num++;
AA = 1;
break;
case 0xC0:
AA = 1;
break;
case 0xC8:
AA = 1;
break;
}
SI = 0;
}
void Init_I2C_SLAVE(void)
{
P13_Quasi_Mode; //set SCL (P13) is Quasi mode
P14_Quasi_Mode; //set SDA (P14) is Quasi mode
SDA = 1; //set SDA and SCL pins high
SCL = 1;
//--set_P0SR_6; //set SCL (P06) is Schmitt triggered input select.
set_EI2C; //enable I2C interrupt by setting IE1 bit 0
set_EA;
I2ADDR = EEPROM_SLA; //define own slave address
set_I2CEN; //enable I2C circuit
set_AA;
}
void main(void)
{
Set_All_GPIO_Quasi_Mode;
Init_I2C_SLAVE();
while (1);
}
|