[stm32][hal]stm32f103rct6控制996r舵機
阿新 • • 發佈:2018-11-20
配置cub
程式碼
https://github.com/qdtom/diy/tree/master/stm32/stm32f103rct6_hal_duoji2
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 */ /* 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_TIM1_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); //一定要手動開啟!!!! /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,5); // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,10); // // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,15); // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,20); // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,24); // // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,20); // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,15); //HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,10); // HAL_Delay(1000); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,5); HAL_Delay(1000); __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,20); } /* USER CODE END 3 */ }
其他用到的東西
介面:
佔空比
角度和佔空比關係:
舵機的控制一般需要一個20ms的時基脈衝,該脈衝的高電平部分一般為0.5ms~2.5ms範圍內的角度控制脈衝部分。以180度角度舵機為例,那麼對應的控制關係是這樣的:
0.5ms————–0度;
1.0ms————45度;
1.5ms————90度;
2.0ms———–135度;
2.5ms———–180度;