AD7606的並行採集

#include "bsp_ad7606.h"
#include "bsp_GPIO.h"

MYGPIO AD_Control_GPIO[] = 
{
	{GPIO_PORT_OS0,GPIO_PIN_OS0,OUTPUT},
	{GPIO_PORT_OS1,GPIO_PIN_OS1,OUTPUT},
	{GPIO_PORT_OS2,GPIO_PIN_OS2,OUTPUT},
	{GPIO_PORT_AD_RANGE,GPIO_PIN_AD_RANGE,OUTPUT},
	{GPIO_PORT_AD_CONA,GPIO_PIN_AD_CONA,OUTPUT},
	{GPIO_PORT_AD_CONB,GPIO_PIN_AD_CONB,OUTPUT},
	{GPIO_PORT_AD_RST,GPIO_PIN_AD_RST,OUTPUT},
	{GPIO_PORT_AD_RD,GPIO_PIN_AD_RD,OUTPUT},
	{GPIO_PORT_AD_CS,GPIO_PIN_AD_CS,OUTPUT},
	{GPIO_PORT_AD_BUSY,GPIO_PIN_AD_BUSY,INPUT},
	{GPIO_PORT_AD_FD,GPIO_PIN_AD_FD,INPUT}
};
MYGPIO AD_Data_GPIO[] = 
{
	{GPIO_PORT_AD_DB0,GPIO_PIN_AD_DB0,INPUT},
	{GPIO_PORT_AD_DB1,GPIO_PIN_AD_DB1,INPUT},
	{GPIO_PORT_AD_DB2,GPIO_PIN_AD_DB2,INPUT},
	{GPIO_PORT_AD_DB3,GPIO_PIN_AD_DB3,INPUT},
	{GPIO_PORT_AD_DB4,GPIO_PIN_AD_DB4,INPUT},
	{GPIO_PORT_AD_DB5,GPIO_PIN_AD_DB5,INPUT},
	{GPIO_PORT_AD_DB6,GPIO_PIN_AD_DB6,INPUT},
	{GPIO_PORT_AD_DB7,GPIO_PIN_AD_DB7,INPUT},
	{GPIO_PORT_AD_DB8,GPIO_PIN_AD_DB8,INPUT},
	{GPIO_PORT_AD_DB9,GPIO_PIN_AD_DB9,INPUT},
	{GPIO_PORT_AD_DB10,GPIO_PIN_AD_DB10,INPUT},
	{GPIO_PORT_AD_DB11,GPIO_PIN_AD_DB11,INPUT},
	{GPIO_PORT_AD_DB12,GPIO_PIN_AD_DB12,INPUT},
	{GPIO_PORT_AD_DB13,GPIO_PIN_AD_DB13,INPUT},
	{GPIO_PORT_AD_DB14,GPIO_PIN_AD_DB14,INPUT},
	{GPIO_PORT_AD_DB15,GPIO_PIN_AD_DB15,INPUT}
};









/* 設置過採樣率 */
#define OS0_1()		 GPIO_WriteBit(GPIO_PORT_OS0,GPIO_PIN_OS0,(BitAction )(1))
#define OS0_0()		 GPIO_WriteBit(GPIO_PORT_OS0,GPIO_PIN_OS0,(BitAction )(0))
#define OS1_1()		 GPIO_WriteBit(GPIO_PORT_OS1,GPIO_PIN_OS1,(BitAction )(1))
#define OS1_0()		 GPIO_WriteBit(GPIO_PORT_OS1,GPIO_PIN_OS1,(BitAction )(0))
#define OS2_1()		 GPIO_WriteBit(GPIO_PORT_OS2,GPIO_PIN_OS2,(BitAction )(1))
#define OS2_0()		 GPIO_WriteBit(GPIO_PORT_OS2,GPIO_PIN_OS2,(BitAction )(0))

#define AD_OS_NO    {OS2_0();  OS1_0();   OS0_0();} 
#define AD_OS_X2    {OS2_0();  OS1_0();   OS0_1();}  
#define AD_OS_X4    {OS2_0();  OS1_1();	OS0_0();} 
#define AD_OS_X8    {OS2_0();  OS1_1();	OS0_1();}  
#define AD_OS_X16   {OS2_1();  OS1_0();	OS0_0();}  
#define AD_OS_X32   {OS2_1();  OS1_0();	OS0_1();} 
#define AD_OS_X64   {OS2_1();  OS1_1();	OS0_0();}  

/*啓動AD轉換*/
#define CONVST_1()	{ GPIO_WriteBit(GPIO_PORT_AD_CONA,GPIO_PIN_AD_CONA,(BitAction )(1));\
                      GPIO_WriteBit(GPIO_PORT_AD_CONB,GPIO_PIN_AD_CONB,(BitAction )(1));}  
