【ST NUCLEO-WB09KE测评】-3-按键,小灯与串口打印测试
本帖最后由 慕容雪花 于 2024-9-10 22:09 编辑<p>MB1801D底板上有4个按键,其中B1~B3为用户按键,B4为复位按键。在开发板上的位置如图所示:</p>
<p style="text-align: center;"> </p>
<p>电路原理图如下:</p>
<p style="text-align: center;"> 三个小灯分别是R, G, B</p>
<p style="text-align: center;"> </p>
<p>之前提到过,项目是在CubeIde基于开发板进行开发的,所以可以方便的使用ST提供的一些外设接口,比如LED初始化可以调用如下函数:</p>
<pre>
<code>BSP_LED_Init(LED_BLUE);
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_RED);</code></pre>
<p> </p>
<pre>
<code>/**
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief </a>Configure LED GPIO.
* @paramLed Specifies the Led to be configured.
* This parameter can be one of following parameters:
* <a href="home.php?mod=space&uid=1238002" target="_blank">@arg </a>LD1
* @argLD2
* @argLD3
* @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;
GPIO_Init.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_Init.Pull= GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT, &GPIO_Init);
if( LL_PWR_IsEnabledPUPDCfg() != 0)
{
if (LED_PORT == GPIOA)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_A, GPIO_Init.Pin);
}
if (LED_PORT == GPIOB)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_B, GPIO_Init.Pin);
}
}
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
return BSP_ERROR_NONE;
}</code></pre>
<p>按键初始化与中断回调注册:</p>
<pre>
<code>BSP_PB_Init(B1, BUTTON_MODE_EXTI);
BSP_PB_Init(B2, BUTTON_MODE_EXTI);
BSP_PB_Init(B3, BUTTON_MODE_EXTI);</code></pre>
<p> </p>
<pre>
<code>/**
* @briefConfigure Button GPIO and EXTI Line.
* @paramButton Specifies the Button to be configured.
* This parameter should be:
* @arg B1
* @arg B2
* @arg B3
* @paramButtonMode 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;
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, &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, &GPIO_Init);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority(BUTTON_IRQn, 0x00, 0);
HAL_NVIC_EnableIRQ(BUTTON_IRQn);
}
if( LL_PWR_IsEnabledPUPDCfg() != 0)
{
if (BUTTON_PORT == GPIOA)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_A, GPIO_Init.Pin);
}
if (BUTTON_PORT == GPIOB)
{
LL_PWR_EnableGPIOPullUp( LL_PWR_GPIO_B, GPIO_Init.Pin);
}
}
return status;
}</code></pre>
<p> </p>
<p>中断处理函数:</p>
<pre>
<code>/* 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");
}
}</code></pre>
<p>实物展示:</p>
<p>c63bc3d4083cd927d774163bb0fc1a5d<br />
</p>
<p>用HAL是不是很容易开发啊,哈哈,我看这个板子也挺好看,支持楼主一波</p>
yangjiaxu 发表于 2024-9-11 11:56
用HAL是不是很容易开发啊,哈哈,我看这个板子也挺好看,支持楼主一波
<p>那可不,我这种菜鸟都蹦哒蹦哒的点灯了,大佬更是如虎添翼</p>
页:
[1]