资源描述:
《STM32定时器正交编码器接口程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*************************************************************************************************************************************************************/#include"encoder.h"voidTIM3_Mode_Config(void){GPIO_InitTypeDefGPIO_InitStructure;TIM_TimeBase
2、InitTypeDefTIM_TimeBaseStructure;TIM_ICInitTypeDefTIM_ICInitStructure;/*----------------------------------------------------------------*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_StructInit(&GPIO_InitStructure);/*ConfigurePA.06,07asenco
3、derinput*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6
4、GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA,&GPIO_InitStructure);/*----------------------------------------------------------------*/RCC_APB1PeriphClockCmd(RCC_APB1Perip
5、h_TIM3,ENABLE);//使能TIM3TIM_DeInit(TIM3);TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);TIM_TimeBaseStructure.TIM_Period=0xffff;//TIM_TimeBaseStructure.TIM_Prescaler=0;//设置预分频:TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//设置时钟分频系数:不分频TIM_TimeBas
6、eStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式//TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned1;/*初始化TIM2定时器*/TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);/*-----------------------------------------------------------------
7、*///编码配置编码模式TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//TIM_ICPolarity_Rising上升沿捕获TIM_ICStructInit(&TIM_ICInitStructure);TIM_ICInitStructure.TIM_ICFilter=6;//比较滤波器TIM_ICInit(TIM3,&TIM_ICInitStruct
8、ure);//ClearallpendinginterruptsTIM_ClearFlag(TIM3,TIM_FLAG_Update);TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能中断//ResetcounterTIM3->CNT=0;TIM_Cmd(TIM3,ENABLE);//使能定时器3}voidTIM_Init(void){TIM3_Mode_Config();}encoder.h"#ifndef__ENCODER_H#define__ENCODE
9、R_H#include"stm32f10x.h"voidTIM_Init(void);voidTIM2_Mode_Config(void);voidTIM3_Mode_Config(void);#endifMAIN.C#include"stm32f10x.h"#include"delay.h"#include"nvic.h"#include"encoder.h"#include"74HC595.h"volatiles16encoder_num;intmain(void