以STM32F407定时器产生0.01秒单位时间,进而产生秒、分,并通过4.3寸液晶显示屏显示分钟、秒信息。同时添加触摸屏按键能够控制倒计时的设置、启动/停止和清零操作完整程序编写
时间: 2024-02-03 08:02:02 浏览: 18
好的,以下是基于STM32F407的完整程序编写,其中使用了定时器和液晶显示屏,同时添加了触摸屏按键进行控制:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_dma.h"
#include "misc.h"
#include "delay.h"
#include "usart.h"
#include "lcd.h"
#include "touch.h"
// 定义分、秒计数值和倒计时总时间
volatile uint8_t minCount = 0, secCount = 0;
volatile uint16_t countdown = 0;
// 倒计时启动标志位
volatile uint8_t countdownStart = 0;
void TIM2_IRQHandler(void) {
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
secCount++;
if (secCount >= 60) {
secCount = 0;
minCount++;
}
}
}
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
EXTI_ClearITPendingBit(EXTI_Line0);
if (countdownStart == 0) {
// 开始倒计时
countdownStart = 1;
countdown = minCount * 60 + secCount;
minCount = 0;
secCount = 0;
LCD_ShowString(10, 10, "Countdown:", WHITE);
LCD_ShowNum(130, 10, countdown, WHITE, 16);
} else {
// 停止倒计时
countdownStart = 0;
LCD_Fill(0, 30, 320, 210, BLACK);
}
}
}
void EXTI15_10_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line13) != RESET) {
EXTI_ClearITPendingBit(EXTI_Line13);
if (countdownStart == 1) {
// 清零倒计时
countdownStart = 0;
minCount = 0;
secCount = 0;
countdown = 0;
LCD_Fill(0, 30, 320, 210, BLACK);
}
}
}
int main(void) {
// 初始化系统时钟
SystemInit();
// 初始化延时函数
delay_init(168);
// 初始化串口1
USART1_Init();
// 初始化LCD
LCD_Init();
LCD_Clear(BLACK);
LCD_ShowString(10, 10, "Time:", WHITE);
LCD_ShowString(10, 30, "Minute:", WHITE);
LCD_ShowNum(130, 30, minCount, WHITE, 2);
LCD_ShowString(10, 50, "Second:", WHITE);
LCD_ShowNum(130, 50, secCount, WHITE, 2);
// 初始化触摸屏
TP_Init();
// 初始化定时器2
TIM_TimeBaseInitTypeDef timerInitStructure;
NVIC_InitTypeDef nvicInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
timerInitStructure.TIM_Prescaler = 16800 - 1;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 100 - 1;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
nvicInitStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 0;
nvicInitStructure.NVIC_IRQChannelSubPriority = 1;
nvicInitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStructure);
// 初始化外部中断0和13
EXTI_InitTypeDef extiInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitTypeDef gpioInitStructure;
gpioInitStructure.GPIO_Pin = GPIO_Pin_0;
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN;
gpioInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpioInitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
extiInitStructure.EXTI_Line = EXTI_Line0;
extiInitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
extiInitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
extiInitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&extiInitStructure);
nvicInitStructure.NVIC_IRQChannel = EXTI0_IRQn;
nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
nvicInitStructure.NVIC_IRQChannelSubPriority = 0x00;
nvicInitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
gpioInitStructure.GPIO_Pin = GPIO_Pin_13;
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN;
gpioInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &gpioInitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource13);
extiInitStructure.EXTI_Line = EXTI_Line13;
extiInitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
extiInitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
extiInitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&extiInitStructure);
nvicInitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
nvicInitStructure.NVIC_IRQChannelSubPriority = 0x01;
nvicInitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStructure);
while (1) {
if (countdownStart == 0) {
// 显示当前时间
LCD_ShowNum(130, 30, minCount, WHITE, 2);
LCD_ShowNum(130, 50, secCount, WHITE, 2);
} else {
// 显示倒计时
countdown--;
if (countdown == 0) {
countdownStart = 0;
minCount = 0;
secCount = 0;
countdown = 0;
LCD_Fill(0, 30, 320, 210, BLACK);
}
LCD_ShowNum(130, 10, countdown, WHITE, 16);
}
}
}
```
其中,`TIM2_IRQHandler()` 为定时器2的中断处理函数,每当定时器溢出时,秒数加1,若秒数达到60,则分钟数加1。`EXTI0_IRQHandler()` 为外部中断0的中断处理函数,用于开始/停止倒计时。`EXTI15_10_IRQHandler()` 为外部中断13的中断处理函数,用于清零倒计时。
在 `main()` 函数中,先初始化各个模块,然后进入循环。当倒计时未启动时,显示当前时间,当倒计时启动时,显示倒计时时间,并每秒减1,当倒计时结束时,自动停止并清零。
以上是完整的程序编写,其中涉及到的函数可以在相关库文件中找到。同时,需要注意的是,以上代码仅供参考,具体实现还需要根据实际情况进行调整和完善。
阅读全文