写一个stm32LED连续亮三次的程序,不要重复程序
时间: 2024-06-11 15:06:39 浏览: 8
以下是一个基于STM32的LED连续亮三次的程序,其中使用了定时器和中断来控制LED的闪烁。
```
#include "stm32f10x.h"
void delay_ms(uint32_t ms)
{
for(uint32_t i = 0; i < ms; i++)
{
for(uint32_t j = 0; j < 20000; j++);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
static uint8_t count = 0;
static uint8_t state = 0;
if(count < 6)
{
if(state == 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_8);
state = 1;
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
state = 0;
}
}
else
{
TIM_Cmd(TIM2, DISABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
count++;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_Period = 500;
TIM_InitStructure.TIM_Prescaler = 35999;
TIM_TimeBaseInit(TIM2, &TIM_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
while(1)
{
delay_ms(1000);
}
}
```
在这个程序中,我们使用了STM32的定时器TIM2来控制LED的闪烁。定时器的时钟频率为72000000Hz,我们将其分频为36000,这样每秒就会有2000个计数。在TIM2的定时周期为500时,每次计数需要500个时钟周期,因此可以得到每秒4次中断。如果我们将计数次数限制在12次以内,则LED将会在3秒内连续亮三次。
在定时器中断处理函数中,我们使用了一个计数器count来记录当前闪烁的次数,使用一个状态变量state来记录当前LED的状态。当计数器小于6时,LED会在每次中断时闪烁一次,当计数器等于6时,定时器会被停止,并且LED会被关闭。在这个程序中,我们使用了标准库函数来控制GPIO的状态,但是这些函数的执行速度可能比较慢,因此我们还可以使用直接操作寄存器的方法来更快地控制GPIO的状态。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)