Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
6.2 kB
3
Indexable
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
#include <string.h>

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim6;
UART_HandleTypeDef huart3;

/* USER CODE BEGIN PV */
uint8_t redMsg[] = "Red LED ON for 7 seconds\r\n";
uint8_t yellowMsg[] = "Yellow LED ON for 5 seconds\r\n";
uint8_t greenMsg[] = "Green LED ON for 3 seconds\r\n";
uint8_t ledOffMsg[] = "LED OFF\r\n";
uint32_t timeCounter = 0;
uint8_t currentLED = 0;
/* USER CODE END PV */

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* 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_USART3_UART_Init();
  MX_TIM6_Init();

  /* Start the timer interrupt */
  HAL_TIM_Base_Start_IT(&htim6);

  /* Infinite loop */
  while (1)
  {
    /* Main loop code */
  }
}

/* Callback for Timer Interrupt */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim == &htim6)
  {
    timeCounter++;

    switch (currentLED)
    {
      case 0: // Red LED
        if (timeCounter <= 7)
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Red LED ON
          HAL_UART_Transmit(&huart3, redMsg, sizeof(redMsg), HAL_MAX_DELAY);
        }
        else
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Red LED OFF
          HAL_UART_Transmit(&huart3, ledOffMsg, sizeof(ledOffMsg), HAL_MAX_DELAY);
          currentLED = 1;
          timeCounter = 0;
        }
        break;

      case 1: // Yellow LED
        if (timeCounter <= 5)
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET); // Yellow LED ON
          HAL_UART_Transmit(&huart3, yellowMsg, sizeof(yellowMsg), HAL_MAX_DELAY);
        }
        else
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET); // Yellow LED OFF
          HAL_UART_Transmit(&huart3, ledOffMsg, sizeof(ledOffMsg), HAL_MAX_DELAY);
          currentLED = 2;
          timeCounter = 0;
        }
        break;

      case 2: // Green LED
        if (timeCounter <= 3)
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); // Green LED ON
          HAL_UART_Transmit(&huart3, greenMsg, sizeof(greenMsg), HAL_MAX_DELAY);
        }
        else
        {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET); // Green LED OFF
          HAL_UART_Transmit(&huart3, ledOffMsg, sizeof(ledOffMsg), HAL_MAX_DELAY);
          currentLED = 0;
          timeCounter = 0;
        }
        break;
    }
  }
}

/**
  * @brief  System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  /* System Clock Configuration Code */
}

/**
  * @brief  TIM6 Initialization Function
  * @param  None
  * @retval None
  */
static void MX_TIM6_Init(void)
{
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 40000 - 1;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 1000 - 1;  // 1 second period
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief  USART3 Initialization Function
  * @param  None
  * @retval None
  */
static void MX_USART3_UART_Init(void)
{
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pins : PB0 PB7 PB14 */
  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_7 | GPIO_PIN_14;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  __disable_irq();
  while (1)
  {
  }
}

#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
Leave a Comment