1

我是 stm32f407 发现板的新手,我正在尝试制作 4 个可以控制相移和占空比的信号。问题是,当我将定时器设置为 PWM 模式时,我无法控制通道_1 和通道_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--)
  {
  }
}
4

1 回答 1

0

如果你让你的问题有点清楚,我想我可以帮助你。至于我能理解你的问题:->我认为你想要 2 个带相移的 PWM?因此,如果您想要两个 pwm 之间的简单相移,只需使用这个假设您想要 1ms 的延迟。-> 你必须使用两个不同的计时器然后..

TIM_Cmd(TIM2,启用);

延迟(1000);

TIM_Cmd(TIM3,启用);

于 2014-12-18T14:53:25.457 回答