353
void updateMotors(void) { // Speed __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, (uint32_t)(abs(speedA) * TIM_PERIOD / 255)); __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, (uint32_t)(abs(speedB) * TIM_PERIOD / 255)); // Direction HAL_GPIO_WritePin(GPIOB, DIR_A_PIN, (speedA > 0) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOB, DIR_B_PIN, (speedB > 0) ? GPIO_PIN_SET : GPIO_PIN_RESET); // Brake On/Off HAL_GPIO_WritePin(GPIOA, BRAKE_A_PIN, (speedA == 0) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, BRAKE_B_PIN, (speedB == 0) ? GPIO_PIN_SET : GPIO_PIN_RESET); } void printSpeeds(void) { char buffer[50]; int len = sprintf(buffer, "Speed A = %d B = %d\r\n", speedA, speedB); HAL_UART_Transmit(&huart1, (uint8_t*)buffer, len, HAL_MAX_DELAY); } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ 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(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM3_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ // Start PWM. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ int16_t oldSpeedA = speedA; int16_t oldSpeedB = speedB; checkKeys(); updateMotors(); if (speedA != oldSpeedA || speedB != oldSpeedB) { printSpeeds(); } HAL_Delay(10); // Small delay to prevent too rapid updates } /* USER CODE END 3 */ }