设备:stm32f407vg
IDE:Keil uvision 5
IDE版本:最新版本
Cubemx 版本:最新版本
我想在我的项目中使用 USART 通信协议。提供了通信但发送了不正确的数据(STM> PC)。
编译器中的“SystemInit()”函数用灰线加下划线。一位页面用户评论说:
"""""
SystemInit() 应仅在重置处理程序中调用
此功能应该将系统时钟重置为默认状态,直接从 HSI 以 16 MHz 运行。
您在 main() 的中间调用它,此时 USART1 已经配置为假设更高的时钟速度。不知道外设在动态更改时钟源时会做什么。
""""
代码:
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "stm32f4xx_hal.h"
#include "defines.h"
#include "tm_stm32_disco.h"
#include "tm_stm32_delay.h"
#include "tm_stm32_lis302dl_lis3dsh.h"
#include "stm32f4xx.h"
#include "arm_math.h"
#define PID_PARAM_KP 100
#define PID_PARAM_KI 0.025
#define PID_PARAM_KD 0
float pid_error;
extern UART_HandleTypeDef huart1;
char* bufftr="Hello\n\r";
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
TM_LIS302DL_LIS3DSH_t Axes_Data;
MX_GPIO_Init();
MX_TIM1_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
SystemInit();
TM_DELAY_Init();
TM_DISCO_LedInit();
if (TM_LIS302DL_LIS3DSH_Detect() == TM_LIS302DL_LIS3DSH_Device_LIS302DL) {
TM_DISCO_LedOn(LED_GREEN | LED_RED);
TM_LIS302DL_LIS3DSH_Init(TM_LIS302DL_Sensitivity_2_3G, TM_LIS302DL_Filter_2Hz);
} else if (TM_LIS302DL_LIS3DSH_Detect() == TM_LIS302DL_LIS3DSH_Device_LIS3DSH) {
TM_DISCO_LedOn(LED_BLUE | LED_ORANGE);
TM_LIS302DL_LIS3DSH_Init(TM_LIS3DSH_Sensitivity_2G, TM_LIS3DSH_Filter_800Hz);
} else {
TM_DISCO_LedOn(LED_GREEN | LED_RED | LED_BLUE | LED_ORANGE);
while (1);
}
Delayms(300);
TM_DISCO_LedOff(LED_ORANGE);
TM_DISCO_LedOff(LED_BLUE);
arm_pid_instance_f32 PID;
PID.Kp = PID_PARAM_KP;
PID.Ki = PID_PARAM_KI;
PID.Kd = PID_PARAM_KD;
arm_pid_init_f32(&PID, 1);
TM_GPIO_SetPinLow(GPIOD, GPIO_Pin_12);
TM_GPIO_SetPinLow(GPIOD, GPIO_Pin_14);
while (1){
HAL_UART_Transmit_IT(&huart1, (uint8_t *)bufftr, 8);
HAL_Delay(500);
}
void SystemClock_Config(void){
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // <-----
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK){
Error_Handler();
}
}
static void MX_USART1_UART_Init(void){
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK){
Error_Handler();
}
}
static void MX_GPIO_Init(void){
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
void Error_Handler(void){
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line){
}
#endif
当我查看文件 startup_stm32f407xx.s 时,代码中包含以下几行。
; Reset handler
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT SystemInit
IMPORT __main
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP
; Dummy Exception Handlers (infinite loops which can be modified)
你认为错误在哪里?提前致谢。