Chinaunix首页 | 论坛 | 博客
  • 博客访问: 319455
  • 博文数量: 80
  • 博客积分: 773
  • 博客等级: 军士长
  • 技术积分: 799
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-24 23:52
个人简介

victory for prepared

文章分类

全部博文(80)

文章存档

2024年(2)

2023年(1)

2022年(2)

2021年(1)

2018年(8)

2017年(3)

2016年(20)

2015年(12)

2013年(5)

2012年(25)

2011年(1)

我的朋友

分类: 其他平台

2018-07-15 15:01:16

 

点击(此处)折叠或打开

  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
阅读(1300) | 评论(0) | 转发(0) |
0

上一篇:运动物体的检测

下一篇:stm32f303 串口设置

给主人留下些什么吧!~~