TIM1 配置 void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /***********************************PWM端口配置********************************/ // /* RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 |GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;//GPIO_OType_OD;//GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_DOWN;//GPIO_PuPd_UP;//GPIO_PuPd_NOPULL GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2); GPIO_PinLockConfig(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ); ///* GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 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_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_2); GPIO_PinLockConfig(GPIOB, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ); //*/ /* GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;//GPIO_PuPd_DOWN;//GPIO_PuPd_DOWN; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_13); GPIO_SetBits(GPIOB, GPIO_Pin_14); GPIO_SetBits(GPIOB, GPIO_Pin_15); */ /***********************************PWM端口配置********************************/ Period = 3199; //2014-8-27 PWM 频率 15K MotorA.PWM_Freq = BLDC_PWM_Freq; MotorA.TimerPeriod = Period; Pluse = 0; //默认占空比30% TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);///++++++++++++++++ TIM_TimeBaseStructure.TIM_Prescaler = 0;//预分频 TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Down;//TIM_CounterMode_Down;//向下计数 TIM_TimeBaseStructure.TIM_Period = Period;//3199周期 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//设置周期计数值 TIM_TimeBaseInit(BLDC_TIMER_NUM, &TIM_TimeBaseStructure); TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//+++++++++++++ TIM_OCStructInit(&TIM_OCInitStructure); /* Channel 1, 2,3 and 4 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; ////使能输出比较状态 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//互补状态使能 TIM_OCInitStructure.TIM_Pulse = Pluse;//比较计数值 到达这个值发生跳变 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当达到计数器跳变是高电平 8 9 10 上桥高电平有效 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;//TIM_OCNPolarity_High;//TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState =TIM_OCIdleState_Set;//TIM_OCIdleState_Reset; //2015-1-22 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;//TIM_OCNIdleState_Reset;//2015-1-22 TIM_OC1Init(BLDC_TIMER_NUM, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_OC2Init(BLDC_TIMER_NUM, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_OC3Init(BLDC_TIMER_NUM, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /**************************************************************************/ TIM_OCStructInit(&TIM_OCInitStructure); /* Channel 4 Configuration in OC */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = 10; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC4Init(TIM1, &TIM_OCInitStructure); /* Enables the TIM1 Preload on CC1 Register */ //TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Enables the TIM1 Preload on CC2 Register */ //TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Enables the TIM1 Preload on CC3 Register */ //TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Enables the TIM1 Preload on CC4 Register */ //TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Automatic Output enable, Break, dead time and lock configuration*/ TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStructure.TIM_LOCKLevel =TIM_LOCKLevel_OFF;//TIM_LOCKLevel_OFF; TIM_BDTRInitStructure.TIM_DeadTime = 48;//48=1us; 原来为96 死区时间 TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; TIM_BDTRConfig(BLDC_TIMER_NUM, &TIM_BDTRInitStructure); TIM_CCxCmd(BLDC_TIMER_NUM, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(BLDC_TIMER_NUM, TIM_Channel_1, TIM_CCxN_Disable); TIM_CCxCmd(BLDC_TIMER_NUM, TIM_Channel_2, TIM_CCx_Disable); TIM_CCxNCmd(BLDC_TIMER_NUM, TIM_Channel_2, TIM_CCxN_Disable); TIM_CCxCmd(BLDC_TIMER_NUM, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(BLDC_TIMER_NUM, TIM_Channel_3, TIM_CCxN_Disable); TIM_CCxCmd(BLDC_TIMER_NUM, TIM_Channel_4, TIM_CCx_Enable); TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); //定时器捕获比较中断 NVIC_InitStructure.NVIC_IRQChannel = BLDC_TIM_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); while(1) { //TIM1->CCR1 = 1000; //AH TIM1->CCR2 = 1000; //BH // TIM1->CCR3 = 0; //CH // TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable); TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable); // TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable); // TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); // TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); TIM_CtrlPWMOutputs(TIM1, ENABLE); } }
|