您现在的位置: 主页 > 嵌入式处理器 > ARM > STM32 > stm32应用 > STM32F103增强型定时器(TIM1,TIM8)输出PWM
本文所属标签:
为本文创立个标签吧:

STM32F103增强型定时器(TIM1,TIM8)输出PWM

来源:网络整理 网络用户发布,如有版权联系网管删除 2017-03-24 

有关STM32F103增强型定时器(TIM1,TIM8)输出PWM(互补加死区)
 
作者:猎人fpga    文章来源:猎人fpga    点击数:6874    更新时间:2013-9-4    
 

需求,输出两组互补PWM,频率为100KHz,占空比可调,死区时间600nS。

对于STM32的定时器配置,主要涉及3部分,时基设置,输出设置以及死区与刹车功能,其配置方法如下:

  1. void TIMER_Config(void)  
  2. {  
  3. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
  4. TIM_OCInitTypeDef TIM_OCInitStructure;  
  5. TIM_BDTRInitTypeDef TIM_BDTRInitStructure;  
  6.   
  7. TIM_TimeBaseStructure.TIM_Prescaler = 0;  
  8. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  9. TIM_TimeBaseStructure.TIM_Period = 720 - 1;   
  10. TIM_TimeBaseStructure.TIM_ClockDivision = 0;   
  11. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;  
  12.   
  13. TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);  
  14. TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);  
  15.   
  16.   
  17. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Set as the BiPolarity Output  
  18. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  
  19. TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;  
  20. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  21. TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;  
  22. TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;  
  23. TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;  
  24. TIM_OCInitStructure.TIM_Pulse = 100;  
  25. TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  
  26. TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);  
  27. TIM_OC1Init(TIM1, &TIM_OCInitStructure);//Set the Channel 1 of TIMER1  
  28. TIM_OC1Init(TIM8, &TIM_OCInitStructure);//Set the Channel 1 of TIMER8  
  29.   
  30. //Set the Dead time  
  31. /*
  32. To set the deadtime(TD),is to determ the regesiter DTG[7:0]
  33. DTG[7:0]:(Dead-time generator setup) 
  34. DTG[7:5]=0xx => DT=DTG[7:0]*Tdtg, Tdtg = Tdts; 
  35. DTG[7:5]=10x => DT=(64+DTG[5:0])*Tdtg, Tdtg = 2*Tdts; 
  36. DTG[7:5]=110 => DT=(32+DTG[4:0])*Tdtg, Tdtg = 8*Tdts; 
  37. DTG[7:5]=111 => DT=(32+DTG[4:0])*Tdtg, Tdtg = 16*Tdts; 
  38.  
  39. Tdts is determined by CKD[1:0], which is from Timer control regesity TIMx_CR1[9:0] 
  40. representing the meaning of the Timer clock Devider
  41.  
  42. CKD[1:0] = 00    Tdts = TCK_INT
  43. CKD[1:0] = 01    Tdts = 2 * TCK_INT
  44. CKD[1:0] = 10    Tdts = 4 * TCK_INT
  45. CKD[1:0] = 11    Reserved
  46. Where TCK_INT is the Timer Clock.
  47.  
  48. Here, the Tdts = TCK_INT = 1/72 uS.
  49. So, the DT maybe as long as follows:
  50. 0nS  ~   1763nS  The step is 1/72 uS
  51. 1777nS   ~   3527nS  The step is 2*1/72 uS
  52. 3555nS   ~   7000nS  The step is 8*1/72 uS
  53. 7111nS   ~   14000nS     The step is 16*1/72 uS
  54.  
  55. Here, We make the DT equals to 600 nS, which is in the limitation of No.1.
  56. So, DTG[7:0] = 0.6/(1/72) = 43 = 0x2B.
  57. */  
  58.   
  59.   
  60. TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; // Disable the Break function  
  61. TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;  
  62. TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //Enable Running State  
  63. TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //Enable Idle State  
  64. TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //Set the lock level  
  65. TIM_BDTRInitStructure.TIM_DeadTime = 0x2B;  
  66. TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //Enable the Auto Outputting.  
  67. TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);  
  68. TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);  
  69.   
  70. TIM_Cmd(TIM1, ENABLE);  
  71. TIM_Cmd(TIM8, ENABLE);  
  72. TIM_CtrlPWMOutputs(TIM1, ENABLE);  
  73. TIM_CtrlPWMOutputs(TIM8, ENABLE);  
  74. }   


 

希望两组PWM波具有独立性,互补干涉,无牵制,考虑到功能余量较大,就将TIM1和TIM8都用上,分别用来才生一组互补pwm,所带来问题就是两组波形之间产生了一个相对稳定的相位差,解决方法是引入第三个定时器,柯采用普通定时器,作为TIM1和TIM8的同步时钟,消除这个相位差的存在。

 


              查看评论 回复



嵌入式交流网主页 > 嵌入式处理器 > ARM > STM32 > stm32应用 > STM32F103增强型定时器(TIM1,TIM8)输出PWM
 

"STM32F103增强型定时器(TIM1,TIM8)输出PWM"的相关文章

网站地图

围观()