本帖最后由 s1351141178 于 2023-6-6 09:38 编辑
使用MM32F013F6 TIM1的外部时钟功能,配置如下:
void TIM1_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //开启GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //Open TIM1 clock
//GPIO初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_4); //PA6复用功能的AF1/*特别注意:引脚功能复用参考数据手册,不要参考库函数中的代码说明*/
TIM_TimeBaseStructure.TIM_Period = 0xffff; //自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_DIV2,TIM_ExtTRGPolarity_NonInverted,0x00);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1, ENABLE);
}
TIM初始化后,并给PA6引脚输入方波信号,定时器可以正常工作。
|