新手入门
- 积分
- 15
- 金钱
- 15
- 注册时间
- 2017-10-1
- 在线时间
- 1 小时
|
1金钱
本帖最后由 zhang289444124 于 2020-5-2 18:40 编辑
下面是我配置定时器1为外部输入计数功能的代码,请指点哪里有问题,现在读TIM_GetCounter(TIM1)一直都是0????谢谢
void TIM1_ETR_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
//TIM_ICInitTypeDef TIM1_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOA12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA12
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_TIM1); //PA12复用位定时器1
TIM_TimeBaseStructure.TIM_Prescaler=0; //定时器分频
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_ITRxExternalClockConfig(TIM1,TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 5);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1, ENABLE);
}
|
|