#ifndef _MAIN_C_
#define _MAIN_C_
/*********************************************************************************************************************/
#include "include/ca51f_config.h"		
#include "include/ca51f2sfr.h"
#include "include/ca51f2xsfr.h"
#include "include/gpiodef_f2.h"
#include "include/system_clock.h"

#include "include/uart.h"
#include "include/spi.h"
#include "include/delay.h"
#include <intrins.h>
/*********************************************************************************************************************/
//  本例程为SPI主机从机通信的从机程序，从机接收数据。  
//   ____________            _____________ 
//  |            |   SSB    |             |
//  |            |<-------->|             |
//  |            |   SCK    |             |
//  |  CA51F2(M) |<-------->|  CA51F2(S)  |
//  |(SPI_Master)|   MOSI   | (SPI_Slave) |
//  |            |<-------->|             |
//  |            |	 MISO		|             |
//  |            |<-------->|             |
//  |____________|          |_____________|
//
/*********************************************************************************************************************/
unsigned char spi_int_flag;
void INT5_ISR (void) interrupt 10 	   
{
	if(SPSTA & SPIF)					 //SPI  interrupt
	{
		SPSTA |= SPIF;
		spi_int_flag = 1;
	}	
	if(SPSTA&MODF)
	{
		SPSTA|=MODF;
		
	}			
}
void main(void)
{
#ifdef LVD_RST_ENABLE
	LVDCON = 0xE1;					//设置LVD复位电压为2V
#endif
#if (SYSCLK_SRC == PLL)
	Sys_Clk_Set_PLL(PLL_Multiple);	//设置系统时钟为PLL，PLL_Multiple为倍频倍数
#endif
	
#ifdef UART0_EN
	Uart0_Initial(UART0_BAUTRATE);	//初始化UART0
#endif
	
#ifdef UART1_EN
	Uart1_Initial(UART1_BAUTRATE);	//初始化UART1
#endif

#ifdef UART2_EN
	Uart2_Initial(UART2_BAUTRATE);	//初始化UART2
#endif
	
	EA = 1;													//开全局中断

#ifdef PRINT_EN
	uart_printf("SPI Slave Demo Code\n");
#endif

	GPIO_Init(P64F,P64_SPI_SCK_SETTING);
	GPIO_Init(P62F,P62_SPI_MISO_SETTING);
	GPIO_Init(P63F,P63_SPI_MOSI_SETTING);
	GPIO_Init(P65F,P65_SPI_SSB_SETTING);
	SPCON =	SPEN(1) | LSBF(0) | SSIG(0) | MSTR(0) | CPOL(0) | CPHA(0) |CPOS(0);					   //高位先发, SSB有效，从机, 1/4系统时钟
	SPSTA |= SPIE;	
	INT5EN = 1;
	spi_int_flag = 0;

	while(1)
	{	
		if(spi_int_flag)
		{
			spi_int_flag=0;
			Uart0_PutChar(SPDAT);	
		} 	
	}
}
#endif


