Untitled
unknown
plain_text
a year ago
7.4 kB
13
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 includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim7;
TIM_HandleTypeDef htim13;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
/* 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_USB_OTG_HS_USB_Init(void);
static void MX_TIM6_Init(void);
static void MX_TIM7_Init(void);
static void MX_TIM13_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t tx1[] = "Do you want to start the system?(Y or N)\r\n";
uint8_t tx2[] = "System Power On\r\n";
uint8_t tx3[] = "System is on standby mode\r\n";
uint8_t tx4[] = "Invalid command\r\n";
uint8_t tx5[] = "Is the seatbelt fastened?(Y or N)\r\n";
uint8_t tx6[] = "Seatbelt is securely fastened\r\n";
uint8_t tx7[] = "Warning: Please fasten the seatbelt immediately\r\n";
uint8_t tx8[] = "Emergency braking activated\r\n";
uint16_t speed = 0;
uint8_t c = 0;
uint8_t s = 0;
uint8_t ch = 0;
char buffer[50];
void delay(uint32_t sec){
HAL_TIM_Base_Start(&htim13);
while(__HAL_TIM_GET_COUNTER(&htim13) <= 2000 * sec);
HAL_TIM_Base_Stop(&htim13);
__HAL_TIM_SET_COUNTER(&htim13, 0);
}
/* 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();
/* 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_USART3_UART_Init();
MX_USB_OTG_HS_USB_Init();
MX_TIM6_Init();
MX_TIM7_Init();
MX_TIM13_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_Base_Start_IT(&htim7);
HAL_UART_Transmit(&huart3, tx1, sizeof(tx1), HAL_MAX_DELAY);
if(HAL_UART_Receive(&huart3, &ch, 1, 100) == HAL_OK){
if(ch == 'Y'){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_UART_Transmit(&huart3, tx2, sizeof(tx2), HAL_MAX_DELAY);
}
else if(ch == 'N'){
HAL_UART_Transmit(&huart3, tx3, sizeof(tx3), HAL_MAX_DELAY);
}
else{
HAL_UART_Transmit(&huart3, tx4, sizeof(tx4), HAL_MAX_DELAY);
}
}
else {
HAL_UART_Transmit(&huart3, tx4, sizeof(tx4), HAL_MAX_DELAY);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
if(htim == &htim6){
if(speed == 20){
HAL_UART_Transmit(&huart3, tx5, sizeof(tx5), HAL_MAX_DELAY);
if(HAL_UART_Receive(&huart3, &c, 1, 100) == HAL_OK){
if(c == 'Y'){
HAL_UART_Transmit(&huart3, tx6, sizeof(tx6), HAL_MAX_DELAY);
}
else if(c == 'N'){
HAL_UART_Transmit(&huart3, tx7, sizeof(tx7), HAL_MAX_DELAY);
}
else{
HAL_UART_Transmit(&huart3, tx4, sizeof(tx4), HAL_MAX_DELAY);
}
}
}
speed += 4;
sprintf(buffer, "Current Speed: %d km/h\r\n", speed);
HAL_UART_Transmit(&huart3, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
delay(1);
}
if(htim == &htim7){
HAL_TIM_Base_Stop_IT(&htim6);
if(speed == 40){
HAL_UART_Transmit(&huart3, tx7, sizeof(tx7), HAL_MAX_DELAY);
speed = 0;
if(HAL_UART_Receive(&huart3, &s, 1, 100) == HAL_OK){
if(s == 'Y'){
HAL_UART_Transmit(&huart3, tx6, sizeof(tx6), HAL_MAX_DELAY);
}
else if(s == 'N'){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_UART_Transmit(&huart3, tx8, sizeof(tx8), HAL_MAX_DELAY);
}
else{
HAL_UART_Transmit(&huart3, tx4, sizeof(tx4), HAL_MAX_DELAY);
}
}
}
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/*AXI clock gating */
RCC->CKGAENR = 0xFFFFFFFF;
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 24;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 4;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK
Editor is loading...
Leave a Comment