想做小车测速,要用TIM3计算脉冲个数
现在我用学习板弄的时候,用TIM3通道3输出PWM方波模拟测速器。
然后我想用TIM3的通道2计算脉冲。
发现通道3输出方波不对。而单独使用通道3时候,通道3输出的方波是符合配置要求的。
那么问题来了,在同一个定时器上,一个通道产生方波。一个计算频率,这样做行不行?
void TIM3_CH3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//打开TIM3时钟
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3 , ENABLE );
//开启IO时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE );
//选择GPIOB0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//复用TIM3_CH3输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//IO输出速度
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//初始化GPIOB0
GPIO_Init( GPIOB , &GPIO_InitStructure );
}
void TIM3_CH2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//开启IO时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA , ENABLE );
//选择GPIOA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
//浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//初始化GPIOA7
GPIO_Init( GPIOA , &GPIO_InitStructure );
}
void TIM3_PWM3_Output( uint16_t TIM_Period_x , uint16_t CCRx_Val )
{
//定时器基础配置
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//输出比较器配置
TIM_OCInitTypeDef TIM_OCInitStructure;
//***************初始化分频时钟基础配置******************
//当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Period = ( TIM_Period_x - 1 );
//设置预分频:不预分频,即72MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
//设置时钟分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//向上计数模式//TIM_CounterMode_Down
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//初始化基础配置
TIM_TimeBaseInit( TIM3 , &TIM_TimeBaseStructure );
//********************配置PWM1跳变阀值,使能PWM等********************
//配置为PWM1模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//当定时器计数值小于CCR1_Val时为高电平
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//打开通道3
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//设置跳变阀值
TIM_OCInitStructure.TIM_Pulse = CCRx_Val ;
//初始化通道3
TIM_OC3Init( TIM3 , &TIM_OCInitStructure );
//配置比较寄存器TIM_CCR预装载
TIM_OC3PreloadConfig( TIM3 , TIM_OCPreload_Enable );
//使能TIM3自动重载ARR
TIM_ARRPreloadConfig( TIM3 , ENABLE );
//使能定时器TIM3
TIM_Cmd( TIM3 , ENABLE );
}
void TIM3_PWM2_Input(void)
{
//设置时钟基准
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//定义TIM3捕足结构体
TIM_ICInitTypeDef TIM_ICInitStructure;
//***************初始化分频时钟基础配置******************
//设置时钟分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//设置计数上限
TIM_TimeBaseStructure.TIM_Period = 0xffff;
//设置分频系数
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
//设置计数方向
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//初始化BASE
TIM_TimeBaseInit( TIM3 , &TIM_TimeBaseStructure );
//*****************配置PWM捕捉通道、模式等******************
//设置TIM中断通道2
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
//设置捕获方式为下降沿
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//设置是否使用交叉触发?直接触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
//设置计数方式,捕抓每一个触发,类似设置分频
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
//设置输入比较滤波器,参数 0x00~0x0f之间
TIM_ICInitStructure.TIM_ICFilter = 0x8;
//初始化TIM3配置
TIM_ICInit( TIM3 , &TIM_ICInitStructure );
//打开中断
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}
void TIM3_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Config(void)
{
TIM3_CH3_GPIO_Config();
TIM3_CH2_GPIO_Config();
TIM3_PWM3_Output( 1000 , 500 );
TIM3_PWM2_Input(); //注释掉就没事。这个问题怎么破
TIM3_NVIC_Config();
}
int main(void)
{
TIM3_Config();
while(1)
{
}
}
代码已附上。谢谢 |