最近做了STM32F103互补PWM带死区频率占空比可调,现在来分享一下。
从STM32的datashee可以看到下图
互补PWM我现在选择的是TIM1的通道1通道2通道3
上图可看出复用脚PA6 PA7 PB0 PB1
不复用的脚PB12 PB13 PB14 PB15,可以根据自己的引脚需求选择复用或者不复用,这些脚是输出互补的引脚,正向PWM引脚为PA8 PA9 PA10 PA11
这个功能其实也挺简单的,下面直接看附件代码。
单片机源程序如下:
- #include "PWM_Deadline.h"
- uint16_t CC1_Value = 1000,CC2_Value = 10000,CC3_Value = 20000;
- void TIM3_PWMShiftInit(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStruct;
- TIM_BDTRInitTypeDef TIM1_BDTRInitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_6 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStruct);
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStruct);
-
- GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);
- //// GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
- //TIM1_TIM2_TIM16
- TIM_TimeBaseStructure.TIM_Period = 65534;//计数1000
- TIM_TimeBaseStructure.TIM_Prescaler = 71;//48/n
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- 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 = 100;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
-
- //配置死区
- TIM1_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Disable;
- TIM1_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Disable;
- TIM1_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
- TIM1_BDTRInitStruct.TIM_DeadTime = 10;
- TIM1_BDTRInitStruct.TIM_Break = TIM_Break_Disable;
- TIM1_BDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_High;
- TIM1_BDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
- TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct);
- // TIM_OCInitStructure.TIM_Pulse = 900;
- // TIM_ICInit(TIM1, &TIM_OCInitStructure);
- // TIM_CCPreloadControl(TIM1, TIM_OCPreload_Enable);
- TIM_OCInitStructure.TIM_Pulse = CC1_Value;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- // TIM_Cmd(TIM1, ENABLE);
- // TIM_CtrlPWMOutputs(TIM1, ENABLE);
-
- TIM_OCInitStructure.TIM_Pulse = CC2_Value;
- TIM_OC2Init(TIM1, &TIM_OCInitStructure);//CH4可以正确输出,但CH1不行,全速转动,为什么?
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
-
- TIM_OCInitStructure.TIM_Pulse = CC3_Value;
- TIM_OC3Init(TIM1, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
-
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
-
- // TIM_ITConfig(TIM1, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
-
- // TIM_ARRPreloadConfig(TIM1, ENABLE);
- }
- uint16_t Fre;
- void Dynami_Fre(void)
- {
- TIM1->ARR = Fre; //可调频率
- if (Fre > 10000) Fre = 10000;
- if (Fre < 1000) Fre = 1000;
- Fre ++;
- TIM_SetCompare1(TIM1, 100); //占空比 = 100 / TIM1->ARR;
- TIM_SetCompare2(TIM1, 200); //占空比 = 200 / TIM1->ARR;
- TIM_SetCompare3(TIM1, 300); //占空比 = 300 / TIM1->ARR;
-
- }
- uint16_t value1 = 0;
- void TIM1_CC_IRQHandler(void)
- {
- uint16_t capture;
- if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
- {
- TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
- capture = TIM_GetCapture1(TIM1);
- if (value1 > 20000) value1 = 20000;
- TIM_SetCompare1(TIM1, 10000);
- TIM1->ARR = 15000;
- }
- // if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)
- // {
- // TIM_ClearITPendingBit(TIM1, TIM_IT_CC2 );
- // capture = TIM_GetCapture2(TIM1);
- // if (value1 > 20000) value1 = 20000;
- // TIM_SetCompare1(TIM1, capture + CC2_Value/2);
- // }
- // if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
- // {
- // TIM_ClearITPendingBit(TIM1, TIM_IT_CC3 );
- // capture = TIM_GetCapture3(TIM1);
- // if (value1 > 20000) value1 = 20000;
- // TIM_SetCompare1(TIM1, capture + CC3_Value/2);
- // }
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
复制代码
所有资料51hei提供下载:
master.7z
(303.03 KB, 下载次数: 97)
|