STM32定時器編碼器模式計算減速電機AB相脈沖個數(shù)
STM32定時器編碼器模式計算減速電機AB相脈沖個數(shù),通過 AB相脈沖的上升沿和下降沿觸發(fā),然后計數(shù)器加一,那么如果檢測到一個周期,有AB兩相,兩個上升沿兩個下降沿的話,計數(shù)器就會四個 加一 。
#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:時鐘預(yù)分頻數(shù)
//這里使用的是定時器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; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時間基數(shù)單位
// TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//選擇TIMx從模式
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中斷,允許更新中斷
//中斷優(yōu)先級NVIC設(shè)置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優(yōu)先級3級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優(yōu)先級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; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時間基數(shù)單位
//TIM_SetCounter(TIM3, 0xFF88);//設(shè)置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;
//中斷優(yōu)先級NVIC設(shè)置
NVIC_InitStructure.NVIC_IRQChannel = TIM_Channel_1|TIM_Channel_2; //TIM3中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優(yōu)先級3級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優(yōu)先級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中斷服務(wù)程序
void TIM3_IRQHandler(void) //TIM3中斷
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //檢查TIM3更新中斷發(fā)生與否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中斷標(biāo)志
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() ; //橫坐標(biāo)
//delay_ms(50);
}
i++ ;
TIM2->CNT = 240;
Frequent() ;
//fre = 1000/(90 /(720*100/72)) ;
//sprintf((char*)speed,"speed : %d ",EncVal );//將LCD ID打印到lcd_id數(shù)組。
//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);//設(shè)置TIMx自動重裝載寄存器值
}
}