初始化
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#include "Debug.h"
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#if DEBUG_ENABLE
|
||||
|
||||
// 调试缓冲区
|
||||
static char debug_buffer[256];
|
||||
|
||||
/**
|
||||
* @brief 初始化调试串口(USART2, PA2=TX, PA3=RX)
|
||||
*/
|
||||
void Debug_Init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
// 使能时钟
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
|
||||
// 配置PA2为推挽输出(TX)
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
// 配置PA3为浮空输入(RX)
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
// 配置USART2
|
||||
USART_InitStructure.USART_BaudRate = 115200;
|
||||
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(USART2, &USART_InitStructure);
|
||||
|
||||
// 使能USART2
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
|
||||
// 发送启动信息
|
||||
Debug_Printf("\r\n========================================\r\n");
|
||||
Debug_Printf(" STM32 Face Recognition System\r\n");
|
||||
Debug_Printf(" Debug Console Started\r\n");
|
||||
Debug_Printf("========================================\r\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过调试串口发送一个字节
|
||||
*/
|
||||
static void Debug_SendByte(uint8_t byte)
|
||||
{
|
||||
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
|
||||
USART_SendData(USART2, byte);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过调试串口发送字符串
|
||||
*/
|
||||
static void Debug_SendString(const char* str)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
Debug_SendByte(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 格式化调试信息输出(类似printf)
|
||||
*/
|
||||
void Debug_Printf(const char* format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(debug_buffer, sizeof(debug_buffer), format, args);
|
||||
va_end(args);
|
||||
|
||||
Debug_SendString(debug_buffer);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// 调试关闭时,提供空实现
|
||||
void Debug_Init(void) {}
|
||||
void Debug_Printf(const char* format, ...) {}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef __DEBUG_H
|
||||
#define __DEBUG_H
|
||||
|
||||
#include "stm32f10x.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// 调试功能开关(1=开启调试,0=关闭调试)
|
||||
#define DEBUG_ENABLE 1 // 暂时关闭,添加Debug.c后改回1
|
||||
|
||||
// 调试串口初始化(使用USART2,PA2/PA3)
|
||||
void Debug_Init(void);
|
||||
|
||||
// 调试信息输出函数
|
||||
void Debug_Printf(const char* format, ...);
|
||||
|
||||
// 快捷宏定义
|
||||
#if DEBUG_ENABLE
|
||||
#define DEBUG_PRINT(...) Debug_Printf(__VA_ARGS__)
|
||||
#define DEBUG_INFO(msg) Debug_Printf("[INFO] %s\r\n", msg)
|
||||
#define DEBUG_ERROR(msg) Debug_Printf("[ERROR] %s\r\n", msg)
|
||||
#define DEBUG_OK(msg) Debug_Printf("[OK] %s\r\n", msg)
|
||||
#else
|
||||
#define DEBUG_PRINT(...)
|
||||
#define DEBUG_INFO(msg)
|
||||
#define DEBUG_ERROR(msg)
|
||||
#define DEBUG_OK(msg)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
// Delay.c (修改后)
|
||||
#include "stm32f10x.h"
|
||||
|
||||
// 1. 添加一个全局变量,用于记录毫秒数
|
||||
// 注意:不使用static,以便中断函数可以访问
|
||||
volatile uint32_t g_time_ms = 0;
|
||||
|
||||
/**
|
||||
* @brief 初始化SysTick定时器,配置为1ms中断一次
|
||||
*/
|
||||
void Delay_Init(void)
|
||||
{
|
||||
// 配置SysTick为1ms中断一次。SystemCoreClock通常是72MHz。
|
||||
if (SysTick_Config(SystemCoreClock / 1000))
|
||||
{
|
||||
// 如果配置失败,进入死循环
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取自启动以来的毫秒数
|
||||
* @retval 当前的毫秒时间戳
|
||||
*/
|
||||
uint32_t Get_Time_ms(void)
|
||||
{
|
||||
return g_time_ms;
|
||||
}
|
||||
|
||||
|
||||
// --- 以下是你原本就有的阻塞式延时函数,保持不变 ---
|
||||
|
||||
void Delay_us(uint32_t xus)
|
||||
{
|
||||
SysTick->LOAD = 72 * xus;
|
||||
SysTick->VAL = 0x00;
|
||||
SysTick->CTRL = 0x00000005;
|
||||
while(!(SysTick->CTRL & 0x00010000));
|
||||
SysTick->CTRL = 0x00000004;
|
||||
}
|
||||
|
||||
void Delay_ms(uint32_t xms)
|
||||
{
|
||||
// 使用SysTick计时器实现精确延时
|
||||
uint32_t start_time = g_time_ms;
|
||||
while((g_time_ms - start_time) < xms) {
|
||||
// 等待
|
||||
}
|
||||
}
|
||||
|
||||
void Delay_s(uint32_t xs)
|
||||
{
|
||||
while(xs--)
|
||||
{
|
||||
Delay_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// 在这个文件的末尾,或者在 stm32f10x_it.c 中,你需要 SysTick_Handler
|
||||
// 为了集中管理,我们把它放在 stm32f10x_it.c
|
||||
@@ -0,0 +1,16 @@
|
||||
// Delay.h (修改后)
|
||||
#ifndef __DELAY_H
|
||||
#define __DELAY_H
|
||||
|
||||
#include "stm32f10x.h" // 确保包含了这个
|
||||
|
||||
// 添加下面两个函数的声明
|
||||
void Delay_Init(void);
|
||||
uint32_t Get_Time_ms(void);
|
||||
|
||||
// 保留你原有的函数声明
|
||||
void Delay_us(uint32_t us);
|
||||
void Delay_ms(uint32_t ms);
|
||||
void Delay_s(uint32_t s);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user