ST NUCLEO-C031C6开发板学习笔记05(LED控制)
<div class='showpostmsg'>## ==8:LED灯控制==### 8.1:硬件设计
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305095813574.png)
从原理图可以看出,使用PA5进行控制LED,高电平有效。
### 8.2:软件设计
1:CubeMX设置
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305100452791.png)
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305100520186.png)
2:代码编写
结合按键控制,单击打开灯,双击关闭灯。
LED引脚端口初始化
```C
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PA5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
根据按键值来判断LED的亮灭
```C
voidtask3_hook(void)
{
switch(gVarInfo.keyVal){
case 1: /*单击*/
DEBUG_LOG("单击开灯");
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
gVarInfo.keyVal = KEY_NONE;
break;
case 2:
break;
case 3:
break;
case 4: /*双击*/
DEBUG_LOG("双击关灯");
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
gVarInfo.keyVal = KEY_NONE;
break;
default:
break;
}
// HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
}
```
### 8.3:单元测试验证
使用串口调试助手看下数据信息:
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305105630737.png)
</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){
} </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]