编写GD32F103芯片定时器中断实现LED亮灭
时间: 2024-04-29 12:25:59 浏览: 225
非常感谢您的提问。关于您的问题,下面是LED亮灭的代码实现:
```c
#include <gd32f10x.h>
void timer_interrupt_config(void);
void gpio_config(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
gpio_config();
timer_interrupt_config();
while (1)
{
}
}
void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOC);
gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
}
void timer_interrupt_config(void)
{
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
timer_oc_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_TOGGLE);
timer_oc_value_config(TIMER2, TIMER_CH_0, 1000);
timer_auto_reload_shadow_enable(TIMER2);
timer_interrupt_enable(TIMER2, TIMER_INT_CH0);
nvic_irq_enable(TIMER2_IRQn, 0, 0);
timer_enable(TIMER2);
}
void TIMER2_IRQHandler(void)
{
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_CH0) != RESET) {
timer_interrupt_flag_clear(TIMER2, TIMER_INT_CH0);
gpio_bit_write(GPIOC, GPIO_PIN_13, (bit_status)(1-gpio_input_bit_get(GPIOC, GPIO_PIN_13)));
}
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
```
这里我们使用的是Timer2来控制LED灯的亮灭,每次定时器中断时通过改变LED灯的状态实现LED灯的闪烁。需要注意的是,这里使用的是GD32F103芯片,仅供参考,具体实现需要根据自己的硬件环境进行调整。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)