打印

STM32如何控制PWM脉冲输出数量

[复制链接]
13212|15
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
liangzisen|  楼主 | 2009-12-3 09:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32如何控制PWM脉冲输出数量,,我用PWM脉冲来做我外设的同步时钟所以需要确切的数值!
沙发
barboon| | 2009-12-3 09:34 | 只看该作者
开中断,每输出一个脉冲进一次中断,计数器+1,到点了关掉。

使用特权

评论回复
板凳
liangzisen|  楼主 | 2009-12-3 21:01 | 只看该作者
我的PWM的更新事件是用来做SRAM->GPIO的触发源的,这样GPIO送数就可以达到5M的速度,但需要严格控制PWM更新事件的数量,如果想2楼说的操作,是可以实现计数,估计DMA送数就没法达到这样高的数量级了,可能就1M~!  有更好的办法吗,比如TIME1的PWM模式有哪里可以设置输出脉冲数量的!

使用特权

评论回复
地板
lxyppc| | 2009-12-3 21:16 | 只看该作者
本帖最后由 lxyppc 于 2009-12-3 21:36 编辑

用到了Timer Master Slave中的Gate模式
比如TIM1输出PWM, 频率为F
可以用TIM2通过Gate来控制TIM1的输出
将TIM2预分频设为1/(F*2),则TIM2的Period 就是 脉冲个数*2 - 1

/*           1     2     3     4     5     6     7     8     9 
             __    __    __    __    __    __    __    __    __            
            |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |            
TIM1:    ___|  |__|  |__|  |__|  |__|  |__|  |__|  |__|  |__|  |_
          ->| Period1|<-
             1) 2) 3) 4) 5) 6) 7) 8) 9) 10)11)12)13)14)15)16)17)
             __________________________________________________   
            |                                                  |   
TIM2:    ___|                                                  |_
          ->|  |<--- Pres2 = Period1/2
            |<------------  Period2 =  N*2-1 = 17 ------------>|
*/

使用特权

评论回复
5
lxyppc| | 2009-12-3 21:26 | 只看该作者
软件:
IAR 4.42限制版
ST库    2.01
硬件:
万利199开发板 STM3210B-LK1
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;

/* Private function prototypes -----------------------------------------------*/
void  RCC_Configuration(void);
void  NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/

#define   PWM_Period      120
int main(void)
{
  u16       waveNumber = 10;
  /* System Clocks Configuration */
  RCC_Configuration();

  /* Enable related peripheral clocks */
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOA,ENABLE);
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOB,ENABLE);
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_TIM1,ENABLE);
  RCC_APB1PeriphClockCmd(  RCC_APB1Periph_TIM3,ENABLE);
   
  /* Config IO for related timers */
  {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Timer1 Channel 2, PA9 */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Timer3 Channel 4, PB1*/
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  }
  /* Setup Timer3 channel 4, Timer3 is master timer
     This timer is used to control the waveform count of timer1 */

  {
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_DeInit(TIM3);
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_OCStructInit(&TIM_OCInitStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = PWM_Period/2 - 1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = waveNumber*2;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
     
    /* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = waveNumber*2 - 1;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC4Init(TIM3,&TIM_OCInitStructure);
    TIM_CtrlPWMOutputs(TIM3, ENABLE);
    TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
  }
  /* Setup timer1 channel 2, Timer1 is slave timer  
     This timer is used to output waveforms */

  {
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_DeInit(TIM1);
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_OCStructInit(&TIM_OCInitStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = PWM_Period;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
     
    /* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PWM_Period/2;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM1,&TIM_OCInitStructure);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
  }
  /* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
    timer1 is work under gate control mode, and controled by timer3
    timer3's channel 4 is used as the control signal
   */

    /* Enable timer's master/slave work mode */
    TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
    TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
    /* timer3's channel 4 is used as the control signal */
    TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );
    /* Check the master/slave is valid or not */
    compile_assert((u16)GetInternalTrigger(TIM1,TIM3) != (u16)-1);  
    /* Config timer1's external clock */
    TIM_ITRxExternalClockConfig(TIM1, GetInternalTrigger(TIM1,TIM3));
    TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);
     
  /* Enable the slave tiemr*/
  TIM_Cmd(TIM1,ENABLE);
  //SetupAlltimers();
  while(1){
    /* Check whether the previous action is done or not */
    if(!(TIM3->CR1 & 1)){
      TIM1->CNT = 0; /* It would be very perfect if gate mode can  
                        reset the slave timer automatically */

      TIM3->ARR = waveNumber*2;  /* Reload wave number*/
      TIM3->CCR4 = waveNumber*2 - 1;
      TIM3->CR1|=1; /* Re-enable the timer */
      /* update waveform number */
      waveNumber++;
      if(waveNumber == 13){
        waveNumber = 10;
      }
    }
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/

void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
  
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);  
   
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);  

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */  
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
}

