打印
[应用相关]

stm32的通用计数器用于编码器的脉冲计数

[复制链接]
972|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
尤彼卡|  楼主 | 2016-8-30 17:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void MY_CONFIG(void)
{
NVIC_InitTypeDef NVIC_InitStructure;  
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
/* TIM4 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);


TIM_DeInit(TIM4);  
TIM_TimeBaseStructure.TIM_Period = 100;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;  
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_SetCounter(TIM4, 100);

TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);


TIM_SetCompare2(TIM4, 100); /*ÉèÖÃ*/
TIM_Cmd(TIM4, ENABLE);
TIM_ClearFlag(TIM4, TIM_IT_CC2);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);


NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);   
}


void TIM4_IRQHandler(void)
{
int IC2Value;

IC2Value = TIM_GetCapture2(TIM4);
printf("\r\n %d\r\n",IC2Value);
TIM_ClearFlag(TIM4, TIM_IT_CC2);        
}
为什么计不了数,感觉中断不起作用
沙发
Stannis| | 2016-8-30 17:34 | 只看该作者
什么触发方式

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

116

主题

774

帖子

1

粉丝