STM32定时器正交编码器接口程序(互联网+)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*----------------------------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3
TIM_DeInit(TIM3);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//Reset counter
TIM3->CNT =0;
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM_Init(void)
{
TIM3_Mode_Config();
}
encoder.h"
#ifndef __ENCODER_H
#define __ENCODER_H
#include "stm32f10x.h"
/******************************************************************************
*******************************************************************************/
#include "encoder.h"
void TIM3_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*-----------------------------------------------------------------*/
//编码配置编码模式
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
void TIM_Init(void);
void TIM2_Mode_Config(void);
void TIM3_Mode_Config(void);
#endif
MAIN.C
#include "stm32f10x.h"
#include "delay.h"
#include "nvic.h"
#include "encoder.h"
#include "74HC595.h"
volatile s16 encoder_num;
int main(void)
{
SystemInit();// 72m时钟
SysTick_Init();
TIM_Init();
NVIC_Config();
GPIO_74HC595_Config();
while (1)
{
encoder_num=TIM_GetCounter(TIM3);
dis_595(encoder_num,encoder_num);
}
}
TIM_TimeBaseStructure.TIM_Period =0xffff; //
TIM_TimeBaseStructure.TIM_Prescaler =0;//设置预分频:
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分频系数:不分频
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
/*初始化TIM2定时器*/
/*----------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIMwk.baidu.comFLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3
TIM_DeInit(TIM3);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//Reset counter
TIM3->CNT =0;
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM_Init(void)
{
TIM3_Mode_Config();
}
encoder.h"
#ifndef __ENCODER_H
#define __ENCODER_H
#include "stm32f10x.h"
/******************************************************************************
*******************************************************************************/
#include "encoder.h"
void TIM3_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*-----------------------------------------------------------------*/
//编码配置编码模式
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
void TIM_Init(void);
void TIM2_Mode_Config(void);
void TIM3_Mode_Config(void);
#endif
MAIN.C
#include "stm32f10x.h"
#include "delay.h"
#include "nvic.h"
#include "encoder.h"
#include "74HC595.h"
volatile s16 encoder_num;
int main(void)
{
SystemInit();// 72m时钟
SysTick_Init();
TIM_Init();
NVIC_Config();
GPIO_74HC595_Config();
while (1)
{
encoder_num=TIM_GetCounter(TIM3);
dis_595(encoder_num,encoder_num);
}
}
TIM_TimeBaseStructure.TIM_Period =0xffff; //
TIM_TimeBaseStructure.TIM_Prescaler =0;//设置预分频:
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分频系数:不分频
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
/*初始化TIM2定时器*/
/*----------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIMwk.baidu.comFLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断