使用定时器实现LED的5s定时开关功能,按键控制LED灯开关,同时UART将LED状态发送给PC,用SMT32实现实验代码
时间: 2024-03-12 19:43:34 浏览: 62
STM32定时器5秒开关LEDE灯按键控制LED灯串口发送状态.zip
好的,以下是使用STM32定时器和UART实现LED的5秒定时开关功能,并通过按键控制LED灯的开关状态,并将LED状态通过UART发送给PC的实验代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#define LED_PIN GPIO_Pin_13
#define LED_GPIO GPIOC
#define LED_RCC_GPIO RCC_APB2Periph_GPIOC
#define KEY_PIN GPIO_Pin_0
#define KEY_GPIO GPIOA
#define KEY_RCC_GPIO RCC_APB2Periph_GPIOA
#define USART_TX_PIN GPIO_Pin_9
#define USART_RX_PIN GPIO_Pin_10
#define USART_GPIO GPIOA
#define USART_RCC_GPIO RCC_APB2Periph_GPIOA
#define USART_RCC_USART RCC_APB2Periph_USART1
#define USART_USART USART1
#define USART_BAUDRATE 115200
static uint8_t led_state = 0; // LED状态,0表示灭,1表示亮
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 配置LED引脚
RCC_APB2PeriphClockCmd(LED_RCC_GPIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO, &GPIO_InitStructure);
// 配置按键引脚
RCC_APB2PeriphClockCmd(KEY_RCC_GPIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_GPIO, &GPIO_InitStructure);
// 配置USART引脚
RCC_APB2PeriphClockCmd(USART_RCC_GPIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 使能TIM2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// TIM2配置
TIM_TimeBaseStructure.TIM_Period = 5000 - 1; // 计数器自动重载值
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; // 时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
// TIM2中断配置
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_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
// 使能USART1时钟
RCC_APB2PeriphClockCmd(USART_RCC_USART, ENABLE);
// USART1配置
USART_InitStructure.USART_BaudRate = USART_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART_USART, &USART_InitStructure);
USART_Cmd(USART_USART, ENABLE);
}
void USART_SendByte(uint8_t byte)
{
// 等待发送缓冲区为空
while(USART_GetFlagStatus(USART_USART, USART_FLAG_TXE) == RESET);
// 发送一个字节
USART_SendData(USART_USART, byte);
}
void USART_SendString(char* str)
{
while(*str)
{
USART_SendByte(*str++);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
led_state = !led_state; // 取反LED状态
GPIO_WriteBit(LED_GPIO, LED_PIN, led_state ? Bit_SET : Bit_RESET); // 控制LED灯
}
}
int main(void)
{
GPIO_Configuration();
TIM_Configuration();
USART_Configuration();
while(1)
{
if(GPIO_ReadInputDataBit(KEY_GPIO, KEY_PIN) == Bit_RESET) // 按键按下
{
led_state = !led_state; // 取反LED状态
GPIO_WriteBit(LED_GPIO, LED_PIN, led_state ? Bit_SET : Bit_RESET); // 控制LED灯
USART_SendString(led_state ? "LED ON\r\n" : "LED OFF\r\n"); // 发送LED状态给PC
while(GPIO_ReadInputDataBit(KEY_GPIO, KEY_PIN) == Bit_RESET); // 等待按键松开
}
}
return 0;
}
```
在上述代码中,首先通过 GPIO_Configuration() 函数配置了 LED 引脚、按键引脚和 USART 引脚。然后通过 TIM_Configuration() 函数配置了 TIM2 定时器,并在中断处理函数 TIM2_IRQHandler() 中实现了 LED 的定时开关功能。最后在主函数中通过轮询检测按键状态,实现了按键控制 LED 灯的开关状态,并通过 USART 发送 LED 状态给 PC。
需要注意的是,该代码使用了 STM32F103C8T6 开发板,如果您使用的是其他型号的开发板,请根据实际情况修改代码。另外,为了方便调试,该代码将 USART 的波特率设置为了 115200,如果您需要修改波特率,请在 USART_Configuration() 函数中修改 USART_InitStructure.USART_BaudRate 的值。
阅读全文