资源简介

STM32F103之实验2控制1个电机并采用编码器读取电机转速(代码)

资源截图

代码片段和文件信息


#include “encoder.h“
#include “led.h“
///TIM4 编码器输入,TIM4_CH1作A相,TIM4_CH2作B相GPIO_Pin_6|GPIO_Pin_7;


TIM_ICInitTypeDef  TIM_ICInitStructure;

void TIM4_Encoder_Init(u16 arru16 psc)
{

GPIO_InitTypeDef  GPIO_InitStructure;


TIM_TimebaseInitTypeDef  TIM_TimebaseStructure;
 NVIC_InitTypeDef NVIC_InitStructure;


 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 ENABLE);// 使能定时器时钟
 

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ENABLE); //io口使能使用到的捕获编码器AB相借,
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  //浮空输入  
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

 GPIO_Init(GPIOB &GPIO_InitStructure);   
 //推挽输出 ,IO口速度为50MHz

 //初始化TIM4的时机单元
    TIM_TimebaseStructure.TIM_Period = 899; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值  80K
TIM_TimebaseStructure.TIM_Prescaler =0; //设置用来作为TIMx时钟频率除数的预分频值  不分频
TIM_TimebaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimebaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
TIM_TimebaseInit(TIM4 &TIM_TimebaseStructure); //根据TIM_TimebaseInitStruct中指定的参数初始化TIMx的时间基数单位

 
 //配置对应寄存器为编码器接口模式以及配置相关的输入捕获配置
  //THE MODE OF ENCODER 
  
   TIM_EncoderInterfaceConfig(TIM4 TIM_EncoderMode_TI12TIM_ICPolarity_Rising TIM_ICPolarity_Rising);  
 //TIM_ICPolarity_Rising上升沿捕获 
    
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01  选择输入端 IC1映射到TI1上
   TIM_ICInitStructure.TIM_ICPolarity = TIM_

评论

共有 条评论