RCC_PLL_MUL9
时间: 2024-04-03 22:30:03 浏览: 240
RCC_PLL_MUL9是指STM32系列微控制器中的一个寄存器位,用于设置PLL(Phase-Locked Loop)的倍频因子。PLL是一种电路,用于将输入时钟信号倍频以获得更高的时钟频率。在STM32中,PLL用于生成系统时钟,以提供给微控制器的各个模块使用。
RCC_PLL_MUL9表示将输入时钟信号倍频9倍。具体来说,当将RCC_PLL_MUL9设置为1时,输入时钟信号将被乘以9倍,从而得到更高的系统时钟频率。
相关问题:
1. 什么是PLL?
2. STM32中的PLL有什么作用?
3. 为什么需要将输入时钟信号倍频?
相关问题
#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函数来设置时间和日期。
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
这段代码是什么意思?
这段代码是设置STM32芯片的时钟源,其中RCC_PLL_MUL16表示将PLL倍频系数设置为16,即将输入时钟频率乘以16倍。这个设置可以提高系统时钟的频率,从而提高芯片的运行速度。同时,该代码设置了一些其他的时钟参数,如RCC_OscInitStruct.PLL.PLLMUL,通过这些参数的设置,可以精确地控制系统时钟的频率。
阅读全文