定时器编码器模式

做一个这样的信号检测
在这里插入图片描述


int main(void)
{
	
  Stm32_Clock_Init(9);	//系统时钟设置
  delay_init(72);	  		//延时初始化
  LED_Init();
  /*编码器工作模式*/
  TIM3_Encoder_TI1TI2();
  
  USART1_Init(115200);
	TIM3->CNT=0;
	LED_R_OFF;
	LED_G_OFF;

  	while(1)
	{ 
		LED_R_ON;		
		delay_ms(100);
		printf("%d  ",TIM3->CNT);   
		LED_G_ON;		
		delay_ms(100);
		printf("%d  ",TIM3->CNT);
		LED_R_OFF;		
		delay_ms(100);
		printf("%d  ",TIM3->CNT);
		LED_G_OFF;		
		delay_ms(100);
		printf("%d  \r\n",TIM3->CNT);

	}	
}





void TIM3_GPIO_Init(void)
{
	RCC->APB2ENR|=1<<2;	//GPIOA时钟使能
 	GPIOA->CRL&=0X00FFFFFF; 
	GPIOA->CRL|=0X88000000;//PA6 PA7 上拉输入
//	GPIOC->ODR|=3<<6;//输出高
}


/*
根据TI1FP1的电平,计数器在TI2FP2的边沿向上/下计数。
1、计数器在TI2有上升沿和下降沿变化
2、TI1为低时,计数器在TI2的上升沿减小,下降沿增加
   TI1为高时,计数器在TI2的上升沿增加,下降沿减小
3、注意时TIx而不是CHx
4、与	TIM3->CR1 &= ~TIM_CR1_DIR;//向上计数无关
例:TI1保持低   CNT在脉冲中会有 1 0 0 1
   TI1保持高   CNT在脉冲中会有 0 1 1 0
*/
void TIM3_Encoder_TI1(void)
{
	RCC->APB2ENR|=1<<2;	//GPIOA时钟使能
 	GPIOA->CRL&=0X00FFFFFF; 
	GPIOA->CRL|=0X88000000;//PA6 PA7 上拉输入
	/*TIM3左编码器接口定时器时钟配置*/
	RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
	RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST;
	RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;
	
	TIM3->ARR = 1000;																		//设定计数器自动重装值
	TIM3->PSC = 0;																		//预分频器不分频

	TIM3->CR1 |= TIM_CR1_ARPE;													//ARR预装载使能
	TIM3->CR1 &= ~TIM_CR1_CMS;													//RESET:边沿对其模式;SET:中央对齐模式
	TIM3->CR1 &= ~TIM_CR1_DIR;													//向上计数
//  TIM3->CR1 |= TIM_CR1_DIR;													//向下计数
	
//	TIM3->CR1 |= TIM_CR1_OPM;													//单脉冲模式
	TIM3->CR1 |= TIM_CR1_URS;														//只有计数器溢出/下溢才产生中断或DMA请求
	TIM3->SMCR |= TIM_SMCR_SMS_0;//001:编码器模式1 – 根据TI1FP1的电平,计数器在TI2FP2的边沿向上/下计数。
	/******通道1配置******/
	TIM3->CCMR1 |= TIM_CCMR1_CC1S_0;										//CC1通道被配置为输入,映射到TI2上
	TIM3->CCMR1 &= ~TIM_CCMR1_IC1PSC;										//捕获不分频
	TIM3->CCMR1 |= (TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_1);//0011: fSAMPLING=fCK_INT, N=8
	TIM3->CCER &= ~TIM_CCER_CC1P;												//上升沿捕获
//	TIM3->CCER |= TIM_CCER_CC1E;												//OC1使能

	TIM3->CR1 |= TIM_CR1_CEN;														//启动定时器3	
}
/*
根据TI2FP2的电平,计数器在TI1FP1的边沿向上/下计数。
1、计数器在TI1有上升沿和下降沿变化
2、TI2为低时,计数器在TI1的上升沿增加,下降沿减小
   TI2为高时,计数器在TI1的上升沿减小,下降沿增加
3、注意时TIx而不是CHx
4、与	TIM3->CR1 &= ~TIM_CR1_DIR;//向上计数无关
例:TI2保持低   CNT在脉冲中会有 0 1 1 0
   TI2保持高   CNT在脉冲中会有 1 0 0 1
*/
void TIM3_Encoder_TI2(void)
{
	RCC->APB2ENR|=1<<2;	//GPIOA时钟使能
 	GPIOA->CRL&=0X00FFFFFF; 
	GPIOA->CRL|=0X88000000;//PA6 PA7 上拉输入  A6-CH1  A7-CH2
	
		/*TIM3左编码器接口定时器时钟配置*/
	RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
	RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST;
	RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;
	
	TIM3->ARR = 1000;																		//设定计数器自动重装值
	TIM3->PSC = 0;																		//预分频器不分频

	TIM3->CR1 |= TIM_CR1_ARPE;													//ARR预装载使能
	TIM3->CR1 &= ~TIM_CR1_CMS;													//RESET:边沿对其模式;SET:中央对齐模式
	TIM3->CR1 &= ~TIM_CR1_DIR;													//向上计数
//	TIM3->CR1 |= TIM_CR1_OPM;													//单脉冲模式
	TIM3->CR1 |= TIM_CR1_URS;														//只有计数器溢出/下溢才产生中断或DMA请求

	TIM3->SMCR |= TIM_SMCR_SMS_1;//010:编码器模式2 – 根据TI2FP2的电平,计数器在TI1FP1的边沿向上/下计数。
	/******通道1配置******/
	TIM3->CCMR1 |= TIM_CCMR1_CC1S_0;										//CC1通道被配置为输入,映射到TI2上
	TIM3->CCMR1 &= ~TIM_CCMR1_IC1PSC;										//捕获不分频
	TIM3->CCMR1 |= (TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_1);//0011: fSAMPLING=fCK_INT, N=8
	TIM3->CCER &= ~TIM_CCER_CC1P;												//上升沿捕获
//	TIM3->CCER |= TIM_CCER_CC1E;												//OC1使能
	/******通道2配置******/
	TIM3->CCMR1 |= TIM_CCMR1_CC2S_0;										//CC2通道被配置为输入,映射到TI1上
	TIM3->CCMR1 &= ~TIM_CCMR1_IC2PSC;										//捕获不分频
	TIM3->CCMR1 |= (TIM_CCMR1_IC2F_0 | TIM_CCMR1_IC2F_1);//0011: fSAMPLING=fCK_INT, N=8
	TIM3->CCER &= ~TIM_CCER_CC2P;												//上升沿捕获
//	TIM3->CCER |= TIM_CCER_CC2E;												//OC1使能
	
	TIM3->CR1 |= TIM_CR1_CEN;														//启动定时器3
}
/*
根据另一个信号的输入电平,计数器在TI1FP1和TI2FP2的边沿向上/下计数。
1、计数器在TI1和TI2有上升沿和下降沿变化
2、TI1为低时,计数器在TI2的上升沿减小,下降沿增加
   TI1为高时,计数器在TI2的上升沿增加,下降沿减小
   TI2为低时,计数器在TI1的上升沿增加,下降沿减小
   TI2为高时,计数器在TI1的上升沿减小,下降沿增加
3、注意时TIx而不是CHx
4、与	TIM3->CR1 &= ~TIM_CR1_DIR;//向上计数无关
例:TI1保持低   CNT在脉冲中会有 0 0 1 1
   TI1保持高   CNT在脉冲中会有 1 1 0 0
   TI2保持低   CNT在脉冲中会有 0 1 1 0
   TI2保持高   CNT在脉冲中会有 1 0 0 1
	TI2:   _|--|__|--|__|-
	TI1:   __|--|__|--|__|      1 2 3 4

	TI1:   _|--|__|--|__|-
	TI2:   __|--|__|--|__|      4 3 2 1

*/
void TIM3_Encoder_TI1TI2(void)
{
	RCC->APB2ENR|=1<<2;	//GPIOA时钟使能
 	GPIOA->CRL&=0X00FFFFFF; 
	GPIOA->CRL|=0X88000000;//PA6 PA7 上拉输入
		/*TIM3左编码器接口定时器时钟配置*/
	RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
	RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST;
	RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;
	
	TIM3->ARR = 1000;																		//设定计数器自动重装值
	TIM3->PSC = 0;																		//预分频器不分频

	TIM3->CR1 |= TIM_CR1_ARPE;													//ARR预装载使能
	TIM3->CR1 &= ~TIM_CR1_CMS;													//RESET:边沿对其模式;SET:中央对齐模式
	TIM3->CR1 &= ~TIM_CR1_DIR;													//向上计数
//	TIM3->CR1 |= TIM_CR1_OPM;													//单脉冲模式
	TIM3->CR1 |= TIM_CR1_URS;														//只有计数器溢出/下溢才产生中断或DMA请求

	TIM3->SMCR |= (TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1);//011:编码器模式3 – 根据另一个信号的输入电平,计数器在TI1FP1和TI2FP2的边沿向上/下计数。
	
	/******通道1配置******/
	TIM3->CCMR1 |= TIM_CCMR1_CC1S_0;										//CC1通道被配置为输入,映射到TI2上
	TIM3->CCMR1 &= ~TIM_CCMR1_IC1PSC;										//捕获不分频
	TIM3->CCMR1 |= (TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_1);//0011: fSAMPLING=fCK_INT, N=8
	TIM3->CCER &= ~TIM_CCER_CC1P;												//上升沿捕获
//	TIM3->CCER |= TIM_CCER_CC1E;												//OC1使能

	/******通道2配置******/
	TIM3->CCMR1 |= TIM_CCMR1_CC2S_0;										//CC2通道被配置为输入,映射到TI1上
	TIM3->CCMR1 &= ~TIM_CCMR1_IC2PSC;										//捕获不分频
	TIM3->CCMR1 |= (TIM_CCMR1_IC2F_0 | TIM_CCMR1_IC2F_1);//0011: fSAMPLING=fCK_INT, N=8
	TIM3->CCER &= ~TIM_CCER_CC2P;												//上升沿捕获
//	TIM3->CCER |= TIM_CCER_CC2E;												//OC1使能
	
	TIM3->CR1 |= TIM_CR1_CEN;														//启动定时器3
}






你可能感兴趣的:(工作杂谈,STM32,定时器,编码器,TIM,电机编码器)