STM32学习(七)TIM编码器测速 新手入门及常见问题解决

main:

#include "delay.h"
#include "encoder.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "Timer.h"
int16_t speed;//利用定时中断后,就不会阻碍主程序的进行设置中断函数
uint16_t Num;
void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		speed = encoder_get();
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}	

 	
 int main(void)
 {	 
 	
	   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init();	    	 //延时函数初始化	  
	
	uart_init(115200);	 	//串口初始化为115200
 	
	LCD_Init();
	POINT_COLOR=RED;
	Timer_Init();
	encoder_Init();

  	while(1) 
	{		 
			
		
//		LCD_ShowxNum(30,40,Num,1,24,1);
		LCD_ShowxNum(50,60,speed,5,24,1);
		delay_ms(500);
        LCD_Clear(GREEN);
	} 
	  
}

encoder:

#include "stm32f10x.h"                  // Device header

void encoder_Init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//time2在APB1里面
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 //使能PA端口时钟

	GPIO_InitTypeDef  GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;				 //LED0-->PB.5 端口配置
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //尽量与外部模块保持一致,外部输入为高则设置为上拉输?
	//如果不确定外部状态或外部输出频率非常小 则为浮空输入 但会受外部噪声影响	 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB.5

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
    TIM_TimeBaseInitStruct.TIM_Prescaler =1-1;//分频器值,72M/7200 = 10K PSC
    TIM_TimeBaseInitStruct.TIM_CounterMode =  TIM_CounterMode_Up;//向上计数	
    TIM_TimeBaseInitStruct.TIM_Period =65536-1; //自动寄存器的值(定时器的周期)相乘结果为1秒 ARR
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//分频为1
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0 ;//高级计时器才有
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);	
	//初始化捕获单元
	TIM_ICInitTypeDef  TIM_ICInitStruct;
	TIM_ICInitStruct.TIM_Channel = TIM_Channel_1; //通道    
	TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; //极性
	TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择通道输入 直连通道
	TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; //分频
	TIM_ICInitStruct.TIM_ICFilter = 0XF; //滤波
	TIM_ICInit(TIM3,&TIM_ICInitStruct);
	
	TIM_ICInitTypeDef  TIM_ICInitStruct2;
	TIM_ICInitStruct2.TIM_Channel = TIM_Channel_2; //通道    
	TIM_ICInitStruct2.TIM_ICPolarity = TIM_ICPolarity_Rising; //极性
	TIM_ICInitStruct2.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择通道输入 直连通道
	TIM_ICInitStruct2.TIM_ICPrescaler = TIM_ICPSC_DIV1; //分频
	TIM_ICInitStruct2.TIM_ICFilter = 0XF; //滤波
	TIM_ICInit(TIM3,&TIM_ICInitStruct2);
	//配置编码器接口
	TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//TIM_ICPolarity_Rising极性不反向
	
	TIM_Cmd(TIM3,ENABLE);//开启定时器

}

int16_t encoder_get(void)
{
	//实现编码器测速
  int16_t temp;
  temp = TIM_GetCounter(TIM3);
  TIM_SetCounter(TIM3,0);
  return temp;

}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值