STM32通用定时器产生多通道PWM输出,会相互影响,怎么办????#define_CH11
#define_CH22
#define_CH33
#define_CH44
#define_CH55
#define_CH66
#define_CH77
#define_CH88
#defineCH1_CH4_PWM_GPIO_CLKRCC_APB2Periph_GPIOC
#defineCH1_CH4_PWM_CLKRCC_APB1Periph_TIM3
#defineCH1_CH4_PWMTIM3
#defineCH1_PWM_GPIO_PORTGPIOC
#defineCH1_PWM_PINGPIO_Pin_9
#defineCH2_PWM_GPIO_PORTGPIOC
#defineCH2_PWM_PINGPIO_Pin_8
#defineCH3_PWM_GPIO_PORTGPIOC
#defineCH3_PWM_PINGPIO_Pin_7
#defineCH4_PWM_GPIO_PORTGPIOC
#defineCH4_PWM_PINGPIO_Pin_6
#defineCH5_CH8_PWM_GPIO_CLKRCC_APB2Periph_GPIOB
#defineCH5_CH8_PWM_CLKRCC_APB1Periph_TIM4
#defineCH5_CH8_PWMTIM4
#defineCH5_PWM_GPIO_PORTGPIOB
#defineCH5_PWM_PINGPIO_Pin_9
#defineCH6_PWM_GPIO_PORTGPIOB
#defineCH6_PWM_PINGPIO_Pin_8
#defineCH7_PWM_GPIO_PORTGPIOB
#defineCH7_PWM_PINGPIO_Pin_7
#defineCH8_PWM_GPIO_PORTGPIOB
#defineCH8_PWM_PINGPIO_Pin_6
#definePWM_Freq(500-1)
/*******************************************************************************
**名称:vLight_SetPWMInit
**描述:各通道PWM输出配置;
**开启外设时钟及配置输出模式
**
**输入:无
**输出:无
********************************************************************************/
voidvLight_SetPWMConfig(void)
{
/*PWM输出GPIO配置*/
vLight_PWMGpioInit();
/*PWM输出模式配置*/
vLight_PWMModeInit();
}
/*******************************************************************************
**名称:vLight_PWMGpioInit
**描述:PWM输出GPIO配置;
**开启外设时钟及配置输出模式
**
**输入:无
**输出:无
********************************************************************************/
voidvLight_PWMGpioInit(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*使能外设时钟*/
RCC_APB2PeriphClockCmd(CH1_CH4_PWM_GPIO_CLK
CH5_CH8_PWM_GPIO_CLK
RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(CH1_CH4_PWM_CLK
CH5_CH8_PWM_CLK,ENABLE);
/*引脚重映*/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
/*配置vLight_PWMGpio*/
GPIO_InitStructure.GPIO_Pin=CH1_PWM_PIN
CH2_PWM_PIN
CH3_PWM_PIN
CH4_PWM_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(CH1_PWM_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=CH5_PWM_PIN
CH6_PWM_PIN
CH7_PWM_PIN
CH8_PWM_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(CH5_PWM_GPIO_PORT,&GPIO_InitStructure);
}
/*******************************************************************************
**名称:vLight_PWMModeInit
**描述:PWM输出模式配置;
**设置PWM工作频率,计数模式,占空比
**
**输入:无
**输出:无
********************************************************************************/
voidvLight_PWMModeInit(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
//TIM_BDTRInitTypeDefTIM_BDTRInitStructure;
//定时器工作在单脉冲下
TIM_DeInit(CH1_CH4_PWM);
TIM_TimeBaseStructure.TIM_Period=PWM_Freq;//计数值,即定时周期
TIM_TimeBaseStructure.TIM_Prescaler=0;//预分频,不分频为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分频系数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(CH1_CH4_PWM,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//在向上计数
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
/*配置通道1*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC4Init(CH1_CH4_PWM,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
/*配置通道2*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC3Init(CH1_CH4_PWM,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
/*配置通道3*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC2Init(CH1_CH4_PWM,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
/*配置通道4*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC1Init(CH1_CH4_PWM,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(CH1_CH4_PWM,ENABLE);
TIM_Cmd(CH1_CH4_PWM,ENABLE);//先不使能能TIM
//定时器工作在单脉冲下
TIM_DeInit(CH5_CH8_PWM);
TIM_TimeBaseStructure.TIM_Period=PWM_Freq;//计数值,即定时周期
TIM_TimeBaseStructure.TIM_Prescaler=0;//预分频,不分频为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分频系数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(CH5_CH8_PWM,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//在向上计数
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
/*配置通道5*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC4Init(CH5_CH8_PWM,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
/*配置通道6*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC3Init(CH5_CH8_PWM,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
/*配置通道7*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC2Init(CH5_CH8_PWM,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
/*配置通道8*/
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC1Init(CH5_CH8_PWM,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(CH5_CH8_PWM,ENABLE);
TIM_Cmd(CH5_CH8_PWM,ENABLE);//先不使能能TIM
}
/*******************************************************************************
**名称:vLight_PWMModeInit
**描述:PWM输出模式配置;
**设置PWM工作频率,计数模式,占空比
**
**输入:无
**输出:无
********************************************************************************/
voidvLight_SetPWM(uint8_tuCH,uint16_tuCycle)
{
switch(uCH)
{
case_CH1:
{
TIM_SetCompare4(CH1_CH4_PWM,uCycle);
break;
}
case_CH2:
{
TIM_SetCompare3(CH1_CH4_PWM,uCycle);
break;
}
case_CH3:
{
TIM_SetCompare2(CH1_CH4_PWM,uCycle);
break;
}
case_CH4:
{
TIM_SetCompare1(CH1_CH4_PWM,uCycle);
break;
}
case_CH5:
{
TIM_SetCompare4(CH5_CH8_PWM,uCycle);
break;
}
case_CH6:
{
TIM_SetCompare3(CH5_CH8_PWM,uCycle);
break;
}
case_CH7:
{
TIM_SetCompare2(CH5_CH8_PWM,uCycle);
break;
}
case_CH8:
{
TIM_SetCompare1(CH5_CH8_PWM,uCycle);
break;
}
default:break;
}
}
复制代码
不知道你想产生什么样的信号,别那一大堆毛病代码出来让人给你看病,好么 不会吧。
估计是你寄存器设置有问题。
铁粉芯电感器磁芯选择方法电源新手,没有经验,在做一个BUCK电路,用铁粉芯做电感,因为在直流偏磁下,铁粉芯磁导率会跌落,然后我设计的时候按照书上规定了跌落不能超过20%,然后选电感磁芯的时候,发现需要很大的
|