Skip to content

Instantly share code, notes, and snippets.

@x225am
Created September 24, 2023 12:16
Show Gist options
  • Save x225am/4c50d2b1dd2904f995f28fb9b953cdc4 to your computer and use it in GitHub Desktop.
Save x225am/4c50d2b1dd2904f995f28fb9b953cdc4 to your computer and use it in GitHub Desktop.
cube ide stm32 code for copy uint8_t array to string and then print via serial port 2
#include "main.h"
#include <string.h>
#include <stdio.h>
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
uint8_t byteArray[] = {65, 66, 67, 68, 69}; // Sample byte array
char str[20]; // String buffer to hold the converted data
// Copy uint8_t array to a string
sprintf(str, "%c%c%c%c%c", byteArray[0], byteArray[1], byteArray[2], byteArray[3], byteArray[4]);
// Print the string via Serial Port 2
HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), HAL_MAX_DELAY);
while (1)
{
// Your main application code here
}
}
void SystemClock_Config(void)
{
// Configure the system clock
// ...
// HAL_Init() calls HAL_MspInit(), which sets up the system clock.
// If you have custom clock configurations, modify them accordingly.
}
static void MX_USART2_UART_Init(void)
{
// Configure the UART peripheral for Serial Port 2
// ...
// HAL_UART_Init() configures UART parameters, including baud rate, data bits, etc.
// If you have custom UART configurations, modify them accordingly.
}
static void MX_GPIO_Init(void)
{
// Configure GPIO pins if needed for UART
// ...
}
void Error_Handler(void)
{
while (1)
{
// Error handling code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment