stm32 PWM产生

发布者:Huayu8888最新更新时间:2024-10-08 来源: cnblogs关键字:stm32  PWM 手机看文章 扫描二维码
随时随地手机看文章
  1. /* TIM Configuration */

  2.   TIM_Config();


  3.   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);


  4.   TIM_OCStructInit(&TIM_OCInitStructure);


  5.   /* ---------------------------------------------------------------------------

  6.      TIM8 is configured to generate an Asymetric signal with a programmable

  7.      Phase-Shifted signal on TIM8_CH2:

  8.      - TIM8 Channel 1 is configured in PWM2 mode

  9.      - TIM8 Channel 2 is configured in Asymetric PWM2 mode

  10.      - The counter mode is center aligned mode

  11.      - The pulse length and the phase shift are programmed consecutively in TIM8_CCR2 and TIM8_CCR1.


  12.      TIM1 is configured to generating the reference signal on Channel1 used by TIM8:

  13.      - TIM1 is generating a PWM signal with frequency equal to 1.5KHz

  14.      - TIM1 is used as master for TIM8, the update event of TIM1 genarates the Reset counter

  15.      of TIM8 to synchronize the two signals: the reference signal (TIM1_CH1) and

  16.             the shifted signal (TIM8_CH2).

  17.     

  18.     In this example TIM1 and TIM8 input clock (TIM18CLK) is set to APB2 clock (PCLK2)

  19.     TIM1 and TIM8 signals are at frequency of (SystemCoreClock / (PWM_FREQUENCY + 1))

  20.                

  21.     TIM8 is gerating a signal with the following caracteristics:

  22.      - Pulse lenght = (TIM8_CCR1 + TIM8_CCR2) / TIM8_CLK

  23.      - Phase shift = TIM8_CCR1/TIM8_CLK

  24.      with TIM8_CLK = (SystemCoreClock / (Period + 1)), as the prescaler is equal to zero.

  25.     

  26.     Note:

  27.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f30x.c file.

  28.      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()

  29.      function to update SystemCoreClock variable value. Otherwise, any configuration

  30.      based on this variable will be incorrect.

  31.   --------------------------------------------------------------------------- */

  32.   /* Initialize Timers: TIM1 & TIM8 */

  33.   /* Time base configuration for TIM8 and TIM1 */

  34.   TIM_TimeBaseStructure.TIM_Period = PWM_FREQUENCY;     //

  35.   TIM_TimeBaseStructure.TIM_Prescaler = 0;        // this prescaler is 1

  36.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;


  37.   /* First wrong parameter: Counting Mode,TIM8 counting direction has to be set to center-aligned mode */

  38.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;

  39.   

  40.   TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

  41.   

  42.     //??????????TIM8?????ì???????????ú????·??????è??????????????·?????????·???????????????????????

  43.   TIM_TimeBaseStructure.TIM_Period = 2 * PWM_FREQUENCY;        // 1??PWM?¨???????è?¨

  44.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  45.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);


  46.   /* Channels 1&2 configuration on TIM8 */

  47.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  48.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  49.   TIM_OCInitStructure.TIM_Pulse = INITIAL_PHASE;

  50.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  51.   TIM_OC1Init(TIM8, &TIM_OCInitStructure); /* Channel1 config done */

  52.   

  53.   /* Second wrong parameter: PWM Mode */

  54.   /* The same PWM mode has to be configured for the two coupled channels */

  55.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Asymmetric_PWM2;


  56.   TIM_OCInitStructure.TIM_Pulse = INITIAL_LENGTH;

  57.   TIM_OC2Init(TIM8, &TIM_OCInitStructure); /* Channel2 config done */

  58.   

  59.   /* Channel1 configuration on TIM1 */

  60.   TIM_OCInitStructure.TIM_Pulse = 2 * PWM_FREQUENCY / 10 * 0.9;        // 2??PWM ????±?

  61.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  62.   TIM_OC1Init(TIM1, &TIM_OCInitStructure);


  63.   /* Enable outputs on both TIM1 & TIM8*/

  64.   TIM_CtrlPWMOutputs(TIM8, ENABLE);

  65.   TIM_CtrlPWMOutputs(TIM1, ENABLE);


  66.   /* Synchronization between TIM1 and TIM8

  67.   The aim is to generate a reference signal on TIM1_CH1

  68.   The Phase-Shifted siganl generated on TIM8_CH2 is compared to the reference

  69.   signal

  70.   */

  71.   TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Reset); /* Configure TIM8 in slave

  72.   mode: an active edge on trigger input generates a reset on tIM8 */

  73.   

  74.   TIM_SelectInputTrigger(TIM8, TIM_TS_ITR0); /* Connect TIM1 to TIM8

  75.   TIM1 is the Master

  76.   TIM8 is the Slave */

  77.   

  78.   TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update); /* Select the Update

  79.   to be the Master Trigger-Out TRGO signal origine */

  80.   

  81.   TIM_CCPreloadControl(TIM8, ENABLE); /* Enable Shadaw register on TIM8

  82.   CCRx register are not accessed directly: their content is updated each Update

  83.   event */

  84.   

  85.   /* TIM8 enable counter */

  86.   TIM_Cmd(TIM1, ENABLE);

  87.   TIM_Cmd(TIM8, ENABLE) 


关键字:stm32  PWM 引用地址:stm32 PWM产生

上一篇:STM32 DAC 播放wav 语音
下一篇:第16章 STM32中断应用概览

小广播
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved