(2)修改main函数,
int main(void)
{
/* USER CODE BEGIN 1 */
int16_t Angle;
char TmpArray[10];
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Angle = (int16_t)(__HAL_TIM_GET_COUNTER(&htim1));//获取定时器的值,因为在配置的时候做了分频处理,所以这边角度直接等于COUNTER的值。这里做了一下强制转换,因为用到了负角度。
sprintf(TmpArray, "%4d", Angle);//
SetDispSubject(DISP_PART_RIGHT, TmpArray);//显示函数,具体代码就不放上来了,大家可以根据自己的方式实现。
}
/* USER CODE END 3 */
}
|