stm32 打印系统频率和寄存器的值

#include "main.h"
#include "gpio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void SystemClock_Config(void);

GPIO_InitTypeDef gpio_c; // led灯gpio控制
UART_InitTypeDef uart_init1;
UART_HandleTypeDef huart1;
GPIO_InitTypeDef gpio_a; // uart1的gpio初始化

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{

    gpio_c.Pin = GPIO_PIN_13;
    gpio_c.Mode = GPIO_MODE_OUTPUT_PP;
    gpio_c.Pull = GPIO_PULLUP;
    gpio_c.Speed = GPIO_SPEED_FREQ_HIGH;

    __HAL_RCC_USART1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    uart_init1.BaudRate = 115200;

    uart_init1.WordLength = UART_WORDLENGTH_8B;
    uart_init1.StopBits = UART_STOPBITS_1;
    uart_init1.Parity = UART_PARITY_NONE;
    uart_init1.HwFlowCtl = UART_HWCONTROL_NONE;
    uart_init1.Mode = UART_MODE_TX_RX;
    uart_init1.OverSampling = UART_OVERSAMPLING_16;

    huart1.Instance = USART1;
    huart1.Init = uart_init1;

    gpio_a.Pin = GPIO_PIN_9;
    gpio_a.Mode = GPIO_MODE_AF_PP;
    gpio_a.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &gpio_a); // PA9 引脚初始化
    gpio_a.Pin = GPIO_PIN_10;
    HAL_GPIO_Init(GPIOA, &gpio_a); // PA10 引脚初始化

    HAL_Init();
    SystemClock_Config();

    MX_GPIO_Init();
    HAL_GPIO_Init(GPIOC, &gpio_c);
    HAL_UART_Init(&huart1);

    unsigned int i = 10;
    i++;

    HAL_TickFreqTypeDef freq = HAL_GetTickFreq();
    char buff[200] = {0};
    sprintf(buff, "freq=%d", freq); // print freq
    sprintf(buff, "GPIOA=%x", GPIOA->CRL);

    while (1)
    {
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
        HAL_UART_Transmit(&huart1, (uint8_t *)buff, strlen(buff), 1000);

        HAL_Delay(5000);
    }
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Initializes the RCC Oscillators according to the specified parameters
     * in the RCC_OscInitTypeDef structure.
     */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
     */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    {
        Error_Handler();
    }
}

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

https://www.nowcoder.com/discuss/460819659290394624

 

posted @ 2025-04-22 20:21  微笑的''80  阅读(4)  评论(0)    收藏  举报