0% found this document useful (0 votes)
2 views6 pages

File Code

The document contains a C program for an embedded system that initializes UART communication and handles incoming data. It receives characters via UART, stores them in a buffer, and transmits the buffer back when a newline character is detected. The program also includes functions for system clock and GPIO initialization, as well as an error handling routine.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

File Code

The document contains a C program for an embedded system that initializes UART communication and handles incoming data. It receives characters via UART, stores them in a buffer, and transmits the buffer back when a newline character is detected. The program also includes functions for system clock and GPIO initialization, as well as an error handling routine.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/* USER CODE BEGIN Header */

Code3

/**

******************************************************************************

* @file : main.c

* @brief : Main program body

******************************************************************************

*/

/* USER CODE END Header */

#include "main.h"

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

uint8_t rx;

uint8_t buffer[50];

int index = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);


int main(void)

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_USART1_UART_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

HAL_UART_Receive(&huart1, &rx, 1, HAL_MAX_DELAY);

if (rx == '\r')

// b? qua

else if (rx == '\n')

buffer[index] = '\0';
HAL_UART_Transmit(&huart1, buffer, index, 100);

HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 100);

index = 0;

else

if (index < sizeof(buffer) - 1)

buffer[index++] = rx;

else

// tràn ? reset

index = 0;

/* USER CODE END WHILE */

/**

* @brief USART1 Initialization Function

*/

static void MX_USART1_UART_Init(void)

{
[Link] = USART1;

[Link] = 115200;

[Link] = UART_WORDLENGTH_8B;

[Link] = UART_STOPBITS_1;

[Link] = UART_PARITY_NONE;

[Link] = UART_MODE_TX_RX;

[Link] = UART_HWCONTROL_NONE;

[Link] = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart1) != HAL_OK)

Error_Handler();

/**

* @brief GPIO Initialization Function

*/

static void MX_GPIO_Init(void)

__HAL_RCC_GPIOA_CLK_ENABLE();

/**

* @brief System Clock Configuration

*/
void SystemClock_Config(void)

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.[Link] = RCC_PLL_ON;

RCC_OscInitStruct.[Link] = RCC_PLLSOURCE_HSI_DIV2;

RCC_OscInitStruct.[Link] = RCC_PLL_MUL16;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

Error_Handler();

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)


{

Error_Handler();

/**

* @brief Error Handler

*/

void Error_Handler(void)

__disable_irq();

while (1)

You might also like