damiaa 发表于 2023-8-6 20:39

【STM32WBA52CG】+3 外设应用一 按键和点灯

<div class='showpostmsg'> 本帖最后由 damiaa 于 2023-8-6 20:46 编辑

<p><strong>【</strong><strong>STM32WBA52CG】+</strong><strong>3</strong><strong> STM32WBA </strong><strong>外设应用</strong><strong>一</strong> <strong>按键和</strong><strong>点灯</strong></p>

<p><a href="https://bbs.eeworld.com.cn/thread-1250745-1-1.html"><strong>【NUCLEO-WBA52CG】+</strong><strong>1开箱贴</strong></a></p>

<p><a href="https://bbs.eeworld.com.cn/thread-1250558-1-1.html"><strong>【</strong><strong>STM32WBA52CG】+2&nbsp;STM32WBA Web Bluetooth试玩</strong></a> <strong>之后继续</strong></p>

<p>感觉外设的使用还是蛮有必要的,于是先玩玩这个吧。</p>

<p>新建一个项目</p>

<p></p>

<p>选择配置为缺省 生成代码如下</p>

<p></p>

<p>IO口是这样的:</p>

<p></p>

<p></p>

<p><strong></strong></p>

<p><strong>加入控制led的代码</strong></p>

<p>&nbsp;</p>

<pre>
<code>while (1)

{

HAL_GPIO_WritePin(GPIOB, LD2_Pin|LD3_Pin|LD1_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin|LD3_Pin|LD1_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}</code></pre>

<p><strong>结果:</strong></p>

<p><strong></strong></p>

<p><strong></strong></p>

<p><strong></strong></p>

<p><strong>按键如下:</strong></p>

<p><strong></strong></p>

<p><strong>下面测试按键和led</strong></p>

<p><strong>修改主程序代码如下:</strong></p>

<pre>
<code>void key_check(void){

uint8_t keyval1 = HAL_GPIO_ReadPin( B1_GPIO_Port, B1_Pin);

if(keyval1 == 1)

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_SET);

else

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_RESET);

uint8_t keyval2 = HAL_GPIO_ReadPin( B2_GPIO_Port, B2_Pin);

if(keyval2 == 1)

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);

else

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);

uint8_t keyval3 = HAL_GPIO_ReadPin( B3_GPIO_Port, B3_Pin);

if(keyval3 == 1)

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);

else

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_RESET);

}

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

if(1) //测试按键和led

{

key_check();

HAL_Delay(100);

}

if(0)//测试led

{

HAL_GPIO_WritePin(GPIOB, LD2_Pin|LD3_Pin|LD1_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin|LD3_Pin|LD1_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_RESET);

HAL_Delay(1000);

}

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}</code></pre>

<p><strong>测试结果:</strong></p>

<p><strong></strong></p>

<p><strong></strong></p>

<p><strong></strong></p>

<p><strong>谢谢大家</strong></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【STM32WBA52CG】+3 外设应用一 按键和点灯