欧姆龙E6A2编码器测速OLED12864显示(基于STM32F407平台)


main.c文件


#include "sys.h"#include "delay.h"#include "usart.h"#include "led.h"#include "SpeedCheck.h"#include "OLED12864.h"extern float speed;int main(void){ delay_init(168); //延时初始化 uart_init(115200); //串口初始化波特率为115200OLED_Init();NVIC_Config();Timer_Init();TIM_Init();while(1){OLEDPritnf("Speed:%0.2f\r\n",speed);delay_ms(1000);}}
SpeedCheck.c文件

//光电编码器测速,TIM3_ETR,PD2
#include "SpeedCheck.h"

uint16_t Qian_Count,Hou_Count;
uint8_t flag;
float speed;

void TIM3_IRQHandler(void)
{
#if 0
	if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET)
	{
		//TIM_ClearITPendingBit(TIM1,TIM_IT_Update); //清除标志位
		GPIOC-> ODR ^= GPIO_Pin_6;
		TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //清中断
		//TIM1->EGR |= 0x0001;
		//TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //清中断
	}
#endif

	if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
	{
	 	TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除标志位
		GPIOC-> ODR ^= GPIO_Pin_8;
		flag++;	 
	}
}

//定时器2的中断函数
void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)
	{
	 	TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除标志位
		GPIOC-> ODR ^= GPIO_Pin_9;
		Qian_Count = TIM3->CNT;
		speed=(800*flag+Qian_Count-Hou_Count)*2.0/200.0;
		flag=0;
		Hou_Count=Qian_Count;	 
	}
}

void NVIC_Config(void) 
{  
		NVIC_InitTypeDef NVIC_InitStructure;    

		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置系统中断优先级分组2 
		NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断 
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; // 抢占优先级1
		NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;  //子优先级2
		NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;  //IRQ通道使能
		NVIC_Init(&NVIC_InitStructure);

		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);   //设置系统中断优先级分组2 
		NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;  //定时器2中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;  // 抢占优先级2
		NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;   //子优先级2
		NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;  //IRQ通道使能
		NVIC_Init(&NVIC_InitStructure);   
}

void TIM_Init(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;

		
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  	//TIM3时钟使能    
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 	//使能PORTA时钟	
		
		
		GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_TIM3); //GPIOD2复用为定时器3

		
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;           //GPIOFD
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //复用功能
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	//速度100MHz
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
		GPIO_Init(GPIOD,&GPIO_InitStructure);              //初始化PD2
	
		
		//配置TIMER3作为计数器
		TIM_DeInit(TIM3);
		TIM_TimeBaseStructure.TIM_Period = 800;	//编码器转四圈,自动重装载值arr
		TIM_TimeBaseStructure.TIM_Prescaler = 0x00;//定时器分频psc
		TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;//设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration

		TIM_ClearFlag(TIM3, TIM_FLAG_Update);	
		TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
		//使用ETR时钟作为计数时钟需要设置为外部时钟模式2,故调用该函数,在设置时1、关闭预分频:TIM_ExtTRGPSC_OFF
		TIM_SetCounter(TIM3, 0);
		TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//允许定时器3更新中断
		TIM_Cmd(TIM3, ENABLE);//使能定时器3
}

void Timer_Init(void)
{
		TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

		//打开TIM2外设的时钟
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);	//使能TIM2时钟

		/*定时器2的设置:72分频,500ms中断一次,向上计数*/
		TIM_TimeBaseStructure.TIM_Period = 5000-1; 	//周期为2S
		TIM_TimeBaseStructure.TIM_Prescaler =8400-1 ; //分频后时钟为1KHz 
		TIM_TimeBaseStructure.TIM_ClockDivision = 0;
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化定时器
		
	  TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);  //允许定时器2更新中断
		TIM_Cmd(TIM2,ENABLE);	   //使能定时器2
}





SpeedCheck.h文件

#ifndef __SpeedCheck_H
#define __SpeedCheck_H
#include "sys.h"


void NVIC_Config(void);
void TIM_Init(void);
void TIM3_IRQHandler(void);
void TIM2_IRQHandler(void);
void Timer_Init(void);

#endif



阅读更多

没有更多推荐了,返回首页