定时器配置代码如下
[mw_shl_code=applescript,true]void EC11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //EC11A MOD B A
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //普通输入模式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //定时器4中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断使能
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 1333; //设定计数器重装值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM4时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising); //使用编码器模式3
TIM_ICStructInit(&TIM_ICInitStructure); //将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 1; //选择输入比较滤波器
TIM_ICInit(TIM4, &TIM_ICInitStructure); //将TIM_ICInitStructure中的指定参数初始化TIM3
;
TIM_ClearFlag(TIM4, TIM_FLAG_Update); //清除TIM3的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //开启TIM3更新标志使能
TIM_SetCounter(TIM4,0); //
TIM_Cmd(TIM4, ENABLE); //启动TIM3定时器
}[/mw_shl_code]
使用的是定时器TIM4 CH1 CH2
[mw_shl_code=applescript,true]int main(void)
{
// u8 i;
u32 n_Counter;
delay_init(168);
KEY_Init();
EC11_Init();
while (1)
{
// i=KEY_Scan(1);
n_Counter = TIM_GetCounter(TIM4);
n_Counter =n_Counter;
delay_ms(500);
}
}[/mw_shl_code]
循环读取计数器的值,但是没有变化
示波器能看到输入引脚的波形 波形没问题
原理图如下
求各位大神解答
您的意思是我用这个办法读脉冲浪费了么。
一周热门 更多>