스위치를 이용한 입력으로 시리얼 전송
시리얼 문자를 이용한 LED OnOFF
1. CUBE MX SET
2. Sorce Code
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_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_StatusTypeDef RcvStat ;
uint8_t bufftx[10] = "Hello!\n" ;
uint8_t b_in[10] = "b_in\n" ;
uint8_t UsartData[10] ;
uint8_t pin_state ;
HAL_UART_Transmit(&huart2, bufftx, 10, 100) ;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
RcvStat = HAL_UART_Receive(&huart2, UsartData, 1, 100) ;
if (RcvStat == HAL_OK) {
if (UsartData[0] == 'a') { // if serial input is 'a', led on
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET) ;
} else if (UsartData[0] == 'b') { // if serial input is 'a', led off
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET) ;
}
HAL_UART_Transmit(&huart2, UsartData, 1, 100) ;
}
pin_state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) ;
if (!pin_state) { // if b1 is low, send b_in
HAL_UART_Transmit(&huart2, b_in, 10, 100) ;
}
}
/* USER CODE END 3 */
}
SWITCH를 누르면 B_IN MSG 송신
시리얼문자 'a' 전송시 LED ON
'b'전송시 LED OFF
'Project > Nucleo-F103RB' 카테고리의 다른 글
Nucleo-F103RB Serial 통신3 (0) | 2019.05.27 |
---|---|
Nucleo-F103RB Serial 통신2 (0) | 2019.05.21 |
Nucleo-F103RB Serial 통신1 (0) | 2019.05.21 |
Nucleo-F103RB LED 켜기 (0) | 2019.05.21 |
Nucleo-F103RB 개발환경 (0) | 2019.05.21 |