|
功能:读取GPIO三态
源码:
typedef enum
{
GND=0x00,
FLOATING=0x02,
HIGH=0x03,
GPIOSTATE_UNK=0x04
}t_GPIOSTATE;
//-------------------
uCHAR PinRead(uCHAR GPIOx,uCHAR pin,uCHAR mode)
{
pGPIOxCtrl[GPIOx]->GPIO_DIR |= (0x1<<pin); // direction:input
if( pin <= BIT07 ) // AF0
pGPIOxCtrl[GPIOx]->GPIO_AFRL &= ~(0xF<<(pin<<2));
}
else
{
pGPIOxCtrl[GPIOx]->GPIO_AFRH&= ~(0xF<<((pin-BIT08)<<2));
}
pGPIOxCtrl[GPIOx]->GPIO_PUDR &= ~(0x3<<(pin<<1)); // clear PUD (floating mode default)
pGPIOxCtrl[GPIOx]->GPIO_PUDR |= (mode<<(pin<<1));
tickWaitus(20,5);
if ( pGPIOxCtrl[GPIOx]->GPIO_DIO & (0x1<<pin) )
{
return(1);
}
else
{
return(0);
}
}
//-------------------
t_GPIOSTATE CheckPin(uCHAR GPIOx,uCHAR pin)
{uCHAR pull_up,pull_down;
pull_up = PinRead(GPIOx,pin,0x01); // 上拉,读取端口状态
pull_down = PinRead(GPIOx,pin,0x02); // 下拉,读取端口状态
switch( (pull_up<<1)|(pull_down<<0) )
{
case 0x00: // 00
return(GND);
case 0x03: // 11
return(HIGH);
case 0x02: // 10
return(FLOATING);
default:
return(GPIOSTATE_UNK);
}
}
|
|