void TIM4_PWM_init(CHn ch, u32 freq, u32 duty) { TIM_TimeBaseInitTypeDef TIM4_TimeBaseStructure;//¶¨Òå½á¹¹Ìå TIM_OCInitTypeDef TIM4_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);//ʹÄÜGPIOD¼°¹¦Äܸ´ÓÃIOʱÖÓ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//ʹÄÜTIM4ʱÖÓ //GPIOAÅäÖÃΪ£º¶¨Ê±Æ÷4µÄPWM4¸öͨµÀ¸´Óù¦ÄÜÊä³ö GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//I/OʱÖÓΪ50MHz GPIO_Init(GPIOD, &GPIO_InitStructure);//¸ù¾ÝÉÏÃæÖ¸¶¨²ÎÊý³õʼ»¯GPIO½á¹¹Ìå GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //½«TIM4Òý½ÅÍêÈ«ÖØÓ³Éä //¶¨Ê±Æ÷»ù±¾ÅäÖà TIM4_TimeBaseStructure.TIM_Period =(u16)(36000000/freq-1);//¼ÆÊýֵΪ1000 TIM4_TimeBaseStructure.TIM_Prescaler = 2-1;//2·ÖƵ 36M TIM4_TimeBaseStructure.TIM_ClockDivision = 0;//²ÉÑù·ÖƵ0 TIM4_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//ÏòÉϼÆÊý TIM_TimeBaseInit(TIM4, &TIM4_TimeBaseStructure); TIM4_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//¶¨Ê±Æ÷ÅäÖÃΪPWM1ģʽ TIM4_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIMÊä³ö±È½Ï¼«ÐÔ¸ß // PWM1ģʽͨµÀ1 if(ch==1) { TIM4_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//ͨµÀ1Êä³öʹÄÜ TIM4_OCInitStructure.TIM_Pulse =(u16)(36000000/freq/PWM_precision*duty);//Âö¿íֵΪ200 TIM_OC1Init(TIM4,&TIM4_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIM2 TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM2ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷ } // PWM1ģʽͨµÀ2 if (ch==2) { TIM4_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM4_OCInitStructure.TIM_Pulse =(u16)(36000000/freq/PWM_precision*duty);//Âö¿íֵΠTIM_OC2Init(TIM4, &TIM4_OCInitStructure); TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);//ʹÄÜTIM2ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷ } // PWM1ģʽͨµÀ3 if (ch==3) { TIM4_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM4_OCInitStructure.TIM_Pulse =(u16)(36000000/freq/PWM_precision*duty); TIM_OC3Init(TIM4, &TIM4_OCInitStructure); TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); } // PWM1ģʽͨµÀ4 if (ch==4) { TIM4_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM4_OCInitStructure.TIM_Pulse =(u16)(36000000/freq/PWM_precision*duty); TIM_OC4Init(TIM4, &TIM4_OCInitStructure); TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); } TIM_ARRPreloadConfig(TIM4, ENABLE);//ʹÄÜTIM2ÔÚARRÉϵÄԤװÔؼĴæÆ÷ TIM_ClearITPendingBit(TIM4, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4);//Ô¤ÏÈÇå³ýËùÓÐÖжÏλ /* ÅäÖÃÊä³ö±È½ÏͨµÀ1ÖжÏ*/ TIM_ITConfig(TIM4,TIM_IT_CC1,DISABLE); TIM_Cmd(TIM4, ENABLE);//ʹÄܶ¨Ê±Æ÷2 } |
RE:STM32PWM产生30K以上的频率就不行了,大家给看看程序!!
下面是我产生38KHz,占空比为1/3的程序,你可以看看
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Õâ¾ä±ØÐë·ÅÔÚ×îÉÏÃæ
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1894;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1894/3;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
RE:STM32PWM产生30K以上的频率就不行了,大家给看看程序!!
下面是我产生38KHz,占空比为1/3的程序,你可以看看
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Õâ¾ä±ØÐë·ÅÔÚ×îÉÏÃæ
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1894;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1894/3;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
回复:STM32PWM产生30K以上的频率就不行了,大家给看看程序!!
extern unsigned char Pwm_freg ;
unsigned char Pwm_duty_num =20 ; // 50kHZ
extern volatile unsigned short int Pwm_period_num ;
unsigned short int CCR1_Val;
unsigned short int CCR2_Val;
//unsigned short int CCR3_Val = 50;
//unsigned short int CCR4_Val = 20;
/*******************************************************************************
* 函数名称: Timer3_PWM_Init();
* 功能描述: 定时器3初始化--用于产生四路PWM
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void Timer3_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
CCR1_Val = (unsigned short int)(50*(Pwm_period_num+1)/100);
CCR2_Val = (unsigned short int)(50*(Pwm_period_num+1)/100);
//时钟配置
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , ENABLE); // 使能GPIOB端口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 打开定时器3的时钟
//TIM3-CH1-CH4GPIO通道配置--PE2-PE5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
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(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(F303C_PWM3_CH1_P0RT , F303C_PWM3_CH1_SOURCE, F303C_PWM3_CH1_AF);
GPIO_PinAFConfig(F303C_PWM3_CH2_P0RT , F303C_PWM3_CH2_SOURCE, F303C_PWM3_CH2_AF);
GPIO_PinAFConfig(F303C_PWM3_CH3_P0RT , F303C_PWM3_CH3_SOURCE, F303C_PWM3_CH3_AF);
GPIO_PinAFConfig(F303C_PWM3_CH4_P0RT , F303C_PWM3_CH4_SOURCE , F303C_PWM3_CH4_AF);
//设置定时器相关参数
TIM_TimeBaseInitStructure.TIM_Prescaler = 71; // 定时器时钟分频后: 72000000 / (72 + 1) = 1MHZ.
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseInitStructure.TIM_Period = Pwm_period_num ; // 自动重装载计数器周期的值--默认50KHZ
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频系数--不分频
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); // TIM2, 每1ms中断一次.
//PWM模式设置-CH3
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
//PWM模式设置-CH4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}