STM32定时器编码器模式计算减速电机AB相脉冲个数
扫描二维码
随时随地手机看文章
STM32定时器编码器模式计算减速电机AB相脉冲个数,通过 AB相脉冲的上升沿和下降沿触发,然后计数器加一,那么如果检测到一个周期,有AB两相,两个上升沿两个下降沿的话,计数器就会四个 加一 。
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM8_Encoder_Init(u16 arr,u16 psc) ;
void Frequent(void) ;
#endif
#include "timer.h"
#include "led.h"
#include "delay.h"
#include "lcd.h"
#include "usart.h"
#define PI 3.14
//#define ar 719
//#define ps 99
#define TIM 90.0
//通用定时器3中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器3!
u16 i =1,EncVal ,num = 0 ;
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
}
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 = EXTI_Trigger_Rising_Falling; //上升沿捕获
//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
}
extern void coordinate(void) ;
u8 speed[12] ;
//extern int ar ,ps ;
double fre ;
int p;
double sin(double) ;
double GetTrigVal(u16 y)
{
double t,n;
n = (double)(y/90.0) *2*PI ;
t = sin(n)*100 ;
return t;
}
void Frequent(void)
{
//fre = 1000/((int)TIM /((ar+1)*(ps+1)/72)) ;
fre = 16.0 /5.0;
}
//定时器3中断服务程序
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(TIM2);
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:X",lcddev.id);
//LCD_ShowString(200,200,200,16,16,"EncVal");
LCD_ShowxNum(250,200,EncVal,5,16,0);
printf("%drn",EncVal) ;
TIM_SetAutoreload(TIM1, 0xFF88);//设置TIMx自动重装载寄存器值
}
}