本帖最后由 sunismyself 于 2014-5-22 17:49 编辑
void PwmIgbtInit(void)
{
uint16_t temp = 0;
uint16_t a = 0;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
PwmIgbtGpioInit();
/* TIM1 Configuration ---------------------------------------------------
Generates 7 PWM signals with 4 different duty cycles:
TIM1CLK = 72 MHz, Prescaler = 0, TIM1 counter clock = 72 MHz
TIM1 frequency = TIM1CLK/(TIM1_Period + 1) = 17.57 KHz
- TIM1 Channel1 & Channel1N duty cycle = TIM1->CCR1 / (TIM1_Period + 1) = 50%
- TIM1 Channel2 & Channel2N duty cycle = TIM1->CCR2 / (TIM1_Period + 1) = 37.5%
- TIM1 Channel3 & Channel3N duty cycle = TIM1->CCR3 / (TIM1_Period + 1) = 25%
- TIM1 Channel4 duty cycle = TIM1->CCR4 / (TIM1_Period + 1) = 12.5%
----------------------------------------------------------------------- */
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 4 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
// TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM_DeadTime = 0xa0; /* 3.55us */
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道1)
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道2)
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道3)
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2); //输入触发源选择TIM3
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ClearITPendingBit( TIM1, TIM_IT_COM);
TIM_ITConfig(TIM1, TIM_IT_COM ,ENABLE);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM1->SMCR|=0x0006; //开TIM1的输入触发
TIM1->DIER=0x0040; //开TIM1的触发中断
// TIM1->CCER &= ~(1<<9); //OC3高极性
// TIM1->CCER &= ~(1<<11); //OC3N高极性
//
// TIM1->CCER &= ~(1<<1); //OC1高极性
// TIM1->CCER &= ~(1<<3); //OC1N高极性
//
// TIM1->CCER &= ~(1<<5); //OC2高极性
// TIM1->CCER &= ~(1<<7); //OC2N高极性
//
// TIM1->CCR1 = 1000;
// TIM1->CCR2 = 1000;
// TIM1->CCR3 = 1000;
// temp = 500;
// while(temp)
// {
// for(a=50;a>0;a--);
// temp --;
// }
}
void BldcHallTimeInit(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_ICInitTypeDef TIM_ICInitStructure; //定义结构体变量
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
BldcGpioInit();
TIM_DeInit(TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 71; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period =65535;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //选择通道1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //输入上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC; //配置通道为输入,并映射到哪里
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入捕获预分频值
TIM_ICInitStructure.TIM_ICFilter = 10; //输入滤波器带宽设置
TIM_ICInit(TIM3, &TIM_ICInitStructure); //输入通道配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =1023;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse =65535;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_SelectHallSensor(TIM3,ENABLE); //使能TIMx的霍尔传感器接口
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1F_ED); //输入触发源选择
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //从模式选择
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//主从模式选择
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref); //选择输出触发模式(TRGO端)
TIM_ITConfig(TIM3, TIM_IT_Trigger|TIM_IT_CC4, ENABLE); //开定时器中断
TIM_Cmd(TIM3,ENABLE);
TIM3->CR1|=0x0001; //开TIM3
TIM3->DIER|=0x0050; //开TIM3中断
}
|