Brise

STM32F411RE-nucleo Standard Peripheral UART 예제 본문

MCU

STM32F411RE-nucleo Standard Peripheral UART 예제

naudhizb 2017. 12. 5. 14:26
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stm32f4xx.h"
 
int main()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStruct;
    
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin     = GPIO_Pin_2;
    GPIO_InitStruct.GPIO_Mode     = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType     = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed     = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_PuPd     = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    
    USART_InitTypeDef USART_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    
    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate         = 9600;
    USART_InitStructure.USART_WordLength     = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits         = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    
    USART_Init(USART2, &USART_InitStructure);
    
    USART_Cmd(USART2, ENABLE);
    
    while(1) {
        FlagStatus fStatus = USART_GetFlagStatus(USART2, USART_FLAG_RXNE); /* Recv 확인 */
        if(fStatus == SET ) { /* 있는 경우 */
            uint16_t rData = USART_ReceiveData(USART2); /* 데이터를 받는다 */
            while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==0); /* TX가 가능할 때까지 blocking*/
            USART_SendData(USART2, rData); /* 데이터를 보낸다. */
        }
    }
}
 
cs


반응형
Comments