728x90
반응형
1. Cube 설정
2. 소스
#include "main.h"
#include "stm32f1xx_hal.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_StatusTypeDef R_stat;
uint8_t UsartData[1];
while (1)
{
R_stat = HAL_UART_Receive(&huart1,UsartData,1,5000);
if(R_stat == HAL_OK) HAL_UART_Transmit(&huart1,UsartData,1,1000);
HAL_UART_Transmit(&huart1,"test",4,1000);
}
}
3. 해석
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_StatusTypeDef R_stat; //통신 상태 확인 구조체 변수
uint8_t UsartData[1]; // 데이터 받는 변수 들어오는 데이터가 아스키 코드이므로 8bit로 받으면 된다
while (1)
{
R_stat = HAL_UART_Receive(&huart1,UsartData,1,5000); //리시브상태를 R_stat로 받고 UsartData변수를 1바이트씩 5초동안 기다림
if(R_stat == HAL_OK) HAL_UART_Transmit(&huart1,UsartData,1,1000); // 리시브가 들어오면 1바이트씩 송신한다.
HAL_UART_Transmit(&huart1,"test",4,1000); //타임아웃 테스트
}
}
728x90
반응형
'공부 > STM32F1' 카테고리의 다른 글
8.[stm32f103][hal] 외부 인터럽트 (0) | 2017.09.19 |
---|---|
6.[stm32f103][hal] printf 사용하기 (0) | 2017.09.12 |
4.[stm32f103][hal] Fatal error: ST-LINK, No MCU device found (0) | 2017.09.11 |
3.[stm32f103][hal] GPIO 입출력 (0) | 2017.08.23 |
2. [stm32f103] 시작하기-stm32 Dragon (0) | 2017.08.23 |