用ch32V303编写流水灯
时间: 2024-05-15 07:16:50 浏览: 8
以下是使用ch32V303编写流水灯的示例代码:
```c
#include <ch32v30x.h>
void delay(int time) {
while (time--);
}
int main() {
// 使能GPIOB时钟
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
// 配置PB0,PB1,PB2和PB3为输出模式
GPIOB->MODER |= GPIO_MODER_MODER0_0 | GPIO_MODER_MODER1_0 | GPIO_MODER_MODER2_0 | GPIO_MODER_MODER3_0;
while (1) {
// 点亮PB0灯
GPIOB->BSRR = GPIO_BSRR_BS_0;
delay(100000);
// 熄灭PB0灯,点亮PB1灯
GPIOB->BSRR = GPIO_BSRR_BR_0 | GPIO_BSRR_BS_1;
delay(100000);
// 熄灭PB1灯,点亮PB2灯
GPIOB->BSRR = GPIO_BSRR_BR_1 | GPIO_BSRR_BS_2;
delay(100000);
// 熄灭PB2灯,点亮PB3灯
GPIOB->BSRR = GPIO_BSRR_BR_2 | GPIO_BSRR_BS_3;
delay(100000);
// 熄灭PB3灯
GPIOB->BSRR = GPIO_BSRR_BR_3;
delay(100000);
}
}
```
上述代码中,我们通过配置GPIOB的MODER寄存器的前四位为01,将PB0,PB1,PB2和PB3配置为输出模式。然后在主循环中,我们依次点亮PB0,PB1,PB2和PB3的LED灯,并且每次点亮一个LED灯的时候,我们都要先熄灭前一个LED灯。为了控制流水灯闪烁的速度,我们在每次点亮LED灯之后加入了一个延时函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)