[英]How to capture input signal and generate pwm signal by one timer
我在此代碼中捕獲並生成信號時遇到問題
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用於生成PWM TIM3_CH1用於捕獲,TIM3_Ch2用於生成PWM
#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);
}
我認為,它沒有用,因為我有一個定時器=>一個CNT,這個CNT無法捕獲輸入信號並同時產生PWM
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.