#define CONVST_0()	{ GPIO_WriteBit(GPIO_PORT_AD_CONA,GPIO_PIN_AD_CONA,(BitAction )(0));\
                      GPIO_WriteBit(GPIO_PORT_AD_CONB,GPIO_PIN_AD_CONB,(BitAction )(0));}   

#define GET_BUSY()    GPIO_ReadInputDataBit(GPIO_PORT_AD_BUSY,GPIO_PIN_AD_BUSY) 
#define GET_FD()      GPIO_ReadInputDataBit(GPIO_PORT_AD_FD,GPIO_PIN_AD_FD) 
#define GET_DATA()    GPIO_ReadInputData(GPIO_PORT_AD_DB0)
#define SET_CS(x)     GPIO_WriteBit(GPIO_PORT_AD_CS,GPIO_PIN_AD_CS,(BitAction )(x))
#define SET_RD(x)     GPIO_WriteBit(GPIO_PORT_AD_RD,GPIO_PIN_AD_RD,(BitAction )(x))
#define SET_RST(x)    GPIO_WriteBit(GPIO_PORT_AD_RST,GPIO_PIN_AD_RST,(BitAction )(x))		/*設置復位*/									
#define RANGE(x)	    GPIO_WriteBit(GPIO_PORT_AD_RANGE,GPIO_PIN_AD_RANGE,(BitAction )(x))  	/*設置輸入量*/										
											
											
											
											
											

/*
*********************************************************************************************************
*	函 數 名: BUSY_EXTI_TriggerMode
*	功能說明: 設置 BUSY 引腳設置爲中斷模式
*********************************************************************************************************
*/
static void  BUSY_EXTI_TriggerMode(void )
{
		EXTI_InitTypeDef   EXTI_InitStructure;
		GPIO_InitTypeDef   GPIO_InitStructure;
		NVIC_InitTypeDef   NVIC_InitStructure;

	   #if  AD7606_STM32F429
	   //PI10
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE); /* Enable GPIOI clock */
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);/* Enable SYSCFG clock */

		/* Configure PI10 pin as input floating */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_Init(GPIOI, &GPIO_InitStructure);

		/* Connect EXTI Line10 to PI10 pin */
		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource10);

		/* Configure EXTI Line10 */
		EXTI_InitStructure.EXTI_Line = EXTI_Line10;
		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
		//EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
		EXTI_InitStructure.EXTI_LineCmd = ENABLE;
		EXTI_Init(&EXTI_InitStructure);

		/* Enable and set EXTI Line10 Interrupt to the lowest priority */
		NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x06;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
		
		#endif
		
		
		#if  AD7606_STM32F429
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);/* Enable GPIOF clock */
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);/* Enable SYSCFG clock */

		/* Configure PF6 pin as input floating */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
		GPIO_Init(GPIOF, &GPIO_InitStructure);

		/* Connect EXTI Line6 to PF6 pin */
		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOF, EXTI_PinSource6);

		/* Configure EXTI Line6 */
		EXTI_InitStructure.EXTI_Line = EXTI_Line6;
		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
		//EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
		EXTI_InitStructure.EXTI_LineCmd = ENABLE;
		EXTI_Init(&EXTI_InitStructure);

		/* Enable and set EXTI Line6 Interrupt to the lowest priority */
		NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x06;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
     #endif 

}



/*設備操作函數*/

void bsp_DelayWithNOP(u32 cnt)
{
	u32 i=0;
	for(i=0 ; i<cnt ;i++)
	{
		__NOP();
	}
}

/*
*********************************************************************************************************
*	函 數 名: bsp_InitAD7606
*	功能說明: 配置連接外部SRAM的GPIO和FSMC
*	形    參:  無
*	返 回 值: 無
*********************************************************************************************************
*/
void bsp_InitAD7606(void)
{
  	//初始化GPIO
	Gpio_Open(AD_Control_GPIO,11); 
	Gpio_Open(AD_Data_GPIO,16);
	BUSY_EXTI_TriggerMode();
	
	AD_OS_NO;		/* 無過採樣 */
	RANGE(1);
   SET_CS(1);
	
	SET_RD(1);
	SET_RST(0);
   bsp_DelayWithNOP(10);
	SET_RST(1);
	bsp_DelayWithNOP(60);
	SET_RST(0);
	
}

