- UID
- 1023166
- 性别
- 男
- 来自
- 燕山大学
|
调试了2天终于把STM32定时器的计数器模式调试出来了,从PA7,TIM3_CH2输入100个方波后产生一个TIM3, TIM_IT_CC2的中断,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TIM3输出初始化
TIM_DeInit(TIM3); //TIM3 ch2 pin17 pa7
TIM_TimeBaseStructure.TIM_Period = 99; //计数值=100-1
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //基本初始化
TIM_SetCounter(TIM3, 99); //设置 TIM3 计数器寄存器值
TIM_ARRPreloadConfig(TIM3, ENABLE); //设置 TIMx 自动重装载寄存器值
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);
//TIM3通道的捕捉初始化
TIM_SetCompare2(TIM3, 99);
TIM_Cmd(TIM3, state); //启动TIM3
TIM_ClearFlag(TIM3, TIM_IT_CC2);
TIM_ITConfig(TIM3, TIM_IT_CC2, state); //打开中断
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = state; //允许中断
NVIC_Init(&NVIC_InitStructure); //写入设置
void TIM3_IRQHandler(void)
{
TIM_ClearFlag(TIM3, TIM_IT_CC2);
} |
|