本帖最后由 kingsings 于 2015-10-21 21:06 编辑
我用的是STM32F407,8MHz外部晶振,设置TIM3定时器,预分配系数84-1,重装载数2500-1,目标400Hz(2500us),但是用示波器测量是2580us,不知道这是本来就会这样,还是我的配置不对。请教高人!谢谢。
配置代码如下:
- //主函数部分
- #include "drivers_usart.h"
- #include "drivers_oled.h"
- #include "drivers_keyboard.h"
- #include "drivers_pwm.h"
- bool view;
- void Delay(__IO uint32_t nCount);
- void Delay(__IO uint32_t nCount)
- {
- while(nCount--){}
- }
- int main(void)
- {
- TIM3_PWM_Init(2499,83);
- while(1)
- {
-
- }//end of while(1)
- }
- //TIM3_PWM_Init函数
- void TIM3_PWM_Init(u32 arr,u32 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3 时钟使能
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能porta时钟
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3); //GA6复用为TIM3
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPIOA6
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseStructure.TIM_Prescaler=psc;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period=arr;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0xff;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM3, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3,ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- }// end of void TIM14_PWM_Init(u32 arr,u32 psc)
复制代码
- ************************ PLL Parameters *************************************/
- #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx)
- /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
- #define PLL_M 8 //原来为25
- #else /* STM32F411xE */
- #if defined (USE_HSE_BYPASS)
- #define PLL_M 8
- #else /* STM32F411xE */
- #define PLL_M 16
- #endif /* USE_HSE_BYPASS */
- #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */
复制代码
还有系统文件的HSE_VALUE已经设置为8000000。
|
已经贴上代码了,帮忙看一下
已经贴上代码了,帮忙看一下
已经贴上代码了,帮忙看一下
已经贴上代码了,帮忙看一下