如何捕获输入信号并通过一个定时器生成pwm信号

问题描述 投票:0回答:1

我在此代码中捕获并生成信号时遇到问题

    void TIM4_Config(void)
{ 
    TIM_OCInitTypeDef HardPWM_ini;
    TIM_TimeBaseInitTypeDef     TIM_Time_user;
    GPIO_InitTypeDef            port;
    TIM_ICInitTypeDef           TIM_ICInit_user;
    NVIC_InitTypeDef NVIC_InitStructure2;
// TIM4 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// GPIOB clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// TIM4 channel_1 configuration : PB6
port.GPIO_Pin   = GPIO_Pin_6;
port.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
port.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(GPIOB, &port);
// Connect TIM pin to AF2

NVIC_InitStructure2.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);

// Time base configuration
TIM_Time_user.TIM_Period = PERIOD;
TIM_Time_user.TIM_Prescaler = PRESCALER;
TIM_Time_user.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_Time_user.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_Time_user);

//Capture configuration
TIM_ICInit_user.TIM_Channel = TIM_Channel_1;
TIM_ICInit_user.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit_user.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit_user.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit_user.TIM_ICFilter = 0x0;

//Inicialize our structure for capture signal
TIM_PWMIConfig(TIM4, &TIM_ICInit_user);

    HardPWM_ini.TIM_OCMode = TIM_OCMode_PWM1;
    HardPWM_ini.TIM_OCPolarity = TIM_OCPolarity_High;
    HardPWM_ini.TIM_OutputState = TIM_OutputState_Enable;
    HardPWM_ini.TIM_Pulse = 150; //set servo's position in 90 deg.          

    /*initialize structure for PWM which use TIM4*/
    TIM_OC4Init(TIM4, &HardPWM_ini);
//input trigger
TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1);

  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); // reset
    // Select the slave Mode: Reset Mode
   TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
// Enable the TIM4 Counter
TIM4->CR1 |= TIM_CR1_CEN;

// Enable the CC1 Interrupt request
TIM4->DIER |= TIM_IT_CC1;
}

void TIM4_IRQHandler(void)
{

if (TIM4->SR & TIM_SR_CC1IF )
    {
        DataCapture_TIM4 = TIM4->CCR2;
            TIM4->SR &= (uint16_t)~TIM_SR_CC1IF;

    }

}

 This part of code capturing input signal by TIM4 CHANNEL 1 well enogh, but do not generate PWM on CHANNEL 4.

但是,如果我单独初始化生成PWM的代码,它将工作。然后我的程序可以在PB10上生成PWM信号,我使用它们三次应该捕获并生成信号

我使用STM32F103C8T6定时器TIM2_CH1用于捕获TIM2_CH3,用于产生用于捕获的PWM TIM4_CH1,TIM4_CH4用于产生用于捕获的TIM TIM3_CH1和用于产生PWM的TIM3_Ch2

#include "PWM.h"
    /*Configuration and Inicialization of TIM4 Channel4 and hardware PWM*/
    void Hard_PWM(void)
    {
        /*create variables for structures*/
        TIM_TimeBaseInitTypeDef TIM_Time_user;
        TIM_OCInitTypeDef HardPWM_ini;
    GPIO_InitTypeDef port;

    /*Enable rcc for GPIOB APB2 bus*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    /*Enable and configuration of GPIOD PB9*/
  GPIO_StructInit(&port);
  port.GPIO_Mode = GPIO_Mode_AF_PP;
  port.GPIO_Pin = GPIO_Pin_9;
  port.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOB, &port);

    /*Enable rcc for TIM4*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    /*Timer configuration(frequency of TIM4 = 50Hz, because 16MHz/((320-1)*(1000-1))*/
    TIM_Time_user.TIM_Prescaler = 320-1;
    TIM_Time_user.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_Time_user.TIM_Period = 1000;////////////////////////////////////////////////////////////////
    TIM_Time_user.TIM_ClockDivision = TIM_CKD_DIV1;

    /*initialize structure for TIM4*/
    TIM_TimeBaseInit(TIM4, &TIM_Time_user);

    /*Configuration PWM*/
    HardPWM_ini.TIM_OCMode = TIM_OCMode_PWM1;
    HardPWM_ini.TIM_OCPolarity = TIM_OCPolarity_High;
    HardPWM_ini.TIM_OutputState = TIM_OutputState_Enable;
    HardPWM_ini.TIM_Pulse = 150; //set servo's position in 90 deg.          

    /*initialize structure for PWM which use TIM4*/
    TIM_OC4Init(TIM4, &HardPWM_ini);

    /*Enable TIM2*/ 
    TIM_Cmd(TIM4, ENABLE);
}
c timer stm32 capture pwm
1个回答
0
投票

我认为,它没有用,因为我有一个定时器=>一个CNT,这个CNT无法捕获输入信号并同时产生PWM

© www.soinside.com 2019 - 2024. All rights reserved.