/*
*********************************************************************************************************
*	函 數 名: AD7606_StartConvst
*	功能說明: 啓動1次ADC轉換
*	形    參: 無
*	返 回 值: 無
*********************************************************************************************************
*/
void AD7606_StartConvst(void)
{
	/* page 7:  CONVST 高電平脈衝寬度和低電平脈衝寬度最短 25ns */
	/* CONVST平時爲高 */
	CONVST_0();
	CONVST_0();
	CONVST_0();
	CONVST_1();
}


/*
*********************************************************************************************************
*	函數名: AD7606_ISR
*	功能說明:定時器採樣中斷程序
*	形    參: 
*	返回值: 無
*********************************************************************************************************
*/

short AD_Value[4] = {0};
static void AD7606_ISR(void)
{
	SET_CS(0);//12
	SET_RD(0);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();//24ns

	if(GET_FD()==1)//18
	{
		AD_Value[0] =  (short)GET_DATA();
	}
	else
	{
		SET_RD(1);//12
		SET_CS(1);//12
		return;
	}
	SET_RD(1);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();//24
	
	SET_RD(0);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();//24
	AD_Value[1] = (short)GET_DATA();
	SET_RD(1);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();//24
	
	SET_RD(0);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();//24
	AD_Value[2] = (short)GET_DATA();
	SET_RD(1);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();//24
	
	
	SET_RD(0);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();//24
	AD_Value[3] = (short)GET_DATA();
	SET_RD(1);//12
	__NOP();__NOP();__NOP();__NOP();__NOP();//24
	
	SET_CS(1);//12
}
/*
*********************************************************************************************************
*	函數名:   AD7606_EXTI_IRQHandler
*	功能說明:定時器採樣中斷程序
*	形    參: 
*	返回值: 無
*********************************************************************************************************
*/

#if  AD7606_STM32F429  

#define   AD7606_EXTI_IRQHandler   EXTI15_10_IRQHandler
#define   AD7606_EXTI_Line         EXTI_Line10 

#endif

#if  AD7606_STM32F407  

#define   AD7606_EXTI_IRQHandler   EXTI9_5_IRQHandler
#define   AD7606_EXTI_Line         EXTI_Line6 

#endif


void AD7606_EXTI_IRQHandler(void)
{
	if (EXTI_GetITStatus(AD7606_EXTI_Line) != RESET)
	{
		AD7606_ISR();
		/* Clear the EXTI line 6 pending bit */
		EXTI_ClearITPendingBit(AD7606_EXTI_Line);
	}
}


/*
*********************************************************************************************************
*	函數名:   bsp_GetADValue
*	功能說明: 返回採樣結果
*	形    參: 
*	返回值: 無
*********************************************************************************************************
*/
int bsp_GetADValue(u8 i)
{
	if(i<4)
	{
		return AD_Value[i];
	}
	else
	{
		return 0;
	}
}

#define KALMAN_Q 0.02
#define KALMAN_R 16.0000
static double KalmanFilter(const double ResrcData,double ProcessNiose_Q,double MeasureNoise_R,u8 ch);
/*
*********************************************************************************************************
*	函數名:   bsp_GetKalADVal
*	功能說明: 返回經過濾波的採樣結果
*	形    參: 
*	返回值: 無
*********************************************************************************************************
*/
int bsp_GetKalADVal(u8 i)
{
	int kalAD = 0;
	if(i<4)
	{
		kalAD = (int)KalmanFilter(AD_Value[i],KALMAN_Q,KALMAN_R,i);
		return kalAD;
	}
	else
	{
		return 0;
	}
}



static double KalmanFilter(const double ResrcData,double ProcessNiose_Q,double MeasureNoise_R,u8 ch)
{

    double R = MeasureNoise_R;
    double Q = ProcessNiose_Q;

    static double  x_last[4];
    double x_mid = x_last[ch];
    double x_now;

    static double p_last[4]; 
    double p_mid ;
    double p_now;

    double kg;

    x_mid=x_last[ch];                       //x_last=x(k-1|k-1),x_mid=x(k|k-1)
    p_mid=p_last[ch]+Q;                     //p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=??

    /*
     *  卡爾曼濾波的五個重要公式
     */
    kg=p_mid/(p_mid+R);                  //kg爲kalman filter,R 爲噪聲
    x_now=x_mid+kg*(ResrcData-x_mid);    //估計出的最優值
    p_now=(1-kg)*p_mid;                  //最優值對應的covariance
    p_last[ch] = p_now;                  //更新covariance 值
    x_last[ch] = x_now;                  //更新系統狀態值

    return x_now;
}



/*
*********************************************************************************************************
*
*模塊名稱: AD7606數據採集塊
*文件名稱: bsp_ad7606.h
*版    本: V1.0
*
*
*********************************************************************************************************
*/

