Demostración del codificador de rejilla STM32

alrededores

win10 、 MDK5 、 Cubo 、 STM32F071CBTx

Descarga del proyecto

GitHub: https://github.com/Shaynerain/STM32Capture

CSDN: https://download.csdn.net/download/shaynerain/12709976

Configuración de cubo

1. Primero, configure el reloj RCC normalmente (48M)

2. TIM2 establece el modo de codificador

3. Configure el parámetro TIM2, Counter Period se puede configurar en el número de línea del codificador correspondiente para configurar el valor máximo directo aquí. EncoderModer generalmente recopila los flancos ascendentes y descendentes, es decir, el modo de codificador TI1 y TI2; configure algunos filtros, si no hay filtrado, habrá influencias de ruido;

4. TIM3 está configurado en un temporizador de 10 ms, calcule cuántas veces se usa la técnica de 10 ms y luego imprima y muestre la velocidad, que se puede cambiar según sea necesario

 

Programa KEIL

1. Después de generar el archivo de acuerdo con lo anterior, abra el archivo principal y agregue inicio

 /* USER CODE BEGIN 2 */
	HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
	HAL_TIM_Base_Start_IT(&htim3);
	
  /* USER CODE END 2 */

2. Agregue la función de devolución de llamada del temporizador

int32_t cnt=0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	cnt = TIM2->CNT;
	TIM2->CNT = 0;
	//cnt = cnt /20000 * 360 /0.01 ;
	cnt = cnt * 0.3;
	printf("Line1=%d;\r\n",cnt);
}

3. Agregue soporte para printf

#include "stdio.h"
int fputc(int ch,FILE *f)
{
    uint8_t temp[1]={ch};
    HAL_UART_Transmit(&huart1,temp,1,2);
		return 0;
}

de: https://blog.csdn.net/shaynerain

Supongo que te gusta

Origin blog.csdn.net/shaynerain/article/details/108051707
Recomendado
Clasificación