STM32按键流水灯代码
时间: 2023-11-04 19:06:00 浏览: 122
以下是使用STM32的按键控制流水灯的示例代码:
```c
#include "stm32f10x.h"
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOA和GPIOC时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
/* 配置PA0为输入引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置PC8,PC9,PC10,PC11为输出引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while(1)
{
/* 判断按键是否按下 */
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0)
{
/* 如果按下,则开始流水灯 */
GPIO_SetBits(GPIOC, GPIO_Pin_8);
Delay(100);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_9);
Delay(100);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_10);
Delay(100);
GPIO_ResetBits(GPIOC, GPIO_Pin_10);
GPIO_SetBits(GPIOC, GPIO_Pin_11);
Delay(100);
GPIO_ResetBits(GPIOC, GPIO_Pin_11);
}
}
}
/* 延迟函数 */
void Delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
```
该代码会在PA0按下时,控制PC8、PC9、PC10、PC11引脚依次点亮,形成流水灯的效果。需要注意的是,该代码中使用了一个简单的延迟函数`Delay()`,实际应用中可能需要使用定时器实现更加精确的延时。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)