qq_17256485
2017-06-13 16:45
采纳率: 100%
浏览 2.4k
已采纳

利用这个设计六路互补spwm波能否实现,今天去实验室仿真没波形,完全没学过stm32的小白制作课程设计

#include "timer.h"
#include "usart.h"

//////////////////////////////////////////////////////////////////////////////////

static void TIM_RCC_Config( void );
static void TIM_GPIO_Config( void );
static void TIM_NVIC_Config( void );
static void TIM_Config( void );
void TIM5_UPDATE(void);
#define TIM5_IRQChannel ((u8)0x32) /* TIM5 global Interrupt */
extern u16 fqc;
extern u16 fz;

/* Private functions ---------------------------------------------------------*/
void TIM_APP_Config( void )
{
TIM_RCC_Config();
TIM_GPIO_Config();
TIM_NVIC_Config();
TIM_Config();
}

void TIM_RCC_Config(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM5 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM6, ENABLE );
}

/*******************************************************************************

  • Function Name : GPIO_Configuration
  • Description : Configure the TIM1 Pins.
  • Input : None
  • Output : None
  • Return : None
    *******************************************************************************/
    void TIM_GPIO_Config(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    // GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);

    /* GPIOE Configuration: Channel 1, as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* GPIOE Configuration: BKIN pin */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /*GPIOB Configuration: as alternate function push-pull*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    }

/*******************************************************************************

  • Function Name : NVIC_Configuration
  • Description : Configures Vector Table base location.
  • Input : None
  • Output : None
  • Return : None *******************************************************************************/ void TIM_NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 /
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /
VECT_TAB_FLASH /
/
Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

/* 利用TIM5产生中断 更新SPWM占空比*/

NVIC_InitStructure.NVIC_IRQChannel= TIM5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init( &NVIC_InitStructure );

}

void TIM_Config(void)
{
/* Time Base configuration */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1 ;
TIM_TimeBaseStructure.TIM_Period = 36000/fz;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM1, ENABLE);
/* Channel 1,  Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);

//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 0;  //这里调整死区大小0-0xff
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);

//TIM1_OC2模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

//TIM1_OC3模块设置(设置3通道占空比)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

TIM5_UPDATE();

TIM_TimeBaseStructure.TIM_Prescaler= 719;
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period= 20;
TIM_TimeBaseStructure.TIM_ClockDivision= 0;

TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
TIM_SelectOutputTrigger( TIM3, TIM_TRGOSource_Update );// ADC Trigger  5KHz
TIM_Cmd( TIM3, ENABLE );

/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);

/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);

}

void TIM5_UPDATE(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler= 0;
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period= 20000/fqc;
TIM_TimeBaseStructure.TIM_ClockDivision= 0;

TIM_TimeBaseInit( TIM5, &TIM_TimeBaseStructure );
TIM_ITConfig( TIM5, TIM_IT_Update, ENABLE );
TIM_Cmd( TIM5, ENABLE );    

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • shen_wei 2017-06-14 08:22
    已采纳
    已采纳该答案
    打赏 评论
  • qq_17256485 2017-06-15 02:28

    产生的波形,,上面那个有点像,下面那个感觉干扰好大图片

    打赏 评论

相关推荐 更多相似问题