初级会员

- 积分
- 70
- 金钱
- 70
- 注册时间
- 2017-12-7
- 在线时间
- 17 小时
|
1金钱
如题,按照网上的教程,配置过参数,但是计数器并没有计数,一直停留在0,下面是代码,请各位看看是配置有问题还是其他的原因。
#include "stm32f10x.h"
#include "encode.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
void TIM4_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);//使能GPIOB时钟
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseStructure.TIM_Period=24*4; //设定计数器重装值 TIMx_ARR = 359*4
TIM_TimeBaseStructure.TIM_Prescaler=0; //TIM4时钟预分频值
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
//TIM_ICInitStructure.TIM_Channel=;
TIM_ICInitStructure.TIM_ICFilter=0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//Clear all pending interrupts
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_Trigger,ENABLE);
//Reset counter
TIM4->CNT = 0;
TIM_Cmd(TIM4, ENABLE); //使能 TIMx 外设
}
void TIM4_IRQHandler(void) //TIM3中断
{
if (TIM4->SR&0x0001) //检查TIM3更新中断发生与否
{
;
}
TIM4->SR&=~(1<<0); //清除TIMx更新中断标志
}
|
最佳答案
查看完整内容[请看2#楼]
已经解决了,在两个引脚需要接10K的上拉电阻,接到5V,但是还有一个问题,现在计数只能记到9就重新从0开始计数了,不知道是怎么回事?
|