void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
// TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
//TIM3_Encoder_Write(65416) ;
TIM_Cmd(TIM3, ENABLE); //使能TIMx
}
/**********TIM2_Encoder******/
void TIM2_Encoder_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
//定时器TIM2初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
//TIM_SetCounter(TIM3, 0xFF88);//设置TIMx自动重装载寄存器值
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,
TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
//TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
//TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM_ICInit(TIM2, &TIM_ICInitStructure);
//TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//TIM2->CNT = 120;
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM_Channel_1|TIM_Channel_2; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
//TIM3_Encoder_Write(65416) ;
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
//TIM2->SMCR ^= 0x8000 ;
EncVal = TIM_GetCounter(TIM8);
if(EncVal>=240)
{
EncVal = 240;
}
LCD_DrawPoint(i, 5+EncVal);
if(i==320)
{
i =0;
LCD_Clear(WHITE);
coordinate() ; //横坐标
//delay_ms(50);
}
i++ ;
TIM2->CNT = 240;
Frequent() ;
//fre = 1000/(90 /(720*100/72)) ;
//sprintf((char*)speed,"speed : %d ",EncVal );//将LCD ID打印到lcd_id数组。
//sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);
//LCD_ShowString(200,200,200,16,16,"EncVal");
LCD_ShowxNum(250,200,EncVal,5,16,0);
printf("%d\r\n",EncVal) ;
TIM_SetAutoreload(TIM1, 0xFF88);//设置TIMx自动重装载寄存器值
}
}
这是我的程序 TIM3是 设置50ms读取一次值。TIM2是编码器模式 |