本帖最后由 tobyking 于 2015-5-21 17:00 编辑
各位大侠,下面是小弟的程序,小弟初学者,刚用定时器没几天,刚学会产生PWM波形, 看看我的为啥检测不到脉冲的输入呢?
我用TIM3产生PWM波形,直接输入到TIM4的CH2,想让pulse_numb_min = TIM_GetCapture2(TIM4)收集到一些脉冲数量,请问我要怎么改?
unsigned int pulse_numb_min=0,pulse_count;
/* Private variables ---------------------------------------------------------*/
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Private function prototypes -----------------------------------------------*/
void RCC_Config(void);
void GPIO_Config(void);
void NVIC_Config(void);
//void LED_Configuration(void);
/************************************************************/
//extern u8 CAPTURE_STA;
int main(void)
{
/* System Clocks Configuration */
RCC_Config();
/* NVIC configuration */
NVIC_Config();
/* Configure the GPIO ports */
GPIO_Config();
TIM3_GPIO_Config();//PWM²¨ÐÎÊä³ö¹Ü½ÅÅäÖÃ
TIM3_Mode_Config(222,121);//˫·PWM²¨ÐβúÉú£¬Õ¼¿Õ±ÈÉèÖÃ
LED_Configuration( );
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 999; //
TIM_TimeBaseStructure.TIM_Prescaler = 83; //1MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//ͨµÀ2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //ÉÏÉýÑØ´¥·¢
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;//²»ÓÃÂ˲¨Æ÷
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//´¥·¢Æ÷TIM4
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //Ñ¡ÔñIC2ΪʼÖÕ´¥·¢Ô´
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM´Óģʽ£º´¥·¢ÐźŵÄÉÏÉýÑØÖØгõʼ»¯¼ÆÊýÆ÷ºÍ´¥·¢¼Ä´æÆ÷µÄ¸üÐÂʼþ
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //Æô¶¯¶¨Ê±Æ÷µÄ±»¶¯´¥·¢
/* TIM enable counter */
TIM_Cmd(TIM4, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);//ʹÄÜÖжÏ
while( 1 )
{
if(pulse_numb_min >= 50 )
LED1_OFF;
}
}
////////////////////
//ÒÔÉÏÊÇÖ÷³ÌÐò²¿·Ö//
////////////////////
/************ÖжÏÀïÃæ×öµÄÊÂÇé**************/
extern unsigned int pulse_numb_min;
void TIM4_IRQHandler(void)
{
/******************************************************************************************/
if( TIM_GetITStatus(TIM4,TIM_FLAG_CC2)== SET )
{
pulse_numb_min = TIM_GetCapture2(TIM4);
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //Çå³ýTIMµÄÖжϴý´¦Àíλ
}
}
void RCC_Config(void)
{
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_AHB2PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM4 channel 2 pin (PB.07) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//¸¡¿ÕÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} |