我在用此代码捕获并生成信号时遇到问题
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信号,我使用了3个定时器,每个定时器都应捕获并生成信号
我使用STM32F103C8T6 定时器是TIM2_CH1,用于捕获TIM2_CH3,用于生成PWM TIM4_CH1用于捕获,TIM4_CH4用于生成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);
}
答案 0 :(得分:0)
我认为,这是行不通的,因为我有一个计时器=>一个CNT,并且该CNT无法捕获输入信号并同时生成PWM