【ST NUCLEO-WB09KE测评】-3-按键,小灯与串口打印测试
[复制链接]
本帖最后由 慕容雪花 于 2024-9-10 22:09 编辑
MB1801D底板上有4个按键,其中B1~B3为用户按键,B4为复位按键。在开发板上的位置如图所示:
电路原理图如下:
三个小灯分别是R, G, B
之前提到过,项目是在CubeIde基于开发板进行开发的,所以可以方便的使用ST提供的一些外设接口,比如LED初始化可以调用如下函数:
BSP_LED_Init(LED_BLUE);
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_RED);
/**
* @brief Configure LED GPIO.
* @param Led Specifies the Led to be configured.
* This parameter can be one of following parameters:
* @arg LD1
* @arg LD2
* @arg LD3
* @retval BSP error code.
*/
int32_t BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_Init;
/* Enable the GPIO_LED Clock */
if (Led == LD1)
{
LD1_GPIO_CLK_ENABLE();
}
else if (Led == LD2)
{
LD2_GPIO_CLK_ENABLE();
}
else /* Led = LD3 */
{
LD3_GPIO_CLK_ENABLE();
}
/* configure the GPIO_LED pin */
GPIO_Init.Pin = LED_PIN[Led];
GPIO_Init.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Init.Pull = GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT[Led], &GPIO_Init);
if( LL_PWR_IsEnabledPUPDCfg() != 0)
{
if (LED_PORT[Led] == GPIOA)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_A, GPIO_Init.Pin);
}
if (LED_PORT[Led] == GPIOB)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_B, GPIO_Init.Pin);
}
}
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
return BSP_ERROR_NONE;
}
按键初始化与中断回调注册:
BSP_PB_Init(B1, BUTTON_MODE_EXTI);
BSP_PB_Init(B2, BUTTON_MODE_EXTI);
BSP_PB_Init(B3, BUTTON_MODE_EXTI);
/**
* @brief Configure Button GPIO and EXTI Line.
* @param Button Specifies the Button to be configured.
* This parameter should be:
* @arg B1
* @arg B2
* @arg B3
* @param ButtonMode Specifies Button mode.
* This parameter can be one of following parameters:
* @arg BUTTON_MODE_GPIO: Button will be used as simple IO
* @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
* generation capability
* @retval BSP error code.
*/
int32_t BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
int32_t status = BSP_ERROR_NONE;
GPIO_InitTypeDef GPIO_Init;
/* Enable the BUTTON clock */
if (Button == B1)
{
B1_GPIO_CLK_ENABLE();
}
else if (Button == B2)
{
B2_GPIO_CLK_ENABLE();
}
else /* B3 */
{
B3_GPIO_CLK_ENABLE();
}
GPIO_Init.Pin = BUTTON_PIN[Button];
GPIO_Init.Pull = GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FREQ_HIGH;
if (ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
GPIO_Init.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_Init);
}
if (ButtonMode == BUTTON_MODE_EXTI)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* Configure Button pin as input with External interrupt */
GPIO_Init.Mode = GPIO_MODE_IT_FALLING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_Init);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority(BUTTON_IRQn[Button], 0x00, 0);
HAL_NVIC_EnableIRQ(BUTTON_IRQn[Button]);
}
if( LL_PWR_IsEnabledPUPDCfg() != 0)
{
if (BUTTON_PORT[Button] == GPIOA)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_A, GPIO_Init.Pin);
}
if (BUTTON_PORT[Button] == GPIOB)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_B, GPIO_Init.Pin);
}
}
return status;
}
中断处理函数:
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) {
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
if (GPIO_Pin == B1_PIN) {
BSP_LED_Toggle(LD1);
printf("\r\n B1 Pressed");
}
if (GPIO_Pin == B2_PIN) {
BSP_LED_Toggle(LD2);
printf("\r\n B2 Pressed");
}
if (GPIO_Pin == B3_PIN) {
BSP_LED_Toggle(LD3);
printf("\r\n B3 Pressed");
}
}
实物展示:
BUTTON-GPIO
|