C语言:TIM1模块产生PWM,带死区//Step1.开启TIM和相应端口时钟//启动GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|\RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,\ENABLE);//启动AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//启动TIM1RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//Step2.GPIO做相应设置,为AF输出//PA.8/9口设置为TIM1的OC1输出口GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);//PB.13/14口设置为TIM1_CH1N和TIM1_CH2N输出口GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);//Step3.TIM模块初始化voidTIM_Configuration(void){TIM_TimeBaseInitTypeDefTIM_BaseInitStructure;TIM_OCInitTypeDefTIM_OCInitStructure;TIM_BDTRInitTypeDefTIM_BDTRInitStructure;//TIM1基本计数器设置(设置PWM频率)//频率=TIM1_CLK/(ARR+1)TIM_BaseInitStructure.TIM_Period=1000-1;TIM_BaseInitStructure.TIM_Prescaler=72-1;TIM_BaseInitStructure.TIM_ClockDivision=0;TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_BaseInitStructure.TIM_RepetitionCounter=0;TIM_TimeBaseInit(TIM1,&TIM_BaseInitStructure);//启用ARR的影子寄存器(直到产生更新事件才更改设置)TIM_ARRPreloadConfig(TIM1,ENABLE);//TIM1_OC1模块设置(设置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_OCPolarity=TIM_OCPolarity_High;TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;TIM_OCInitStructure.TIM_Pulse=120;//TIM1_Pulse设置了待装入捕获比较寄存器的脉冲值TIM_OC1Init(TIM1,&TIM_OCInitStructure);//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);//TIM2_OC2模块设置(设置2通道占空比)TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse=680;TIM_OC2Init(TIM1,&TIM_OCInitStructure);//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC2PreloadConfig(TIM1,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=0x90;//这里调整死区大小0-0xffTIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable;TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High;TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);//TIM1开启TIM_Cmd(TIM1,ENABLE);//TIM1_OC通道输出PWM(一定要加)TIM_CtrlPWMOutputs(TIM1,ENABLE);}另一程序---------------------------------#include#include"PWM.h"//头文件#include"IOSET.h"//包含io口设置头文件u16pw1=0;u16pw2=5600;u16pw3=2800;voidPWM_Init(u16arr,u16psc)//PWM初始化程序{RCC->APB2ENR|=1<<11;//使能定时器1时钟RCC->APB2ENR|=1<<2;//使能IO口A时钟RCC->APB2ENR|=1<<3;//使能IO口B时钟RCC->APB2ENR|=1<<0;//开启辅助时钟GPIOB->CRH&=0X000FFFFF;//清除PB13PB14,15状态寄存器位GPIOB->CRH|=0XBBB00000;//设定PB13PB14,15//TIM1CH1N,TIM1CHH2NCH3N输出GPIOA->CRH&=0XFFFFF000;//清除PA8,9,10状态寄存器位GPIOA->CRH|=0X00000BBB;//设定PA8,9,10为复用功能TIM1CH1,2,3输出GPIOB->ODR|=1<<13;GPIOB->ODR|=1<<14;GPIOB->ODR|=1<<15;//输出上拉,低电平有效GPIOA->ODR|=1<<8;GPIOA->ODR|=1<<9;GPIOA->ODR|=1<<10;TIM1->CCR1=pw1;//捕获比较预装载值TIM1->CCR2=pw2;TIM1->CCR3=pw3;TIM1->ARR=arr;//设定计数器自动重装值TIM1->PSC=psc;//预分频器不分频TIM1->BDTR|=0xCD;//死区时间设定TIM1->CCER|=1<<0;//TIM1CH1通道开关TIM1->CCER|=1<<2;//TIM1CH1N通道开关TIM1->CCER|=1<<4;//TIM1CH2通道开关TIM1->CCER|=1<<6;//TIM4CH2N通道开关TIM1->CCER|=1<<8;//TIM4CH3通道开关TIM1->CCER|=1<<10;//TIM4CH3N通道开关TIM1->CCMR1|=7<<4;//TIM1CH1PWM2模式TIM1->CCMR1|=7<<12;//TIM1CH2PWM2模式TIM1->CCMR2|=7<<4;//TIM1CH3PWM2模式TIM1->CCMR1|=1<<3;//TIM1CH1预装载使能TIM1->CCMR1|=1<<11;//TIM1CH2预装载使能TIM1->CCMR2|=1<<3;//TIM1CH3预装载使能TIM1->CR1|=0X80;//CR1ARPE自动重装载预装载允许位使能TIM1->BDTR|=1<<15;//刹车使能位置1,开启所有输出通道TIM1->CR1|=0X01;//使能定时计数器1//TIM1->EGR|=1<<0;}