STM32F407 PWM控制相移,占空比

时间:2014-12-13 21:54:22

标签: arm stm32 stm32f4discovery coocox

我是stm32f407发现板的新手,我试图制作4个信号,我可以控制相移和占空比。问题是,当我将定时器设置为PWM模式时,我无法控制channel_1和channel_2之间的相移。我知道我必须使用中断,但我无法弄清楚我应该在其中编码什么。如果你能帮助我,我将不胜感激。

这是我的代码,我使用一个中断,每隔一段时间打开/关闭一个LED(延迟没有很好地同步)。

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "misc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "delay.h"

/* Private typedef -----------------------------------------------------------*/
GPIO_InitTypeDef  GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* Private define ------------------------------------------------------------*/
#define frequency 42500

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
int Prescaler = 0;
int Period = 84000000 / frequency; // ~42.5KHz
int pulse_width;

/* Private function prototypes -----------------------------------------------*/
void GPIO_Config(void);
void PWM_Config(void);
void PWM_SetDC(int channel,int dutycycle);
void Delay(__IO int nCount);
void LED_Config(void);

/* Private functions ---------------------------------------------------------*/
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{

    Delay(10000000);

    GPIO_ToggleBits(GPIOD, GPIO_Pin_15);

    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
}
/**
  * Main program
  */
int main(void)
{


  /* GPIO Configuration */
  GPIO_Config();

  LED_Config();

  /* PWM Configuration */
  PWM_Config();

  PWM_SetDC(1,80);
  PWM_SetDC(2,40);
  PWM_SetDC(3,0);
  PWM_SetDC(4,0);

  while (1)
  {

  }
}

/**
  * Configure the TIM2 Output Channels.
  */
void GPIO_Config(void)
{

  NVIC_InitTypeDef NVIC_InitStructure;
  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOA clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;
  /* GPIOC Configuration: TIM2 CH1 (PA0), TIM2 CH2 (PA1), TIM2 CH3 (PA2), TIM2 CH4 (PA3) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Connect TIM2 pins to AF2 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2);

}

void PWM_Config(void)
{

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = Period - 1;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_ARRPreloadConfig(TIM2, ENABLE);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

  ////////////////////////* TIM INTERRUPT enable *////////////////////////////
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
  ////////////////////////////////////////////////////////////////////////////

  /* PWM1 Mode configuration: Channel2 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel3 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC3Init(TIM2, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC4Init(TIM2, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);

  /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);
}

void PWM_SetDC(int channel,int dutycycle)
{
    if (dutycycle <= 100 && dutycycle >= 0)
    {
        pulse_width=(Period*dutycycle)/100;
          if (channel == 1)
          {
            TIM2->CCR1 = pulse_width;

          }
          else if (channel == 2)
          {

            TIM2->CCR2 = pulse_width;
          }
          else if (channel == 3)
          {
            TIM2->CCR3 = pulse_width;
          }
          else
          {
            TIM2->CCR4 = pulse_width;
          }
    }
}

void LED_Config(void)
{
  /* GPIOD Peripheral clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure PD12, PD13, PD14 and PD15 in output push-pull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

}

/* Delay Function.
 * nCount:specifies the Delay time length.
 */
void Delay(__IO int nCount)
{
  while(nCount--)
  {
  }
}

1 个答案:

答案 0 :(得分:0)

我想我可以帮助你,如果你让你问题有点清楚.. 至于我能理解你的问题: - &gt;我想你想要2个具有相移功能的PWM吗? 所以,如果你想在两个pwm之间进行简单的相移,那就简单地说吧,如果你想要1ms的延迟,请说。 - &GT;你必须使用两个不同的计时器..

TIM_Cmd(TIM2,ENABLE);

延迟(1000);

TIM_Cmd(TIM3,ENABLE);