本帖最后由 zhl100 于 2014-1-18 17:07 编辑
求stm32f207 定时器ETR模式例程?
同样的配置107就可以对外部脉冲计数,207就不行,哪里有问题?IO口输入能读到0或是1,定时器初始值设置的6或是8也能从TIM_GetCounter(); 中读到。就是说我IO配置没问题,定时器配置也没问题,问题出在就是读不到外部脉冲数,而107 TIM1和TIM4读的都好好的,问问版主,why?我继续找。。。
调试代码。。。主要的时钟,定时器,IO的代码,有点乱,请版主看看,怎么回事
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //重映射
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
// TIM_ETRClockMode1Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM1, 6);
TIM_Cmd(TIM1, ENABLE);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
// TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM4, 8);
TIM_Cmd(TIM4, ENABLE);
while (1) {
// n_Counter4 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_0);
// n_Counter4 = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_7);
n_Counter4 = TIM_GetCounter(TIM4);
n_Counter1 = TIM_GetCounter(TIM1);
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* TIM2 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_DeInit(); /* RCC system reset(for debug purpose) */
RCC_HSEConfig(RCC_HSE_ON); /* Enable HSE */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); /* Wait till HSE is ready */
if(HSEStartUpStatus == SUCCESS) {
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(ENABLE );
FLASH_SetLatency(FLASH_Latency_2); /* Flash 2 wait state */
RCC_HCLKConfig(RCC_SYSCLK_Div1); /* HCLK = SYSCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK2 = HCLK */
RCC_PCLK1Config(RCC_HCLK_Div2); /* PCLK1 = HCLK/2 */
// RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_6); /* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE, 63, 432, 2,5);
RCC_PLLCmd(ENABLE); /* Enable PLL */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} /* Wait till PLL is ready */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Select PLL as system clock source */
while(RCC_GetSYSCLKSource() != 0x08) {} /* Wait till PLL is used as system clock source */
}
|