//2014年4月17日
//2014年6月20日入“未完毕”
//2014年6月21日
有两种方案:双定时器和单定时器
学长表示双定时器输出波形不好,还是单定时器好。
原理例如以下:
SPWM採用正弦波与三角波比較方式,详细百度。
单片机详细工作:
TIM1
三角波:DIR选中央对齐模式
正弦波:数组实现採样的正弦波
ARR:设为carrval,决定了输出波形的频率
CCR1:我们通过改变它来实现占空比的改变
改变CCR1:每次计数器完毕ARR,更新时产生中断(我们用UP_IQR),此时把正弦波数组赋给CCR1
注意:正弦波数组要经过放大,使其与ARR匹配
void SPWM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_BaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; GPIO_InitTypeDef GPIO_InitStructure; /*******************************************************************************/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1,ENABLE); /*********************** GPIO ********************************************************/ //PA.8口设置为TIM1的OC1输出口 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //PB.13口设置为TIM1_CH1N和TIM1_CH2N输出口 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /************************PWM频率=72000/(360*200)=1Khz*******************************************************/ //TIM1基本计数器设置(设置PWM频率) //频率=TIM1_CLK/(ARR+1) TIM_BaseInitStructure.TIM_Period = 199; TIM_BaseInitStructure.TIM_Prescaler = 359; TIM_BaseInitStructure.TIM_ClockDivision = 0; TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; TIM_BaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure); TIM_ARRPreloadConfig(TIM1, ENABLE); //TIM1->CR1=0x00b1; TIM1->CR2=0x0200; /*************************设置占空比******************************************************/ 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 = 150; //TIM1_Pulse设置了待装入捕获比較寄存器的脉冲值 /*************************** 多路SPWM ******************************/ TIM_OC1Init(TIM1, &TIM_OCInitStructure); //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置) TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM1->CCR1=0x0100; TIM1->ARR=CarrVal; /***************************死区设置****************************************************/ TIM1->CR1&=0x00ff; //配置时钟分频因子 Tdts = Tck_int = 72M 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-0xff TIM_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); /*******************************************************************************/ TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //TIM1->DIER=0x0001; //TIM1开启 TIM_Cmd(TIM1, ENABLE); //TIM1_OC通道输出PWM(一定要加) TIM_CtrlPWMOutputs(TIM1, ENABLE); }
原文:http://www.cnblogs.com/blfshiye/p/4004926.html