微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > stm32的通用计数器用于编码器的脉冲计数

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

时间:10-02 整理:3721RD 点击:
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);       
}
为什么计不了数,感觉中断不起作用


已退回5积分

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top