版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
配置时钟:
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
CMU_ClockEnable(cmuClock_HFPER, true);
/* Enable GPIO in CMU */
CMU_ClockEnable(cmuClock_GPIO, true);
配置引脚为输入:
void GPIO_PinModeSet(GPIO_Port_TypeDef port,
unsigned int pin,
GPIO_Mode_TypeDef mode,
unsigned int out)
/* Configure PB9 and PB10 as input */
GPIO_PinModeSet(gpioPortB, 9, gpioModeInput, 0);
GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0);
配置引脚为输出:
void GPIO_PinModeSet(GPIO_Port_TypeDef port,
unsigned int pin,
GPIO_Mode_TypeDef mode,
unsigned int out)
GPIO_PinModeSet(ledArray[ledNo].port, ledArray[ledNo].pin, gpioModePushPull, 0);
配置引脚为中断输入:
中断初始化:
void GPIOINT_Init(void)
{
NVIC_ClearPendingIRQ(GPIO_ODD_IRQn); //清除奇数引脚中断标志
NVIC_EnableIRQ(GPIO_ODD_IRQn); //使能奇数引脚中断
NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn); //清除偶数引脚中断标志
NVIC_EnableIRQ(GPIO_EVEN_IRQn); //使能偶数引脚中断
}
先配置为输入,再配置中断
__STATIC_INLINE void GPIO_IntConfig(GPIO_Port_TypeDef port,
unsigned int pin,
bool risingEdge,
bool fallingEdge,
bool enable)
/* Set falling edge interrupt for both ports */
GPIO_IntConfig(gpioPortB, 9, false, true, true);
GPIO_IntConfig(gpioPortB, 10, false, true, true);
引脚输出高电平:
__STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
GPIO_PinOutSet(ledArray[ledNo].port, ledArray[ledNo].pin);
引脚输出低电平:
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
GPIO_PinOutClear(ledArray[ledNo].port, ledArray[ledNo].pin);
引脚翻转输出电平:
__STATIC_INLINE void GPIO_PinOutToggle(GPIO_Port_TypeDef port, unsigned int pin)
GPIO_PinOutToggle(ledArray[ledNo].port, ledArray[ledNo].pin);
读取输出引脚电平:
__STATIC_INLINE unsigned int GPIO_PinOutGet(GPIO_Port_TypeDef port,
unsigned int pin)
retVal = (int)GPIO_PinOutGet(ledArray[ledNo].port, ledArray[ledNo].pin);
读取输入引脚电平:
__STATIC_INLINE unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port,
unsigned int pin)
retVal = (int)GPIO_PinInGet(ledArray[ledNo].port, ledArray[ledNo].pin);
读取输入端口:
__STATIC_INLINE uint32_t GPIO_PortInGet(GPIO_Port_TypeDef port)
retVal = GPIO_PortInGet(port);