#include "include.h"
#define    R_IIC_ID    0x7E		//从机地址
u8 receive_data;			//IIC数据
/*
BL08M003-IIC-Demo
注意：
1、仿真无法使用PA0/PA1  为通信IO口
2、仿真IIC时，SDA_MAP选择PORTC6  SCL_MAP选择PORTC5 否则无法仿真IIC 烧录到芯片IIC功能正常
*/
void IIC_Write(u8 Adress,u8 IIC_Data)  		//主机写数据
{
	//主机发送模式
	STA = 1;
	IICIF = 0;
	while(!IICIF);

	STA = 0;
	IICDAT = R_IIC_ID;    ////地址
	IICIF = 0;
	while(!IICIF);
	
	IICDAT = Adress;   //发送数据地址
	IICIF = 0;
	while(!IICIF);

	IICDAT = IIC_Data; //发送数据
	IICIF = 0;
	while(!IICIF);

	STO = 1;
	IICIF = 0;
}

unsigned char IIC_Read(u8 Adress)	//主机读数据
{
	u8 IIC_Data;
	STA = 1;
	IICIF = 0;
	while(!IICIF);
	
	STA = 0;
	IICDAT = R_IIC_ID;    ////地址
	IICIF = 0;
	while(!IICIF);
	
	IICDAT = Adress;   //发送数据地址
	IICIF = 0;
	while(!IICIF);
		
	STA = 1;
	IICIF = 0;		//重新开始信号
	while(!IICIF);
	
	STA = 0;
	AA = 0;
	IICDAT = R_IIC_ID + 1;    ////地址+读
	IICIF = 0;
	while(!IICIF);

	AA = 0;
	IICIF = 0;			//等待读取成功，发送非应答
	while(!IICIF);
	IIC_Data = IICDAT;		//接收数据
	AA = 0;
	STO = 1;
	IICIF = 0;
		
	return IIC_Data;
}

void main(void)
{
	////初始化函数 
	System_Int();
	
	GIE = 1;   ///使能总中断

	while(1)
	{
//		asm("clrwdt");   		//注意WDT是否开启
		IIC_Write(0xD0,0x55);	//从机0xD0地址写0x55
		receive_data = IIC_Read(0xD0);			//读从机地址0xD0数据

		Delay_Us(500);	
	}
}
///中断服务函数
void interrupt Timer0_Isr(void)
{

}