GPIO-输入/输出配置



//输入端口定义
GPIO_TypeDef *  m_gpio_in[INPUT_NUM]  ={GPIOE,GPIOE,GPIOE,GPIOE,GPIOE};  
unsigned int    m_port_in[INPUT_NUM]  ={GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6};
//输出端口定义
GPIO_TypeDef *  m_gpio_out[OUTPUT_NUM]={GPIOG,GPIOG,GPIOG,GPIOG,GPIOB,GPIOF,GPIOF};  
unsigned int    m_port_out[OUTPUT_NUM]={GPIO_Pin_6,GPIO_Pin_7,GPIO_Pin_9,GPIO_Pin_15,GPIO_Pin_6,GPIO_Pin_9,GPIO_Pin_10};


static unsigned char  m_status_out[OUTPUT_NUM];
static unsigned char  m_ist[INPUT_NUM];
static unsigned int   m_KeyDownTime[INPUT_NUM];


//*********************************************************************
//IO始化函数
//*********************************************************************
void Gpio_Init(void)
{
unsigned char i;
GPIO_InitTypeDef  GPIO_InitStructure;


    //输入配置
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);                         //使能GPIOE时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;   //PE2 3 4 5 6对应引脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;                                  //普通输入模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;                            //100M
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                                  //上拉
    GPIO_Init(GPIOE, &GPIO_InitStructure);                                        //初始化GPIOE2,3,4
    
    //输出配置     
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);                         //使能GPIOG时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_15;   //PG6 7 9 15
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                                 //普通输出模式
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                                //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;                            //100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                                  //上拉
    GPIO_Init(GPIOG, &GPIO_InitStructure);                                        //初始化GPIO
    GPIO_SetBits(GPIOG,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_15);             //初始高输出


    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);                         //使能GPIOB时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                                     //PB6
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                                 //普通输出模式
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                                //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;                            //100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                                  //上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);                                        //初始化GPIO     
    GPIO_SetBits(GPIOB,GPIO_Pin_6);                                               //初始高输出


    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);                         //使能GPIOD的时钟
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;                                  //输出
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;                                 //推挽输出
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;                               //上拉输出
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;                             //高速GPIO
    GPIO_Init(GPIOF,&GPIO_InitStructure);
    GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);                                  //GPIOF 高电平  
  
    for(i=0;i     for(i=0;i


//*********************************************************************
//无滤波读输入 
//1-无按下,0-有低电平有效输入
//*********************************************************************
unsigned char Gpio_ReadIo(unsigned char port)
{
unsigned char val;
    val=GPIO_ReadInputDataBit(m_gpio_in[port],m_port_in[port]); 
    return val;
}


//*********************************************************************
//写输出 : 
//0-关闭 1-输出,低电平输出
//*********************************************************************
void Gpio_WriteOut(unsigned char port,unsigned char op)
{
    if(op==0) 
    {
        GPIO_SetBits(m_gpio_out[port],m_port_out[port]);        //引脚拉高,关闭输出
        m_status_out[op]=0;
    }
else
    {
        GPIO_ResetBits(m_gpio_out[port],m_port_out[port]);      //引脚拉低,打开输出,低电平输出有效
        m_status_out[op]=1;
    }
}
//*********************************************************************
//读输出
//*********************************************************************
unsigned char Gpio_ReadOut(unsigned char op)
{
    return(m_status_out[op]);
}


//*********************************************************************
//有消抖读输入(必须先执行消抖扫描函数)
//*********************************************************************
int Gpio_ReadIoFilter(unsigned char port)
{
if(port>=INPUT_NUM)return(INPUT_UP);
if(m_ist[port]>=2 && m_ist[port]<=4)return(INPUT_DOWN);
else return(INPUT_UP);
}
//*********************************************************************
//获取按钮按下时间
//*********************************************************************
unsigned int GetKeyDownTime(unsigned char port)
{
OS_ERR err;    
return(OSTimeGet(&err)-m_KeyDownTime[port]);
}


//*********************************************************************
//消抖扫描输入
//注意:获取状态判断只能读一次,然后再对结果进行多态判断如:
//key=Gpio_ScanInput(0) if(key==INPUT_UP ||key==INPUT_LONG_UP )
//*********************************************************************
#define KEY_DOWN_TIME 2000
int Gpio_ScanInput(unsigned char port) 
{
OS_ERR err;
static unsigned int uiInputDelay[INPUT_NUM];
static unsigned int down_time[INPUT_NUM];
    
if(port>=INPUT_NUM)return(0);
switch(m_ist[port])
{
case 0:
if(Gpio_ReadIo(port)==0)
{
uiInputDelay[port]=OSTimeGet(&err)+10;
m_ist[port]++;
}
break;
case 1:
if(OSTimeGet(&err)>=uiInputDelay[port])
{
if(Gpio_ReadIo(port)==0)m_ist[port]++;
else m_ist[port]=0;
}
break;
case 2:
down_time[port]=OSTimeGet(&err)+KEY_DOWN_TIME;
m_ist[port]++;
return(INPUT_DOWN);
case 3:
if(Gpio_ReadIo(port)!=0)
{
uiInputDelay[port]=OSTimeGet(&err)+10;
m_ist[port]++;
}
break;
case 4:
if(OSTimeGet(&err)>=uiInputDelay[port])
{
if(Gpio_ReadIo(port)!=0)m_ist[port]++;
else m_ist[port]--;
}
break;
case 5:
m_ist[port]=0;
if(OSTimeGet(&err)>=down_time[port])
{
return(INPUT_LONG_UP);
}
else
{
return(INPUT_UP);
}
default:
m_ist[port]=0;
break;
}
return(0);
}

你可能感兴趣的:(STM32)