#ifndef _BSP_AD7606_H
#define _BSP_AD7606_H
#include "stm32f4xx.h"

void bsp_InitAD7606(void);
void AD7606_StartConvst(void);

#define AD7606_STM32F429   1
#define AD7606_STM32F407   0


#if  AD7606_STM32F429

#define GPIO_PORT_OS0   GPIOC
#define GPIO_PIN_OS0    GPIO_Pin_0 
#define GPIO_PORT_OS1   GPIOC
#define GPIO_PIN_OS1    GPIO_Pin_1  
#define GPIO_PORT_OS2   GPIOC
#define GPIO_PIN_OS2    GPIO_Pin_2  

#define GPIO_PORT_AD_RANGE    GPIOE
#define GPIO_PIN_AD_RANGE     GPIO_Pin_4

#define GPIO_PORT_AD_RST      GPIOI
#define GPIO_PIN_AD_RST       GPIO_Pin_8

#define GPIO_PORT_AD_CS       GPIOI
#define GPIO_PIN_AD_CS        GPIO_Pin_9

#define GPIO_PORT_AD_CONA     GPIOE
#define GPIO_PIN_AD_CONA      GPIO_Pin_5

#define GPIO_PORT_AD_CONB     GPIOE
#define GPIO_PIN_AD_CONB      GPIO_Pin_6

#define GPIO_PORT_AD_RD       GPIOC
#define GPIO_PIN_AD_RD        GPIO_Pin_13

#define GPIO_PORT_AD_BUSY     GPIOI
#define GPIO_PIN_AD_BUSY      GPIO_Pin_10

#define GPIO_PORT_AD_FD       GPIOI
#define GPIO_PIN_AD_FD        GPIO_Pin_11



#define GPIO_PORT_AD_DB0      GPIOF
#define GPIO_PIN_AD_DB0       GPIO_Pin_0

#define GPIO_PORT_AD_DB1      GPIOF
#define GPIO_PIN_AD_DB1       GPIO_Pin_1

#define GPIO_PORT_AD_DB2      GPIOF
#define GPIO_PIN_AD_DB2       GPIO_Pin_2

#define GPIO_PORT_AD_DB3      GPIOF
#define GPIO_PIN_AD_DB3       GPIO_Pin_3

#define GPIO_PORT_AD_DB4      GPIOF
#define GPIO_PIN_AD_DB4       GPIO_Pin_4

#define GPIO_PORT_AD_DB5      GPIOF
#define GPIO_PIN_AD_DB5       GPIO_Pin_5

#define GPIO_PORT_AD_DB6      GPIOF
#define GPIO_PIN_AD_DB6       GPIO_Pin_6

#define GPIO_PORT_AD_DB7      GPIOF
#define GPIO_PIN_AD_DB7       GPIO_Pin_7

#define GPIO_PORT_AD_DB8      GPIOF
#define GPIO_PIN_AD_DB8       GPIO_Pin_8

#define GPIO_PORT_AD_DB9      GPIOF
#define GPIO_PIN_AD_DB9       GPIO_Pin_9

#define GPIO_PORT_AD_DB10      GPIOF
#define GPIO_PIN_AD_DB10       GPIO_Pin_10

#define GPIO_PORT_AD_DB11      GPIOF
#define GPIO_PIN_AD_DB11       GPIO_Pin_11

#define GPIO_PORT_AD_DB12      GPIOF
#define GPIO_PIN_AD_DB12       GPIO_Pin_12

#define GPIO_PORT_AD_DB13      GPIOF
#define GPIO_PIN_AD_DB13       GPIO_Pin_13

#define GPIO_PORT_AD_DB14      GPIOF
#define GPIO_PIN_AD_DB14       GPIO_Pin_14

#define GPIO_PORT_AD_DB15      GPIOF
#define GPIO_PIN_AD_DB15       GPIO_Pin_15

#endif

#if AD7606_STM32F407

#define GPIO_PORT_OS0   GPIOC
#define GPIO_PIN_OS0    GPIO_Pin_0 
#define GPIO_PORT_OS1   GPIOC
#define GPIO_PIN_OS1    GPIO_Pin_1  
#define GPIO_PORT_OS2   GPIOC
#define GPIO_PIN_OS2    GPIO_Pin_2  

#define GPIO_PORT_AD_RANGE    GPIOF
#define GPIO_PIN_AD_RANGE     GPIO_Pin_0

#define GPIO_PORT_AD_RST      GPIOF
#define GPIO_PIN_AD_RST       GPIO_Pin_1

#define GPIO_PORT_AD_CS       GPIOF
#define GPIO_PIN_AD_CS        GPIO_Pin_2

