硬件连接 :PE.7复用为TIM1.ETR, PD.13复用为TIM4-CH2(输出PWM),PE.7连接PD.13,对PWM计数。
问题如下:1想配置PE.8输出PWM,但是PWM和TIM1的周期一样,担心溢出所以改用TIM4对PE.7计数。实际可以用PE.8?
2.要完成测车轮转速,是不是还要开定时器,在T时间内对外部脉冲计数,从而得到转速?
3.能不能使用软件来定时采样脉冲,再计数处理?
下面脉冲计数的代码:
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Delay(__IO uint32_t nCount);
void tim1_confi(void);
void tim4_confi(void); //TIM4初始化
u16 counter=0;
int main(void)
{
// u8 i=0;
/* System Clocks Configuration */
RCC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/*Configure tim1 and tim4 */
tim1_confi();
tim4_confi();
while (1)
{
counter=TIM_GetCounter(TIM1); //只能读出最终值,采样的时钟要比输入信号快。
}
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void RCC_Configuration(void)
{
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();
/* EnableGPIO Port, GPIO_LED and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器TIM1时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //使能PE端口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器TIM1时钟
GPIO_PinRemapConfig (GPIO_FullRemap_TIM1 ,ENABLE ); //重映射
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOE, &GPIO_InitStructure); //根据设定参数初始化GPIOE.7
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PE.8 端口配置
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
// GPIO_Init(GPIOE, &GPIO_InitStructure); //根据设定参数初始化GPIOE.8
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PD端口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器TIM4时钟
GPIO_PinRemapConfig (GPIO_Remap_TIM4 ,ENABLE ); //重映射
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void tim1_confi(void) //TIM1初始化
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit( TIM1 ) ;
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Period = 0XFFFF; //
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM1-ETR Configuration */
TIM_ETRClockMode2Config (TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0);
TIM_SetCounter( TIM1,0) ; //从零计数
TIM_Cmd(TIM1, ENABLE); //使能打开
}
void tim4_confi(void) //TIM4初始化
{
// TIM4-CH2基本计数器设置(设置PWM频率)
// 频率=TIM1_CLK/(ARR+1)
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_DeInit( TIM4 ) ;
TIM_BaseInitStructure.TIM_Period = 100; // 确定T=2.35ms,34%
TIM_BaseInitStructure.TIM_Prescaler = 72-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_BaseInitStructure);
//启用ARR的影子寄存器(直到产生更新事件才更改设置)
TIM_ARRPreloadConfig(TIM3, ENABLE);
//TIM4_OC2模块设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
//TIM3开启
TIM_Cmd(TIM4, ENABLE);
//TIM1_OC通道输出PWM(一定要加)
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
|