最近在使用正点原子的阿波罗
开发板做
tiM14的PWM输出实验 要求输出30Hz的。对比手册,TIM14挂在APB1上,APB
时钟为54MHz,可实际使用中发现TIM14的时钟源输出的是432MHz,查看代码,只有在系统时钟初始化时将PLL设为432MHz,代码如下:
GPIO_InitTypeDef GPIO_Initure;
TIM_HandleTypeDef TIM14_Handle;
TIM_OC_InitTypeDef TIM14_CH1Handle;
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_TIM14_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_9; //PF9 ϽµÑØ´¥·¢£¬ÉÏÀ
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //¸´ÓÃÍÆÍì
GPIO_Initure.Pull=GPIO_PULLUP; //ÉÏÀ
GPIO_Initure.Alternate = GPIO_AF9_TIM14;
HAL_GPIO_Init(GPIOF,&GPIO_Initure);
//base sconfig
TIM14_Handle.Instance = TIM14;
TIM14_Handle.Init.Period = 100;
TIM14_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
TIM14_Handle.Init.Prescaler = (36000 - 1);
TIM14_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM14_Handle.Init.RepetitionCounter = 0; //ÂíÉϸüÐÂARR
HAL_TIM_Base_Init(&TIM14_Handle);
//OC config
TIM14_CH1Handle.OCMode=TIM_OCMODE_PWM1;
TIM14_CH1Handle.Pulse = 50;
TIM14_CH1Handle.OCPolarity=TIM_OCPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel(&TIM14_Handle,&TIM14_CH1Handle,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM14_Handle,TIM_CHANNEL_1);
432M/4/36000 = 3KHz
计数100 刚好30Hz 示波器
测量也是30Hz
APB1总线被配置为54MHz, 当APB1分频数不为1时 TIM时钟要倍频 所以TIM时钟为108MHz
{
HAL_StatusTypeDef ret = HAL_OK;
RCC_OscInitTypeDef RCC_OscInitStructure;
RCC_ClkInitTypeDef RCC_ClkInitStructure;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStructure.OscillatorType=RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStructure.HSEState=RCC_HSE_ON;
RCC_OscInitStructure.PLL.PLLState=RCC_PLL_ON;
RCC_OscInitStructure.PLL.PLLSource=RCC_PLLSOURCE_HSE;
RCC_OscInitStructure.PLL.PLLM=pllm;
RCC_OscInitStructure.PLL.PLLN=plln;
RCC_OscInitStructure.PLL.PLLP=pllp;
RCC_OscInitStructure.PLL.PLLQ=pllq;
ret=HAL_RCC_OscConfig(&RCC_OscInitStructure);
if(ret!=HAL_OK) while(1);
ret=HAL_PWREx_EnableOverDrive();
if(ret!=HAL_OK) while(1);
RCC_ClkInitStructure.ClockType=(RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStructure.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStructure.AHBCLKDivider=RCC_SYSCLK_DIV1;
RCC_ClkInitStructure.APB1CLKDivider=RCC_HCLK_DIV4;
RCC_ClkInitStructure.APB2CLKDivider=RCC_HCLK_DIV2;
ret=HAL_RCC_ClockConfig(&RCC_ClkInitStructure,FLASH_LATENCY_7);
if(ret!=HAL_OK) while(1);
}
外部HSE 25MHz晶振,先25分频得到1MHz作为VCO的输入, 然后432倍频得到PLL时钟432MHz。
后面就是SysCLK = PLLCLK/2 = 216MHz AHB = 216MHz APB1 = 216/4 = 54MHz APB2 = 216/2 = 108M USB/SDIO CLK = PLLCLK/9 = 48MHz
432MHz 是VCO的频率,手册上PLLQ范围为2-15顺便看了下HAL库里面的写法 也没有提到有2的限制,可能是cube的限制吧
一周热门 更多>