/*******************************************************************************
* Device      : AD18F06A
* Author      :
* Version     :
* Date        :
* Description :
* Note		  :
********************************************************************************/

/********************************INCLUDES*************************************/
#include <stdio.h>
#include "AD18F06A.h"

__CONFIG(1,_FINTOSC_DIV1_1L   & _LVREN_OFF_1L      & _LVR24V_1L                                                                                          );
__CONFIG(2,_FLASH_CP7_OFF_2L  & _FLASH_CP6_OFF_2L  & _FLASH_CP5_OFF_2L   & _FLASH_CP4_OFF_2L   & _FLASH_CP3_OFF_2L
         & _FLASH_CP2_OFF_2L  & _FLASH_CP1_OFF_2L  & _FLASH_CP0_OFF_2L   & _CRYLEN_OFF_2L      & _CRYHEN_OFF_2L
  	     & _IDIS_ON_2L        & _CKSUM_OFF_2L      & _CBP_OFF_2L         & _RESETE_OFF_2L      & _RSEL_LOW_2L     & _STV_ON_2L              	         );
__CONFIG(3,_DBG_OFF_3L        & _PSUT_8_3L         & _FCPU_2T_3L         & _WDTEN_OFF_3L       & _TWDT_011_3L                                            );
__CONFIG(4,_OSCM_HIRC_16M_4L  & _ENVDDL_ON_4L      & _EEDIS_OFF_4L       & _FLDIS_OFF_4L       & _FCMEN_OFF_4L    & _IESO_OFF_4L&  _DBGS_IOSEL_1_4L      );



#define N 17
#define	ADDR0 (0x20>>1)		//0x20
#define	ADDR1 (0x70>>1)		//0xa0

volatile unsigned char Buf[N];
volatile unsigned char i          = 0;
volatile unsigned char Frame_Head = 0;
volatile unsigned char Cnt        = 0;
volatile unsigned char Rec_Flag   = 0;
volatile unsigned char TempData   = 0;

void putch(unsigned char tmp)
{
	while(!TXIF)continue;
	TXREG = tmp;
	TXIF = 0;
}

unsigned char getch(void)
{
	while(!RCIF)continue;
	RCIF = 0;
	return RCREG;
}
//SDA ->PA0 SCL->PA1 UART ->PB0 BPS ->9600
void main(void)
{
	//TX_PB0_RX_PB1@9600
	UART0CLKEN = 1;
    TX0_MAP    = MAP_PB0;
    RX0_MAP    = MAP_PB1;
	TRISB0     = 0;
	TRISB1     = 1;
	UARTCON    = 0x00;
	SPBRG      = 0x1A;
	RCSTA      = 0x90;
	TXSTA      = 0x20;

	printf("***********************************\r\n");
	printf("*             AD18F06_IIC_TEST     *\r\n");
	printf("***********************************\r\n");
	printf("                                   \r\n");

	I2CCLKEN    = 1;
	TRISA0 = 0;PA0PU = 0;PA0PD = 1;PA0OD = 1;PA0AEN = 0;
	TRISA1 = 0;PA1PU = 0;PA1PD = 1;PA1OD = 1;PA1AEN = 0;
    SDA_MAP     = MAP_PA0;
    SCL_MAP     = MAP_PA1;

	I2CADD0     = ADDR0|0X80;  //设置并使能ADDR0
	I2CADD1     = ADDR1|0X80;  //设置并使能ADDR1   slave-从器件地址（7bit或另一10bit 地址高4bit）
	I2CCON0     = 0X20;	       //使能并将SDA和SCL正确配置为输入输出 ,必须发START信号发送与接收的地址才可以匹配

	I2C_STATRIF = 0;
	I2C_RCIF    = 0;
	I2C_STOPIF  = 0;

	I2C_RCIE    = 1;	       //接收中断使能
	I2C_RCIP    = 1;	       //接收中断使能

	I2C_STATRIE = 1;	       //地址匹配 开始
	I2C_STATRIE = 1;	       //接收中断使能

	I2C_STOPIE  = 1;	       //停止信号
	I2C_STOPIP  = 1;	       //停止信号

	IPEN        = 1;	       //使能中断优先级
	GIEL        = 0;           // 外设GIEL
	GIEH        = 1;           // 全局GIEH

	while(1)
	{
		if(Rec_Flag == 1)
		{
			Rec_Flag = 0;
			printf("Slave Received:");
			for(i = 0;i <N ;i++)
			{
				printf(" 0x%02X, ",Buf[i]);
			}
			printf("\r\n");
		}
		if(Frame_Head == 3)
		{
			Frame_Head   = 0;
			for(i = 1;i < N;i++)
			{
				while(I2C_TXIF == 0) ;	//等待发送完成
				I2C_TXIF = 0;
				I2CTX    = Buf[i];	    //ch放入发送缓冲区

				while(ACKGET == 0) ;    //等待主机响应
				ACKGET   = 0;
			}
			while(I2C_STOPIF==0);
			I2C_TXIF = 0;

			printf("Slave Return  :");
			for(i = 0; i < N; i++)printf(" 0x%02X, ",Buf[i]);
			printf("\r\n");
		}
	}
}

void interrupt ISR_h (void)
{
	if(I2C_STATRIE == 1 && I2C_STATRIF == 1)	//地址匹配 开始
	{
		I2C_STATRIF = 0;
		Frame_Head  = 1;	                    //标志位_开始接收
		Cnt         = 0;
		I2CTX       = Buf[0];	                //预先填入要发送的第一个数据
	}

	if(I2C_RCIE == 1 && I2C_RCIF == 1)	        //从机接收中断
	{
		I2C_RCIF    = 0;
		TempData    = I2CRC;

		if(Frame_Head == 2)
		{
			if(Cnt < N-1)	                    //N为要接收的数目
			{
				Buf[Cnt++] = TempData;

				ACKEN      = 0;	                //从机应答
			}else
			{
				Buf[Cnt++] = TempData;
				Cnt        =0;
				Rec_Flag   =1;
			}
		}

		if(Frame_Head == 1)	                    //首次接收且地址匹配
		{
			if(R == 0)
			{
				Frame_Head = 2;	                //标志位_从机开始接收
			}else
			{
				Frame_Head = 3;	                //标志位_从机开始发送
			}
			Cnt   = 0;		                    //接收数据计数清零
			ACKEN = 0;	                        //从机应答
		}
	}

	if(I2C_STOPIE == 1 && I2C_STOPIF == 1)	    //从机发送中断
	{
		I2C_STOPIF = 0;
		Frame_Head = 0;
		ACKEN      = 0;	                        //从机应答
	}
}

void interrupt low_priority ISR_l (void)
{

}
