반응형
Notice
Recent Posts
Recent Comments
Link
Brise
STM32F411RE-nucleo UART 데이터 중계 예제 본문
반응형
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <stdio.h> #include "stm32f4xx.h" void UART2_init(){ 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); } void UART1_init(){ GPIO_InitTypeDef GPIO_InitStruct; GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; 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_PinSource10, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, 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(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } int main() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); UART1_init(); UART2_init(); while(1) { FlagStatus fStatus; fStatus = USART_GetFlagStatus(USART2, USART_FLAG_RXNE); /* Recv 확인 */ if(fStatus == SET ) { /* 있는 경우 */ uint16_t rData = USART_ReceiveData(USART2); /* 데이터를 받는다 */ printf("%c",rData); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==0); /* TX가 가능할 때까지 blocking*/ USART_SendData(USART1, rData); /* 데이터를 보낸다. */ } fStatus = USART_GetFlagStatus(USART1, USART_FLAG_RXNE); /* Recv 확인 */ if(fStatus == SET ) { /* 있는 경우 */ uint16_t rData = USART_ReceiveData(USART1); /* 데이터를 받는다 */ printf("%c",rData); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==0); /* TX가 가능할 때까지 blocking*/ USART_SendData(USART2, rData); /* 데이터를 보낸다. */ } } } | cs |
Uart2는 st-link의 vcp를 통하여 컴퓨터와 연결되고
Uart1은 핀을 통하여 외부 모듈과 연결할 수 있습니다.
stm칩의 경우 5v tolerant하지만, 출력 자체는 3.3v이기 때문에 외부 통신 전압에 따라 로직 컨버터를 고려할 필요가 있습니다.
반응형
'MCU' 카테고리의 다른 글
HAL SPI DMA 코드 (0) | 2017.12.24 |
---|---|
STM32F401RE-nucleo Standard Peripheral ADC (0) | 2017.12.07 |
STM32F401RE-nucleo CMSIS ADC (0) | 2017.12.07 |
STM32F411RE-nucleo Printf 예제 (0) | 2017.12.05 |
STM32F411RE-nucleo Standard Peripheral UART 예제 (0) | 2017.12.05 |
STM32F401RE-nucleo CMSIS UART 예제 (0) | 2017.12.05 |
STM32F429-DISC1 디스커버리 보드 BSP와 CubeMX 연동하여 사용하기 (0) | 2017.11.11 |
Comments