STM32 TIMER3 OCToggle模式输出占空比可调的方波

平台:stm32+keil5
【STM32 TIMER3 OCToggle模式输出占空比可调的方波】功能:用TIMER3的OCToggle模式产生占空比非50%的方波OCToggle模式可以产生多路频率不同的方波,而PWM只能产生多路频率相同的方波。
代码如下:

因为使用了中断,所以代码有2部分:第一部分是功能配置,第二部分是中断处理:

__IO uint16_t CCR1_Val = 32768; __IO uint16_t CCR2_Val = 16384; __IO uint16_t CCR3_Val = 8192; __IO uint16_t CCR4_Val = 4096; uint16_t PrescalerValue = https://www.it610.com/article/0; /** * disable CHX output * */void DisableChxOutput(uint8_t CHX) { switch(CHX) { case 1: { /* Disable the Channel 1: Reset the CC1E Bit */ TIM3->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E); TIM_ITConfig(TIM3, TIM_IT_CC1 , DISABLE); } break; case 2: { /* Disable the Channel 2: Reset the CC2E Bit */ TIM3->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC2E); TIM_ITConfig(TIM3, TIM_IT_CC2 , DISABLE); } break; case 3: { TIM3->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC3E); TIM_ITConfig(TIM3, TIM_IT_CC3 , DISABLE); } break; case 4: { TIM3->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC4E); TIM_ITConfig(TIM3, TIM_IT_CC4 , DISABLE); } break; default: break; }}void TIMER3_PWM_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE); /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); }void TIMER3_OCToggleMode_Config(void) {NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; TIM_OCInitTypeDefTIM_OCInitStructure; /* PCLK1 = HCLK/4 */ RCC_PCLK1Config(RCC_HCLK_Div4); /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIMER3_PWM_GPIO_Configuration(); /* Enable the TIM3 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* --------------------------------------------------------------------------- TIM3 Configuration: Output Compare Toggle Mode: TIM3CLK = SystemCoreClock / 2, The objective is to get TIM3 counter clock at 12 MHz: - Prescaler = (TIM3CLK / TIM3 counter clock) - 1 CC1 update rate = TIM3 counter clock / CCR1_Val = 183.1 Hz CC2 update rate = TIM3 counter clock / CCR2_Val = 366.2 Hz CC3 update rate = TIM3 counter clock / CCR3_Val = 732.4 Hz CC4 update rate = TIM3 counter clock / CCR4_Val = 1464.8 Hz ----------------------------------------------------------------------------*/PrescalerValue = https://www.it610.com/article/(uint16_t) (SystemCoreClock / 24000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Output Compare Toggle Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM3, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val; TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* TIM IT enable */ TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); }

中断处理:修改占空比,控制输出波形的个数
uint16_t capture = 0; uint8_t gpiostat = 0; uint16_t chcnt = 0; void TIM3_IRQHandler(void) { #if 1 /* TIM3_CH1 toggling with frequency = 183.1 Hz */ if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 ); capture = TIM_GetCapture1(TIM3); TIM_SetCompare1(TIM3, capture + CCR1_Val); #if 1 if (gpiostat)//调节占空比 { TIM_SetCompare1(TIM3, capture + CCR1_Val*0.9); gpiostat = 0; } else { TIM_SetCompare1(TIM3, capture + CCR1_Val*0.1); gpiostat = 1; } chcnt++; if (chcnt >= 9)//输出个数 { DisableChxOutput(1); chcnt = 0; } #endif }/* TIM3_CH2 toggling with frequency = 366.2 Hz */ if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); capture = TIM_GetCapture2(TIM3); TIM_SetCompare2(TIM3, capture + CCR2_Val); }/* TIM3_CH3 toggling with frequency = 732.4 Hz */ if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC3); capture = TIM_GetCapture3(TIM3); TIM_SetCompare3(TIM3, capture + CCR3_Val); }/* TIM3_CH4 toggling with frequency = 1464.8 Hz */ if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC4); capture = TIM_GetCapture4(TIM3); TIM_SetCompare4(TIM3, capture + CCR4_Val); } #endif }

最后输出波形:绿色的为通道1,如果把中断里面的#if 1改成#if 0,通道1的占空比50%,通道1的频率为通道2的2倍,反之,占空比非50%,通道1的频率和通道2相同。这点要注意。
STM32 TIMER3 OCToggle模式输出占空比可调的方波
文章图片


    推荐阅读