- #include "stm32f10x.h"
- #include "stm32f10x_conf.h "
- void Delay(__IO u32 nCount);
- void TIM4_EIR_Init(void);
-
- uint16_t COUNT = 0;
-
- int main(void)
- {
- TIM4_EIR_Init();
- while (1)
- {
- TIM_SetCounter(TIM4, 0); // CLEAR
- COUNT=TIM_GetCounter(TIM4);
- TIM_SetCounter(TIM4, 0); // CLEAR
- COUNT=TIM_GetCounter(TIM4);
- TIM_SetCounter(TIM4, 0); // CLEAR
- }
- }
- void Delay(__IO u32 nCount) //简单的延时函数
- {
- for(; nCount != 0; nCount--);
- }
-
- void TIM4_EIR_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- /* GPIOE clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
- //Config PE0 为浮空输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- TIM_DeInit(TIM4);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // Time base configuration
-
- TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
- TIM_SetCounter(TIM4, 0);
- TIM_Cmd(TIM4, ENABLE);
-
- }
- }
这是代码,编译正常。
用的是STM32F103VET的片子,TIM4的 ETR ,输入引脚为PE0
在主函数单步运行的时,没有脉冲输入的情况下,TIM_GetCounter(TIM4) 的值会变化,虽然能被IM_SetCounter(TIM4, 0)函数清零,但是单步到下一步又会计数,好像是自己在计数,而不是计外部脉冲数,求指点函数配置哪里有问题,刚开始使用STM32,非常感谢。
|