/**
  ******************************************************************************
  * @file GPIO/IOToggle/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version  V3.0.0
  * @date  04/06/2009
  * @brief  Main Interrupt Service Routines.
  *         This file provides template for all exceptions handler and 
  *         peripherals interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"	 
#include "stm32f10x_exti.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "pbdata.h"

void NMI_Handler(void)
{
}

void USART1_IRQHandler(void)
{
   if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
   {
		USART_SendData(USART1,USART_ReceiveData(USART1));
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
   }
}


/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval : None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval : None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval : None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval : None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval : None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval : None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval : None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval : None
  */
void SysTick_Handler(void)
{
}

void TIM3_IRQHandler(void)
{
	

}
/****************************************************************************
* 名    称：void EXTI9_5_IRQHandler(void)
* 功    能：EXTI9-5中断处理程序
* 入口参数：无
* 出口参数：无
* 说    明：
* 调用方法：无 
****************************************************************************/
void EXTI9_5_IRQHandler(void)
{
}

/****************************************************************************
* 名    称：void EXTI1_IRQHandler(void)
* 功    能：EXTI2中断处理程序
* 入口参数：无
* 出口参数：无
* 说    明：
* 调用方法：无 
****************************************************************************/
void EXTI1_IRQHandler(void)
{
	
}

/****************************************************************************
* 名    称：void EXTI2_IRQHandler(void)
* 功    能：EXTI2中断处理程序
* 入口参数：无
* 出口参数：无
* 说    明：
* 调用方法：无 
****************************************************************************/
void EXTI2_IRQHandler(void)
{
    
}

/****************************************************************************
* 名    称：void EXTI3_IRQHandler(void)
* 功    能：EXTI3中断处理程序
* 入口参数：无
* 出口参数：无
* 说    明：
* 调用方法：无 
****************************************************************************/
void EXTI3_IRQHandler(void)
{
   
}

u8 Get_CMOS_Data(void)
{
	u8 temp;

	temp=0;
	temp=temp<<1;//7

	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//6
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//5
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//4
	if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//3
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//2
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//1
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)!=Bit_RESET)
	{
	   temp++;
	}

	temp=temp<<1;//0
	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)!=Bit_RESET)
	{
	   temp++;
	}	
	return 	temp;
}


u16 flag=0;
void EXTI4_IRQHandler(void)
{	 
	u16 i,j,t1,t2; 

	if(flag==0)    //FIFO写指针复位
	{
	    flag=1;
		FIFO_WRST_H;
		FIFO_WRST_L;
 		for(i=0;i<100;i++);
		FIFO_WRST_H;      
		FIFO_WR_H;	   //允许CMOS数据写入FIFO		
	}else if(flag==1)
	{
	 	FIFO_WR_L;     //禁止CMOS数据写入FIFO
		EXTI->IMR&=~(1<<4);	 //禁止外部中断，准备从FIFO中取数据
		EXTI->EMR&=~(1<<4);

		FIFO_RRST_L;  //FIFO读指针复位 
		FIFO_RCK_L;                
		FIFO_RCK_H;	
		FIFO_RCK_L;
		FIFO_RCK_H;
	  	FIFO_RRST_H;

		LCD_WR_CMD(0x0003,0x1028);//110//图像显示方向		

		LCD_WR_CMD(0x0050, 0); 					//水平显示区起始地址 0-239
		LCD_WR_CMD(0x0051, 239);           		//水平显示区结束地址 0-239
		LCD_WR_CMD(0x0052, 0);     			    //垂直显示区起始地址 0-399
		LCD_WR_CMD(0x0053, 319);         		//垂直显示区结束地址 0-399
		LCD_WR_CMD(32, 0);		          	//水平显示区起始地址
  		LCD_WR_CMD(33, 319);		   		//垂直显示区起始地址
  		LCD_WR_REG(34);				        //写数据到显示区

		FIFO_OE_L;			  //允许FIFO输出  	

		for(i = 0; i < 9600; i ++)	 //该开发板配套的TFT3.2屏最大显示尺寸320*240
		 {	
		 	for(j = 0; j < 8; j ++)
			{											
				FIFO_RCK_L;					
				FIFO_RCK_H;		 				
				t1=Get_CMOS_Data();
				FIFO_RCK_L;						
				FIFO_RCK_H;	
				t2=Get_CMOS_Data();
				LCD_WR_Data(t1<<8|t2); 		//将FIFO中的16位数据写入显示区 	  							   	
			}			
		}
		FIFO_OE_H;		 	  //禁止FIFO输出
		flag=0;	   
		EXTI->IMR|=(1<<4);	  //允许外部中断，以便接收下帧图像数据
		EXTI->EMR|=(1<<4);
	} 		

	EXTI_ClearITPendingBit(EXTI_Line4);
}
