我用的是KEIL,代码基本是抄自正交编码接口应用笔记。 运行KEIL的Simulator (没有设置断点), 并在端口PA6(TIM3_CH1)和PA7(TIM3_CH2)上按如下顺序输入信号模拟正交编码器 PA6 1 1 0 0 1 1 0 0 PA7 0 1 1 0 0 1 1 0 或者逆序: PA6 0 1 1 0 0 1 1 0 PA7 1 1 0 0 1 1 0 0 都没有得到TIM3计数器值(程序中是eCounter )的更新,但如果在程序中加入读取PA6和PA7的代码,是可以看到PA6和PA7是按上述顺序更新的。 大家帮忙看看是哪里出了问题。先谢谢了!
int main(void) {
#ifdef DEBUG debug(); #endif
/* Configure the system clocks */ RCC_Configuration(); /* NVIC Configuration */ NVIC_Configuration();
ENC_Init();
eCounter = TIM3->CNT; eTemp =0; while(1) { eTemp++; if (eTemp==100000) { eTemp=0; eCounter = TIM3->CNT; //eCounter = TIM_GetCounter(TIM3); } } }
void ENC_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Encoder unit connected to TIM3, 4X mode */ GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM3 clock source enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Enable GPIOA, clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_StructInit(&GPIO_InitStructure); /* Configure PA.06,07 as encoder input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the TIM3 Update Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */ // TIM_DeInit(TIM3); //TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling TIM_TimeBaseStructure.TIM_Period = (4*125)-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 6; TIM_ICInit(TIM3, &TIM_ICInitStructure); // Clear all pending interrupts //TIM_ClearFlag(TIM3, TIM_FLAG_Update); //TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //Reset counter //TIM3->CNT = 0; //ENC_Clear_Speed_Buffer(); TIM_Cmd(TIM3, ENABLE); } |