问题:现在想用N76E003当做IIC从机接受乐高EV3(主机)发送来的IIC数据。用示波器看到iic总线数据是正常的,尤其是地址位!如下图
主机是每隔3s发送一次数据的。
然后从机使用的是官方V1.06版本里的从机代码,加入了一个中断标志位(UINT8 recvFlag=0;)作为进入中断的查看,但是我发现配置好之后程序一直没有进入过中断?哪位大神有调试成功的IIC从机程序可以借来参考下,谢谢!邮箱:490054348@qq.com
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technoledge Corp.
// Website: http://www.nuvoton.com
// E-Mail : MicroC-8bit@nuvoton.com
// Date : May/1/2015
//***********************************************************************************************************
//***********************************************************************************************************
// File Function: N76E003 I2C Slave demo code
//***********************************************************************************************************
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
//***********************************************************************************************************
// N76E885-series I2C slave mode demo code, the Slave address = 0xA4
//
// ____________ _____________
// | | SDA | |
// | |<-------->| |
// | | | |
// |N76E003(M) | | N76E003(S) |
// |(I2C_Master)| | (I2C_Slave) |
// | | SCL | |
// | |--------->| |
// |____________| |_____________|
//
// The protocol of I2C is same the "24LC64"
//***********************************************************************************************************
#define I2C_CLOCK 13
#define EEPROM_SLA 0x02
UINT8 data_received[34], data_num = 0;
UINT8 recvFlag=0;
void Init_I2C(void);
void Bsp_delay10us(void)
{
UINT8 i=0;
for(i=0;i<18;i++)
{
_nop_();
}
}
void Bsp_delayms(int ms)
{
int i=0,j=0;
for(i=0;i<ms;i++)
{
for(j=0;j<100;j++)
{
Bsp_delay10us();
}
}
}
//========================================================================================================
void I2C_ISR(void) interrupt 6
{
recvFlag=1;
switch (I2STAT)
{
case 0x00:
STO = 1;
break;
case 0x60:
AA = 1;
//P3 = 0x60;
break;
case 0x68:
P02 = 0;
while(1);
break;
case 0x80:
//P3 = 0x80;
data_received[data_num] = I2DAT;
data_num++;
if (data_num == 34)
AA = 0;
else
AA = 1;
break;
case 0x88:
//P3 = 0x88;
data_received[data_num] = I2DAT;
data_num = 0;
AA = 1;
break;
case 0xA0:
//P3 = 0xA0;
AA = 1;
break;
case 0xA8:
//P3 = 0xA0;
I2DAT = data_received[data_num];
data_num++;
AA = 1;
break;
case 0xB8:
//P3 = 0xB8;
I2DAT = data_received[data_num];
data_num++;
AA = 1;
break;
case 0xC0:
AA = 1;
break;
case 0xC8:
//P3 = 0xC8;
AA = 1;
break;
//ywm
default:
Init_I2C();
break;
}
SI = 0;
clr_I2TOF;
}
//========================================================================================================
void Init_I2C(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_P1SR_3;
set_EI2C; //enable I2C interrupt by setting IE1 bit 0
set_EA;
// I2c¶¨Ê±Æ÷
//set_I2TOCEN;
//set_DIV;
//clr_DIV;
//clr_I2TOF;
I2ADDR = EEPROM_SLA; //define own slave address
set_I2CEN; //enable I2C circuit
set_AA;
}
//========================================================================================================
void main(void)
{
UINT8 i=0;
Set_All_GPIO_Quasi_Mode;
/* Initial I2C function */
Init_I2C(); //initial I2C circuit
for(i=0;i<3;i++)
{
clr_GPIO1;// Tiny board GPIO1 LED define
set_GPIOFMQ;
Bsp_delayms(100);
set_GPIO1;
clr_GPIOFMQ;
Bsp_delayms(100);
}
while (1)
{
if(recvFlag == 1)
{
set_GPIOFMQ;// Tiny board GPIO1 LED define
Bsp_delayms(1000);
clr_GPIOFMQ;
Bsp_delayms(1000);
recvFlag=0;
}
clr_GPIO1;// Tiny board GPIO1 LED define
Bsp_delayms(100);
set_GPIO1;
Bsp_delayms(100);
}
/* =================== */
}
|