STM32-L298N-Initialization

by Marwen Maghrebi
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
// Key pins
#define KEY_PLUS_A_PIN GPIO_PIN_10
#define KEY_MINUS_A_PIN GPIO_PIN_1
#define KEY_BREAK_A_PIN GPIO_PIN_5
#define KEY_PLUS_B_PIN GPIO_PIN_2
#define KEY_MINUS_B_PIN GPIO_PIN_3
#define KEY_BREAK_B_PIN GPIO_PIN_7

// Motor Shield Pins
#define PWM_A_PIN GPIO_PIN_6
#define PWM_B_PIN GPIO_PIN_0
#define DIR_A_PIN GPIO_PIN_12
#define DIR_B_PIN GPIO_PIN_13
#define BRAKE_A_PIN GPIO_PIN_8
#define BRAKE_B_PIN GPIO_PIN_9

// PWM configuration
#define TIM_CLOCK_FREQ 16000000  // TIM3 clock frequency (assuming 72MHz system clock)
#define PWM_FREQUENCY 50        // Desired PWM frequency in Hz
#define TIM_PERIOD (TIM_CLOCK_FREQ / PWM_FREQUENCY) - 1
/* USER CODE END PD */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
// Motors Speed
int16_t speedA = 0;
int16_t speedB = 0;

// Old keys state for events triggering
uint8_t oldPlusA = 0, oldMinusA = 0, oldBreakA = 0;
uint8_t oldPlusB = 0, oldMinusB = 0, oldBreakB = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void checkKeys(void)
{
    // Keys Trigger
    uint8_t plusA = HAL_GPIO_ReadPin(GPIOA, KEY_PLUS_A_PIN) ? 0 : 1;
    if (plusA == oldPlusA)
        plusA = 0;
    else
        oldPlusA = plusA;

    uint8_t minusA = HAL_GPIO_ReadPin(GPIOA, KEY_MINUS_A_PIN) ? 0 : 1;
    if (minusA == oldMinusA)
        minusA = 0;
    else
        oldMinusA = minusA;

    uint8_t breakA = HAL_GPIO_ReadPin(GPIOA, KEY_BREAK_A_PIN) ? 0 : 1;
    if (breakA == oldBreakA)
        breakA = 0;
    else
        oldBreakA = breakA;

    uint8_t plusB = HAL_GPIO_ReadPin(GPIOA, KEY_PLUS_B_PIN) ? 0 : 1;
    if (plusB == oldPlusB)
        plusB = 0;
    else
        oldPlusB = plusB;

    uint8_t minusB = HAL_GPIO_ReadPin(GPIOA, KEY_MINUS_B_PIN) ? 0 : 1;
    if (minusB == oldMinusB)
        minusB = 0;
    else
        oldMinusB = minusB;

    uint8_t breakB = HAL_GPIO_ReadPin(GPIOA, KEY_BREAK_B_PIN) ? 0 : 1;
    if (breakB == oldBreakB)
        breakB = 0;
    else
        oldBreakB = breakB;

    // Keys Reaction
    if (plusA)
    {
        speedA += 50;
        if (speedA > 250)
            speedA = 250;
    }
    if (minusA)
    {
        speedA -= 50;
        if (speedA < -250)
            speedA = -250;
    }
    if (breakA)
        speedA = 0;
    if (plusB)
    {
        speedB += 50;
        if (speedB > 250)
            speedB = 250;
    }
    if (minusB)
    {
        speedB -= 50;
        if (speedB < -250)
            speedB = -250;
    }
    if (breakB)
        speedB = 0;
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00