STM32F4对外部脉冲无法计数,紧急求助,请用过F4外部脉冲计数的大虾不吝赐教!

2019-07-21 00:11发布

源码如下,ETR脚(PA15)输入脉冲

void TIMER2_Counter_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    
    TIM_DeInit(TIM2);

    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;    
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure);

//    TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
//    TIM_ETRClockMode1Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
    TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);

    TIM_SetCounter(TIM2, 0);
    
    TIM_Cmd(TIM2, ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
正点原子
1楼-- · 2019-07-21 03:17
参考M3的即可,差异不大.
M3的脉冲统计,论坛我发过帖子的.
wanwb
2楼-- · 2019-07-21 07:53
回复【2楼】正点原子:
---------------------------------
多谢原子大师的指点!找到了一点问题,现在是TIM2~TIM4可以正常计数,TIM1还是不行,TIM2~4和TIM1的配置是一样的,痛苦了2天了!!!详细代码如下,请大师再帮分析指点!

//??????????????
void TIMER1_Counter_Init(void)
{    
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //?±??????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //?±??????

    //IO????
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;        //??????
    GPIO_Init(GPIOE, &GPIO_InitStructure);      //??????IO    
    
    //?????÷???? 
    TIM_DeInit(TIM1);

    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;     //?¤·???
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;     //????????
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;      //×?????×°????     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);        //???????¨?±?÷

//    TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);      //????TIx?±??????
//    TIM_ETRClockMode1Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????1????
    TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????2????

    TIM_SetCounter(TIM1, 0);        //?????????÷
    
    TIM_Cmd(TIM1, ENABLE);      //?¨?±?÷????   
}

//??????????????
void TIMER2_Counter_Init(void)
{    
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //?±??????  

    //IO????
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;        //??????
    GPIO_Init(GPIOA, &GPIO_InitStructure);      //??????IO
   
    //?????÷???? 
    TIM_DeInit(TIM2);

    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;     //?¤·???
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;     //????????
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;      //×?????×°????     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);        //???????¨?±?÷

//    TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);      //????TIx?±??????
//    TIM_ETRClockMode1Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????1????
    TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????2????

    TIM_SetCounter(TIM2, 0);        //?????????÷
    
    TIM_Cmd(TIM2, ENABLE);      //?¨?±?÷????   
}

//??????????????
void TIMER3_Counter_Init(void)
{    
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //?±??????

    //IO????
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;        //??????
    GPIO_Init(GPIOD, &GPIO_InitStructure);      //??????IO
   
    //?????÷???? 
    TIM_DeInit(TIM3);

    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;     //?¤·???
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;     //????????
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;      //×?????×°????     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);        //???????¨?±?÷

//    TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);      //????TIx?±??????
//    TIM_ETRClockMode1Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????1????
    TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????2????

    TIM_SetCounter(TIM3, 0);        //?????????÷
    
    TIM_Cmd(TIM3, ENABLE);      //?¨?±?÷????   
}

//??????????????
void TIMER4_Counter_Init(void)
{    
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //?±??????

    //IO????
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_TIM4);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;        //??????
    GPIO_Init(GPIOE, &GPIO_InitStructure);      //??????IO
    
    //?????÷???? 
    TIM_DeInit(TIM4);

    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;     //?¤·???
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;     //????????
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;      //×?????×°????     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);        //???????¨?±?÷

//    TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);      //????TIx?±??????
//    TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????1????
    TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);      //?????±??????2????

    TIM_SetCounter(TIM4, 0);        //?????????÷
    
    TIM_Cmd(TIM4, ENABLE);      //?¨?±?÷????   
}
long7qazzaq
3楼-- · 2019-07-21 08:44
 精彩回答 2  元偷偷看……
frankzhan
4楼-- · 2019-07-21 09:57
你好 你能不能把你发的M3的STM32F4对外部脉冲计数的帖子的地址发一下 你的帖子太多了我找不到啊
frankzhan
5楼-- · 2019-07-21 13:12
如果可以的话能不能发一下源程序到我邮箱我看一下 看能不能找到BUG邮箱615565132@qq.com

一周热门 更多>