1、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.
2、 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, /PB.13/14口设置为 TIM1_CH1N 和 TIM1_CH2N 输出口GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13| GPIO_Pin_14;GPIO_
3、InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB, /Step3. TIM 模块初始化voidTIM_Configuration(void)TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;TIM_OCInitTypeDef TIM_OCInitStructure;TIM_BDTRInitTypeDef TIM_BDTRInitStructure;/TIM1基本计数器设置(设置 PWM 频率)/频率
4、 =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, /启用 ARR 的影子寄存器(直到产生更新事件
5、才更改设置)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_O
6、CInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;TIM_OCInitStructure.TIM_Pulse = 120; /TIM1_Pulse 设置了待装入捕获比较寄存器的脉冲值TIM_OC1Init(TIM1, /启用 CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);/TIM2_OC2模块设置(设置 2通道占空比)TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Ena
7、ble;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = 680;TIM_OC2Init(TIM1, /启用 CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);/死区设置TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;TIM_BDTRInitStructure.TIM_OSSIStat
8、e = 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 =
9、 TIM_AutomaticOutput_Enable;TIM_BDTRConfig(TIM1, /TIM1开启TIM_Cmd(TIM1, ENABLE);/TIM1_OC 通道输出 PWM(一定要加)TIM_CtrlPWMOutputs(TIM1, ENABLE);另一程序-#include#include“PWM.h“ / 头文件#include“IOSET.h“ / 包含 io 口设置头文件u16 pw1=0;u16 pw2=5600;u16 pw3=2800;void PWM_Init( u16 arr,u16 psc) /PWM 初始化程序RCC-APB2ENR|=1APB2ENR|
10、=1APB2ENR|=1APB2ENR|=1CRH / 清除 PB13PB14,15 状态寄存器位 GPIOB-CRH|=0XBBB00000; / 设定 PB13PB14,15/TIM1CH1N,TIM1CHH2N CH3N 输出GPIOA-CRH / 清除 PA8,9,10 状态寄存器位 GPIOA-CRH|=0X00000BBB; / 设定 PA8,9,10 为复用功能 TIM1CH1,2,3 输出GPIOB-ODR|=1ODR|=1ODR|=1ODR|=1ODR|=1ODR|=1CCR1=pw1; /捕获比较预装载值TIM1-CCR2=pw2;TIM1-CCR3=pw3;TIM1-ARR=arr; / 设定计数器自动重装值 TIM1-PSC=psc; / 预分频器不分频TIM1-BDTR|=0xCD; / 死区时间设定TIM1-CCER|=1CCER|=1CCER|=1CCER|=1CCER|=1CCER|=1CCMR1|=7CCMR1|=7CCMR2|=7CCMR1|=1CCMR1|=1CCMR2|=1CR1|=0X80; / CR1 ARPE 自动重装载预装载允许位使能TIM1-BDTR|=1CR1|=0X01; / 使能定时计数器 1/TIM1-EGR|=10;