基于STM32的奥运会倒计时代码
时间: 2024-06-09 14:11:53 浏览: 7
以下是一个基于STM32的奥运会倒计时的代码示例:
```c
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stdio.h"
#define LED_PORT GPIOB
#define LED_PIN GPIO_Pin_1
TIM_TimeBaseInitTypeDef timerInitStructure;
NVIC_InitTypeDef nvicStructure;
USART_InitTypeDef usartInitStructure;
void initLED(void);
void initTimer(void);
void initUSART(void);
void sendString(char *str);
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
initLED();
initTimer();
initUSART();
while (1);
}
void initLED(void)
{
GPIO_InitTypeDef gpioStructure;
gpioStructure.GPIO_Pin = LED_PIN;
gpioStructure.GPIO_Mode = GPIO_Mode_Out_PP;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &gpioStructure);
}
void initTimer(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
timerInitStructure.TIM_Prescaler = 7200 - 1; // 1秒钟计数次数
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Down; // 向下计数模式
timerInitStructure.TIM_Period = 60 * 60 * 24 - 1; // 计数周期为1天
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
nvicStructure.NVIC_IRQChannelSubPriority = 0;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
TIM_Cmd(TIM2, ENABLE);
}
void initUSART(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef gpioStructure;
gpioStructure.GPIO_Pin = GPIO_Pin_9;
gpioStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioStructure);
gpioStructure.GPIO_Pin = GPIO_Pin_10;
gpioStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpioStructure);
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &usartInitStructure);
USART_Cmd(USART1, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
int days = TIM_GetCounter(TIM2) / 86400;
int hours = (TIM_GetCounter(TIM2) - days * 86400) / 3600;
int minutes = (TIM_GetCounter(TIM2) - days * 86400 - hours * 3600) / 60;
int seconds = TIM_GetCounter(TIM2) - days * 86400 - hours * 3600 - minutes * 60;
char buffer[50];
sprintf(buffer, "倒计时:%d天 %02d:%02d:%02d\r\n", days, hours, minutes, seconds);
sendString(buffer);
if (TIM_GetCounter(TIM2) == 0)
{
GPIO_SetBits(LED_PORT, LED_PIN);
}
}
}
void sendString(char *str)
{
while (*str)
{
USART_SendData(USART1, *str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
这个代码使用了STM32的定时器和串口模块实现了一个倒计时功能。在主函数中初始化了LED、定时器和串口模块,然后进入了一个死循环。定时器的计数周期为1天,每次计数完成后会触发一个中断,在中断处理函数中计算倒计时并通过串口发送到电脑上。当倒计时结束时,会点亮一个LED。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)