STM32高级定时器输出多路互补PWM

STM32高级定时器输出多路互补PWM,第1张

STM32高级定时器输出多路互补PWM

单片机型号使用的是STM32F103RCT6,103系列不同型号的单片机只需要查看手册重新配置引脚即可,STM32的两个高级定时器TIM1和TIM8都可以输出互补PWM,但是每个定时器只能输出三路互补PWM,本文使用TIM8同时输出三路互补PWM,代码如下:

timer.h头文件

#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"

void TIM8_Init(u32 arr, u32 psc);
#endif

timer.c文件

#include "timer.h"


void TIM8_Init(u32 arr, u32 psc)

{
		GPIO_InitTypeDef         GPIO_InitStructure;
		TIM_TimebaseInitTypeDef TIM_TimebaseInitStructure;
		TIM_OCInitTypeDef       TIM_OCInitStructure;
		TIM_BDTRInitTypeDef      TIM_BDTRInitStructure;
		//´®¿Ú³õʼ»¯
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 | RCC_APB2Periph_GPIOC | 
  RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); 

	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOC,&GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = 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_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
	
		//¶¨Ê±Æ÷ÉèÖÃ
		TIM_DeInit(TIM8);
		TIM_TimebaseInitStructure.TIM_Period = arr;     
		TIM_TimebaseInitStructure.TIM_Prescaler = psc;  
		TIM_TimebaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;    
		TIM_TimebaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimebaseInit(TIM8,&TIM_TimebaseInitStructure); 
    //ͨµÀ1 ÉèÖÃÊä³ö±È½Ïģʽ
		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_Reset;     
		TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
		TIM_OC1Init(TIM8, &TIM_OCInitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = 0;
		TIM_OC2Init(TIM8, &TIM_OCInitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = 0;
		TIM_OC3Init(TIM8, &TIM_OCInitStructure);
		
		TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);
		TIM_OC2PreloadConfig(TIM8,TIM_OCPreload_Enable);
		TIM_OC3PreloadConfig(TIM8,TIM_OCPreload_Enable);

		TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
		TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
		TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
		TIM_BDTRInitStructure.TIM_DeadTime = 0;
		TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;             //½ûֹɲ³µÊäÈë  
		TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
		TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
		TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);

		TIM_Cmd(TIM8, ENABLE);
		TIM_CCPreloadControl(TIM8,ENABLE);
		TIM_CtrlPWMOutputs(TIM8, ENABLE);
}

主函数如下:

 int main(void)
{
    u16 ccr=250;//占空比
	delay_init();	  
    uart_init(115200);
    TIM8_Init(2000-1,72-1); //调节PWM波的频率  F=72000000/2000/72=500Hz
while(1)
	{    
//改变ccr的值改变三路互补PWM的占空比
	   TIM_SetCompare1(TIM8,ccr);
       TIM_SetCompare2(TIM8,ccr);	
	   TIM_SetCompare3(TIM8,ccr);
	
	}
}

使用示波器查看其中一路互补波形如下:

昨天忘记设置占空比了所以没有正常输出。希望本文可以帮助到大家 !

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5635772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存