Try to search your question here, if you can't find : Ask Any Question Now ?

How to capture input signal and generate pwm signal by one timer

HomeCategory: stackoverflowHow to capture input signal and generate pwm signal by one timer
Avatarjohn asked 5 months ago

I have a problem with capturing and generate signal in this code

    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.

BUT If i inicialize code for Generate PWM separately it will work. Like that then my program can generate PWM signal on PB10
#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);
}
1 Answers
Best Answer
AvatarMannu answered 5 months ago
Your Answer

15 + 6 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com