Why USART transmits incorrect data as the APB1 frequency changes. (RCC <= 21MHz)

两盒软妹~` 提交于 2019-12-24 19:15:35

问题


I want to use the USART communication protocol in my project. Communication is provided but incorrect data is sent (STM> PC).

I'd try:

  1. Boundrade bands are the same.

  2. Suitable combinations of PLL Source Mux (HSI-HSE) and System Clock Mux (HSI-HSE-PLLCLK) were tested. Available: HSE and PLLCLK

  3. APB1 Clock frequency was changed within the allowed range. It was observed that the data obtained at each change also changed. Sometimes STM sent very fast data.

  4. The STM card was fed from a different source and tested by ground equalization.

Note-1: Codes do not include the entire project. In this case, the problem that I mentioned occurs.

#include "main.h"
#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

Note-2: The problem does not occur when I create and run a project containing only USART when all the conditions are the same.

Note-3: I do not receive any error messages or warning messages.


回答1:


SystemInit() should be called in the reset handler only

This function is supposed to reset the system clock to a default state, running directly from HSI at 16 MHz.

You are calling it in the middle of main(), when USART1 is already configured assuming a higher clock speed. There is no telling what the peripheral would do when its clock source is changed on the fly.



来源:https://stackoverflow.com/questions/56608971/why-usart-transmits-incorrect-data-as-the-apb1-frequency-changes-rcc-21mhz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!