输出波形如下图

使用特权

评论回复
6
lxyppc| | 2009-12-3 21:34 | 只看该作者
本帖最后由 lxyppc 于 2009-12-3 21:52 编辑



P.S. 论坛怎么不能上传图了

使用特权

评论回复
7
zwll| | 2009-12-3 22:24 | 只看该作者
5楼的代码好长啊

使用特权

评论回复
8
sinadz| | 2009-12-4 08:40 | 只看该作者
还是没看到问题的解决方法

使用特权

评论回复
9
lxyppc| | 2009-12-4 11:36 | 只看该作者
5楼的代码好长啊
zwll 发表于 2009-12-3 22:24

这只能怪ST的库了
用寄存器直接操作意思会很不明确

使用特权

评论回复
10
liangzisen|  楼主 | 2009-12-4 15:05 | 只看该作者
本帖最后由 liangzisen 于 2009-12-4 15:08 编辑

谢谢5楼给的信息,我测试了下,确实能出现你说的那波形,10、11、12脉冲间隔输出,但理论上如果我的主TIME3只让他运行一次,即产生一次脉冲,当主TIME3结束后从TIME1 的波形应该当也更随结束,但我测试结果是TIME1小停后产生连续的PWM波形,就是说TIME1 没停止工作!

使用特权

评论回复
11
liangzisen|  楼主 | 2009-12-4 15:41 | 只看该作者
本帖最后由 liangzisen 于 2009-12-4 15:44 编辑

TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );   比较成功后,产生一次TRGO脉冲(即时是高电平),那就是说在正常情况下TRGO 都是高电平,只有在强制产生脉冲的时候,才有一瞬间的低电平,只有这个时候从TIM3才会停止PWM输出,又因为  TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated); 从TIME1的TRGI模式是 TIM_SlaveMode_Gated(当触发信号TRGI为高电平计数器时钟时能),所以即时主TIM1做一个周期后停止工作,TRGO能然是高电平,TIME3继续PWM输出!
这个我的理解,能否找出个解决的办法,可以使主TIM1停止后TIM3也立即停止,使用中断除外!

使用特权

评论回复
12
lxyppc| | 2009-12-4 16:20 | 只看该作者
不会啊
TIM3作为主时钟,配置为OnePulse模式,OC4为PWM模式1
TRGO设置为OC4的Ref信号,OC4Ref在CNT<CCR4时输出高电平(TIM1工作),这之后输出低电平(TIM1停止)

由于TIM3是OnePulse,CNT=ARR之后TIM3就停了,不会再输出了,会保持低电平状态

使用特权

评论回复
13
liangzisen|  楼主 | 2009-12-4 18:47 | 只看该作者
不可能,我今天用示波器跟踪了,TIM1在TIM3停止工作后,还是输出PWM脉冲,后来我用了TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Enable); 来触发,当主TIM3计数器使能,产生TRGO信号触发从TIM3开始工作,当TIM3计数完毕后,TIM3失效,相应的TRGO应该也是低电压,才是得从TIME1停止工作

使用特权

评论回复
14
lxyppc| | 2009-12-4 20:43 | 只看该作者
你的这个配置能达到你的要求,只要把Prescaler和Period设置得合适就行了
我的那个没有仔细测试过,楼主最好用你的试过可行的方法来做
回头我再看看我的为什么不行

使用特权

评论回复
15
haier00410| | 2010-5-5 14:44 | 只看该作者
强  都是牛人呀

使用特权

评论回复
16
dcp| | 2011-9-13 03:06 | 只看该作者
不可能,我今天用示波器跟踪了,TIM1在TIM3停止工作后,还是输出PWM脉冲,后来我用了TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Enable); 来触发,当主TIM3计数器使能,产生TRGO信号触发从TIM3开始工作,当TIM3计 ...
liangzisen 发表于 2009-12-4 18:47



可否发代码上来参考一下呢?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

66

帖子

0

粉丝