利用定时器 T1 和 GPIO 口编写程序控制 LED 灯控制程序。要求:LED1 亮 1 秒,灭 0.5 秒后,LED2 亮 1 秒,灭 1 秒.交替闪烁。
时间: 2024-05-09 15:15:27 浏览: 46
以下为基于STM32F103C8T6芯片的代码示例:
```
#include "stm32f10x.h"
void delay(uint32_t nCount);
int main(void)
{
// 初始化 GPIO 口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while(1)
{
// LED1 亮 1 秒,灭 0.5 秒
GPIO_SetBits(GPIOC, GPIO_Pin_13);
delay(1000);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
delay(500);
// LED2 亮 1 秒,灭 1 秒
GPIO_SetBits(GPIOC, GPIO_Pin_14);
delay(1000);
GPIO_ResetBits(GPIOC, GPIO_Pin_14);
delay(1000);
}
}
void delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
```
该程序通过不断循环,控制 LED1 和 LED2 的亮灭,实现交替闪烁的效果。其中 delay 函数实现了简单的延时功能。需要注意的是,该程序使用的是定时器 T1,但是并没有使用到该定时器的计数功能。如果需要更高精度的定时功能,可以考虑使用定时器的计数功能。