``` HAL_RCC_GetPCLK1Freq() / (baudrate * 18);```解析
时间: 2024-06-25 12:00:55 浏览: 113
`HAL_RCC_GetPCLK1Freq()` 是一个来自 STM32 外部中断和定时器 HAL(硬件抽象层)的函数,用于获取系统时钟(通常称为 PCLK1 频率),PCLK1 是 RCC(Reset and Clock Control)模块中的一路时钟源,它通常是系统时钟(HCLK)的子时钟。
`baudrate` 是指通信中期望的波特率(bps, bits per second),比如在串口通信中设置的发送速率。
这个表达式的计算 `HAL_RCC_GetPCLK1Freq() / (baudrate * 18)` 的目的是为了计算能够达到给定 `baudrate` 的最大时钟分频因子。在许多情况下,特别是在串行通信中(如 UART),为了得到正确的波特率,需要确保发送数据的频率(即时钟频率除以分频值)与期望的 baudrate 成正比。
18 这个数通常是基于 UART 的公式计算得出的,因为一个数据位可能包含停止位、起始位和校验位,所以 18 或者类似数值代表了每个字符可能需要的时钟周期数。
相关问题
#include "main.h" #include "stdio.h" #include "string.h" #include "time.h" UART_HandleTypeDef huart1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); while (1) { time_t now = time(NULL); struct tm *timeinfo = localtime(&now); char time_str[9]; sprintf(time_str, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); HAL_UART_Transmit(&huart1, (uint8_t *)time_str, strlen(time_str), HAL_MAX_DELAY); HAL_Delay(1000); } } 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_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 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_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); /*Configure GPIO pin : PA9 */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } 在以上代码的基础上,编写代码以实现计算发送 hh:mm:ss到单片机,修改单片机时间
可以使用串口接收单片机发送过来的时间数据,然后通过C语言中的time()函数来设置单片机的时间。下面是示例代码:
```
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "time.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
// 用于接收单片机发送过来的时间数据
char time_input[9];
while (1) {
// 接收串口数据
HAL_UART_Receive(&huart1, (uint8_t *)time_input, 9, HAL_MAX_DELAY);
// 将接收到的时间数据转换成struct tm格式
struct tm timeinfo = {0};
sscanf(time_input, "%d:%d:%d", &timeinfo.tm_hour, &timeinfo.tm_min, &timeinfo.tm_sec);
// 设置单片机时间
time_t now = mktime(&timeinfo);
if(now != -1) {
if(stm32_settime(now) == 0) {
printf("Time set successfully!\r\n");
}
else {
printf("Failed to set time.\r\n");
}
}
else {
printf("Invalid time data.\r\n");
}
}
}
// 设置单片机时间
int stm32_settime(time_t t) {
struct tm *timeinfo = localtime(&t);
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
sTime.Hours = timeinfo->tm_hour;
sTime.Minutes = timeinfo->tm_min;
sTime.Seconds = timeinfo->tm_sec;
sDate.WeekDay = timeinfo->tm_wday;
sDate.Month = timeinfo->tm_mon + 1;
sDate.Date = timeinfo->tm_mday;
sDate.Year = timeinfo->tm_year - 100;
if(HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) {
return -1;
}
if(HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) {
return -1;
}
return 0;
}
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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pin : PA9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
同时,需要在单片机上添加RTC实时时钟模块,并在CubeMX中进行配置。需要注意的是,使用RTC模块前需要先初始化时钟,可以使用RCC_OscConfig和RCC_ClkConfig函数来进行初始化。在设置时间时,需要将时间数据转换成RTC模块所需的格式,并调用HAL_RTC_SetTime和HAL_RTC_SetDate函数来设置时间和日期。
stm32cubeIDE串口通信:在stm32cubemx中配置串口参数,使用USART1串口,采用中断接收函数HAL_UART_Receive_IT数据,使用DMA方式函数HAL_UART_Transmit_DMA发送数据的具体配置代码
首先,在STM32CubeMX中配置串口参数:
1. 打开STM32CubeMX,选择对应芯片型号。
2. 在Pinout & Configuration中,将USART1的TX和RX引脚设置为GPIO_Alternate_function,并设置对应的引脚号。
3. 在Configuration中,选择USART1,将Mode设置为Asynchronous,Baud Rate设置为波特率,Data Bits设置为8,Parity设置为None,Stop Bits设置为1。
4. 在NVIC Settings中,使能USART1的中断,并设置优先级。
5. 生成代码后,打开main.c文件,在MX_USART1_UART_Init函数后添加以下代码:
```c
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 开启中断接收数据
```
接下来,使用中断接收函数HAL_UART_Receive_IT读取串口数据:
```c
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
// 处理接收到的数据
}
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 继续开启中断接收数据
}
```
最后,使用DMA方式函数HAL_UART_Transmit_DMA发送数据:
```c
char tx_data[] = "hello world";
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_data, strlen(tx_data));
```
完整代码如下:
```c
#include "main.h"
#include <string.h>
UART_HandleTypeDef huart1;
char rx_data;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 开启中断接收数据
char tx_data[] = "hello world";
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_data, strlen(tx_data)); // 使用DMA方式发送数据
while (1)
{
}
}
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_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 = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_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_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
// 处理接收到的数据
}
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 继续开启中断接收数据
}
void Error_Handler(void)
{
}
```
阅读全文