利用TIM编码器接口可以直接驱动EC11编码器,简化个人编码量。
1、设置TIM3为Encoder Mode
2、设置TIM3输入GPIO PA6 PA7上拉
3、生成代码:
static void MX_TIM3_Init(void)
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_Encoder_InitTypeDef sConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
sConfig.IC1Filter = 16;
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
sConfig.IC2Filter = 16;
if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
4、main函数中:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
int CaptureNumber = 0;
char temp[8];
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
LCD_DB0_CLR;
LCD_DB1_CLR;
LCD_DB2_CLR;
LCD_DB3_CLR;
LCD_DB4_CLR;
LCD_DB5_CLR;
LCD_DB6_CLR;
LCD_DB7_CLR;
InitLCD();
LCD_LED_SET;
clrScr(VGA_RED);
__HAL_TIM_GET_COUNTER(&htim3) = 0; //计数器值置位
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
GUI_WriteASCII(30,30,"Encoder",VGA_WHITE,VGA_RED);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(__HAL_TIM_GET_COUNTER(&htim3) != 0) {
char Dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3); //读取转动方向:0为正、1为负
if(Dir)CaptureNumber--;
else CaptureNumber++;
__HAL_TIM_GET_COUNTER(&htim3) = 0; //计数器值重新置位
sprintf(temp,"%03d",CaptureNumber);
GUI_WriteASCII64x32(35,60,temp,VGA_WHITE,VGA_RED);
}
}
/* USER CODE END 3 */
}
设置编码器初值,并启动TIM3
__HAL_TIM_GET_COUNTER(&htim3) = 0; //计数器值置位
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
轮询方式读取编码值,显示在LCD上
if(__HAL_TIM_GET_COUNTER(&htim3) != 0) {
char Dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3); //读取转动方向:0为正、1为负
if(Dir)CaptureNumber--;
else CaptureNumber++;
__HAL_TIM_GET_COUNTER(&htim3) = 0; //计数器值重新置位
sprintf(temp,"%03d",CaptureNumber);
GUI_WriteASCII64x32(35,60,temp,VGA_WHITE,VGA_RED);
}
5、效果
|