#define GPIO_PORT_AD_CONA     GPIOF
#define GPIO_PIN_AD_CONA      GPIO_Pin_3

#define GPIO_PORT_AD_CONB     GPIOF
#define GPIO_PIN_AD_CONB      GPIO_Pin_4

#define GPIO_PORT_AD_RD       GPIOF
#define GPIO_PIN_AD_RD        GPIO_Pin_5

#define GPIO_PORT_AD_BUSY     GPIOF
#define GPIO_PIN_AD_BUSY      GPIO_Pin_6

#define GPIO_PORT_AD_FD       GPIOF
#define GPIO_PIN_AD_FD        GPIO_Pin_7



#define GPIO_PORT_AD_DB0      GPIOE
#define GPIO_PIN_AD_DB0       GPIO_Pin_0

#define GPIO_PORT_AD_DB1      GPIOE
#define GPIO_PIN_AD_DB1       GPIO_Pin_1

#define GPIO_PORT_AD_DB2      GPIOE
#define GPIO_PIN_AD_DB2       GPIO_Pin_2

#define GPIO_PORT_AD_DB3      GPIOE
#define GPIO_PIN_AD_DB3       GPIO_Pin_3

#define GPIO_PORT_AD_DB4      GPIOE
#define GPIO_PIN_AD_DB4       GPIO_Pin_4

#define GPIO_PORT_AD_DB5      GPIOE
#define GPIO_PIN_AD_DB5       GPIO_Pin_5

#define GPIO_PORT_AD_DB6      GPIOE
#define GPIO_PIN_AD_DB6       GPIO_Pin_6

#define GPIO_PORT_AD_DB7      GPIOE
#define GPIO_PIN_AD_DB7       GPIO_Pin_7

#define GPIO_PORT_AD_DB8      GPIOE
#define GPIO_PIN_AD_DB8       GPIO_Pin_8

#define GPIO_PORT_AD_DB9      GPIOE
#define GPIO_PIN_AD_DB9       GPIO_Pin_9

#define GPIO_PORT_AD_DB10      GPIOE
#define GPIO_PIN_AD_DB10       GPIO_Pin_10

#define GPIO_PORT_AD_DB11      GPIOE
#define GPIO_PIN_AD_DB11       GPIO_Pin_11

#define GPIO_PORT_AD_DB12      GPIOE
#define GPIO_PIN_AD_DB12       GPIO_Pin_12

#define GPIO_PORT_AD_DB13      GPIOE
#define GPIO_PIN_AD_DB13       GPIO_Pin_13

#define GPIO_PORT_AD_DB14      GPIOE
#define GPIO_PIN_AD_DB14       GPIO_Pin_14

#define GPIO_PORT_AD_DB15      GPIOE
#define GPIO_PIN_AD_DB15       GPIO_Pin_15

#endif



#endif

/***************************** (END OF FILE) *********************************/



#include "bsp_GPIO.h"

static void GPIO_SetRCC(GPIO_TypeDef* GPIOx)
{
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  if (GPIOx == GPIOA)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  }
  else if (GPIOx == GPIOB)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  }
  else if (GPIOx == GPIOC)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  }
  else if (GPIOx == GPIOD)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  }
  else if (GPIOx == GPIOE)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  }
  else if (GPIOx == GPIOF)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  }
  else if (GPIOx == GPIOG)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  }
  else if (GPIOx == GPIOH)
  {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  }
  else
  {
    if (GPIOx == GPIOI)
    {
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
    }
  }
}


void Gpio_Open(MYGPIO * prt ,uint8_t num)
{
 uint8_t index;
 for(index=0;index<num;index++)
 {
	  GPIO_InitTypeDef   GPIO_InitStructure;
    GPIO_SetRCC(prt[index].GPIOx);
	  GPIO_InitStructure.GPIO_Mode = prt[index].Mode;
	  if( prt[index].Mode == OUTPUT ) 
		{
			GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
			GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		}else 
		{ 
			GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
		  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		}
		GPIO_InitStructure.GPIO_Pin =  prt[index].Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(prt[index].GPIOx, &GPIO_InitStructure);
 }
}





#ifndef _BSP_GPIO_H
#define _BSP_GPIO_H
#include "stm32f4xx.h"


typedef enum 
{
	INPUT  = 0x00,
	OUTPUT = 0x01,
	AIN    = 0x03
}PinMode;
typedef struct
{
	GPIO_TypeDef*   GPIOx;
	uint16_t        Pin;
	PinMode         Mode;
}MYGPIO;


void Gpio_Open(MYGPIO * prt ,uint8_t num);

#endif

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章