STM32F429定时器时钟配置问题
我现在使用TIMER3和中断来做一个LED灯的闪烁功能,在Timer3的中断中将LED灯电平翻转,Timer3使用的是APB1的时钟,按我的配置应该是90MHz,我配置Timer3的TIM_Prescaler=18000,故Timer3的时钟就是5kHz,那么在设置TIM_Period=2000-1的情况下,得到的情况应该是高低电平各0.4秒,周期为0.8秒。但是实际情况如下图:下面贴上我的程序:
void SysInit(void)
{
//Resets the RCC clock configuration to the default reset state
RCC_DeInit();
//Configures the External High Speed oscillator (HSE)
RCC_HSEConfig(RCC_HSE_ON);
//Waits for HSE start-up
while( RCC_WaitForHSEStartUp() != SUCCESS );
//Configures the main PLL clock source, multiplication and division factors
//M=8,N=360,P=16,Q=7
RCC_PLLConfig(RCC_PLLSource_HSE,8,360,16,7);
//Enables or disables the main PLL
RCC_PLLCmd(ENABLE);
//Configures the system clock (SYSCLK)
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//Configures the AHB clock (HCLK)
RCC_HCLKConfig(RCC_HCLK_Div1);
//Configures the Low Speed APB clock (PCLK1)
RCC_PCLK1Config(RCC_HCLK_Div4);
//Configures the High Speed APB clock (PCLK2).
RCC_PCLK2Config(RCC_HCLK_Div2);
}
void TIMER_cfg(void)
{
/*Define Time3 Initial type structure*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*Deinit Time3*/
TIM_DeInit(TIM3);
/*Enable Timer3 Clock*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/*Using internal clock for TIM3*/
TIM_InternalClockConfig(TIM3);
/*What's this?*/
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
/*CK_CNT=90000000/18000=5kHz*/
TIM_TimeBaseStructure.TIM_Prescaler=18000;
/*Counting mode is up*/
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
/*Specifies the period value to be loaded into the active Auto-Reload Register at the next update event*/
TIM_TimeBaseStructure.TIM_Period=2000-1;
/*It will generate an interrupt when the overfow numberreach the RepetitionCounter number*/
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/*Clear up the Time updata flag*/
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
/*Disable the preload buffer of arr*/
TIM_ARRPreloadConfig(TIM3,DISABLE);
/*Enable the TIM3 IT*/
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
}
我找到问题所在了, RCC_PLLConfig(RCC_PLLSource_HSE,8,360,16,7);这里设置有问题,应该将16改为2。我使用RCC_GetClocksFreq(&Clock);这个函数查到的。还有就是如我想了解RCC_PLLConfig(RCC_PLLSource_HSE,8,360,16,7);函数中的参数,具体大家找函数中的解释吧,或者查固件库手册,希望对大家会有所帮助。 还没等大家看自己就解决了,代码供大家参考吧 *=============================================================================
* Supported STM32F42xxx/43xxx devices
*-----------------------------------------------------------------------------
* System Clock source | PLL (HSE)
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 180000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 180000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 4
*-----------------------------------------------------------------------------
* APB2 Prescaler | 2
*-----------------------------------------------------------------------------
* HSE Frequency(Hz) | 25000000
*-----------------------------------------------------------------------------
* PLL_M | 25
*-----------------------------------------------------------------------------
* PLL_N | 360
*-----------------------------------------------------------------------------
* PLL_P | 2
*-----------------------------------------------------------------------------
* PLL_Q | 7
*-----------------------------------------------------------------------------
* PLLI2S_N | NA
*-----------------------------------------------------------------------------
* PLLI2S_R | NA
*-----------------------------------------------------------------------------
* I2S input clock | NA
*-----------------------------------------------------------------------------
* VDD(V) | 3.3
*-----------------------------------------------------------------------------
* Main regulator output voltage | Scale1 mode
*-----------------------------------------------------------------------------
* Flash Latency(WS) | 5
*-----------------------------------------------------------------------------
* Prefetch Buffer | ON
*-----------------------------------------------------------------------------
* Instruction cache | ON
*-----------------------------------------------------------------------------
* Data cache | ON
*-----------------------------------------------------------------------------
* Require 48MHz for USB OTG FS, | Disabled
* SDIO and RNG clock |
*-----------------------------------------------------------------------------
*=============================================================================#if !defined(HSE_VALUE)
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */楼主可以直接修改stm32f4xx.h和system_stm32f4xx.c中的HSE_VALUE 、PLL_M这两个参数就够了,其他的都不用改,更不用多一个配置时钟的函数!
页:
[1]