AI文库-您身边的智能助手,如没有想要的内容请与本站万能的AI助手联系。

stm32闪烁灯交替闪烁周期1s

来自 福建省 的网友 时间: 热度:°C 加入收藏 我要投稿 点赞()
您可以使用STM32的定时器来实现闪烁灯交替闪烁周期1s。以下是一个简单的示例代码,演示了如何在STM32F系列微控制器上控制一个LED灯的闪烁:

```c
#include "stm32f10x.h"

void Delay(uint32_t nCount)
{
for (; nCount != 0; nCount--);
}

int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

uint32_t counter = 0;
uint8_t state = 0;

while (1)
{
if (state == 0)
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
state = 1;
}
else if (state == 1)
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
state = 0;
}
Delay(1000);
}
}
```
221381
领取福利

微信扫码领取福利

微信扫码分享