패킷 메세지 전달시 프로토콜에 맞게 문자열 출력하는 예제
사용 USART : USART2
Baud rate : 115200
Word Length : 8bits
Parity : None
Stop Bits : 1
STM32CUBEIDE 설정 창
코드 설명
UART 송신 방식 : DMA
아래 코드에서 사용된 프로토콜
stx | led | on/off | etx |
0x02 | 1 | 1/0 | 0x03 |
0x02 | 2 | 1/0 | 0x03 |
/* USER CODE BEGIN 0 */
uint8_t rx_data[100];
uint8_t data_arrived = 0;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
data_arrived = 1;
HAL_UART_Transmit(&huart2, (uint8_t*)rx_data, 4, HAL_MAX_DELAY);
HAL_UART_Receive_DMA(&huart2, (uint8_t*)rx_data, 4);
}
/* 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_DMA_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_DMA(&huart2, (uint8_t*)rx_data, 4);
while(!data_arrived);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(data_arrived){
if(rx_data[0] == '2' && rx_data[3] == '3')
{
if(rx_data[1] == '1')
{
if(rx_data[2] == '1') printf("%s\n", "LED1 ON");
else printf("%s\n", "LED1 OFF");
}else if(rx_data[1] == '2'){
if(rx_data[2] == '1') printf("%s\n", "LED2 ON");
else printf("%s\n", "LED2 OFF");
}
}
data_arrived = 0;
}
HAL_Delay(100);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
'Stm32 > 실제 구동(stm32cubeide)' 카테고리의 다른 글
[HAL Driver] 내부 플래시 메모리 읽고 쓰기 (0) | 2024.12.18 |
---|---|
ADC Multi Channel - Joy Stick (0) | 2024.07.10 |
Timer Interrupt (0) | 2024.06.18 |
UART (Polling, Interrupt , DMA) [HAL-DRIVER 이용] (0) | 2024.05.08 |
[1]GPIO 입출력 레지스터 직접 제어 (0) | 2024.05.02 |