게시글 본문내용
|
|
다음검색
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #ifndef _USART_H_ #define _USART_H_ #include "stm32f10x.h" #include <yfuns.h> //#define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */ #define RCC_APB1Periph_GPIO_USART2 RCC_APB1Periph_USART2 #define GPIO_USART2 GPIOA #define GPIO_USART2_Rx_Pin GPIO_Pin_3 #define GPIO_USART2_Tx_Pin GPIO_Pin_2 void usart2_Init(void); int usart2_Putchar(int ch); int usart2_Getchar(); #endif /*_USART_H_ */ | cs |
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 | #include "usart.h" void usart2_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Enable USART2 clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure the GPIO ports */ /* Configure USART1_Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Tx_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); //* Configure USART1_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Rx_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; 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; /* Configure the USART2 */ USART_Init(USART2, &USART_InitStructure); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE); } int usart2_Putchar(int ch) { /* Write a character to the USART */ if( ch == '\n') { USART_SendData(USART2, '\r'); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, '\n'); }else { USART_SendData(USART2, (u8) ch); } /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); return ch; } int usart2_Getchar() { int data; /* loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); /* Write a character to the USART */ data = USART_ReceiveData(USART2); return data; } | cs |
너무 늦은것 같아서 일단 작성중이지만 올립니다.
아직 책이 이해가 잘 되지않고 어떻게 사용해야할지 감이 잡히지않아 계속해서 책과 인터넷을 참고중입니다.
어제 usart통신이 되었던것은 scanf 와 printf 를 이용해서 usart1,2 를 통해 되었었는데(지금은 다시 시도해도 되지 않습니다..), 그게 정확히 통신이 된건지 확인이 되지않고 방법이 맞지않는것 같아 usart2, 3를 사용해 다시 확실히 해보려고 시도중입니다. 제가 아직 실력이 부족해서 도움이 필요할것 같습니다.
-----------------------------------------------------------------------------------------------------
수정본(인터럽트로 수신[USART2 사용])
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 | //usart.h #ifndef _USART_H_ #define _USART_H_ #include "stm32f10x.h" #include <yfuns.h> #define RCC_APB1Periph_GPIO_USART2 RCC_APB1Periph_USART2 #define GPIO_USART2 GPIOA #define GPIO_USART2_Rx_Pin GPIO_Pin_3 #define GPIO_USART2_Tx_Pin GPIO_Pin_2 #define FALSE 0 #define TRUE 1 typedef unsigned char bool; #define U2_BUFFER_SIZE 100 void USART2_Init(void); int USART2_Putchar(int ch); void USART2_Puts(char *str); int USART2_Getchar(); void USART2_Gets(char *string); void USART2_NVIC_Init(void); void u2_increase_point_value(uint32_t * data_p); bool USART2_Is_Empty(void); void USART2_EnQueue(uint16_t data); uint16_t USART2_DeQueue(void); void USART2_Interupt_Init(void); #endif /*_USART_H_ */ | cs |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | //usart.c #include "usart.h" void USART2_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Enable USART2 clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure the GPIO ports */ /* Configure USART2_Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Tx_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); //* Configure USART2_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Rx_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; 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; /* Configure the USART2 */ USART_Init(USART2, &USART_InitStructure); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE); } void USART2_Interupt_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Enable USART2 clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure the GPIO ports */ /* Configure USART2_Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Tx_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); //* Configure USART2_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_USART2_Rx_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_USART2, &GPIO_InitStructure); /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; 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; /* Configure USART_rx */ USART_Init(USART2, &USART_InitStructure); /* Enable Receive interrupts */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /* Enable the USART_rx */ USART_Cmd(USART2, ENABLE); } void USART2_NVIC_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } int USART2_Putchar(int ch) { /* Write a character to the USART */ if( ch == '\n') { USART_SendData(USART2, '\r'); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, '\n'); }else { USART_SendData(USART2, (u8) ch); } /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); return ch; } void USART2_Puts(char *str) { while(*str) USART2_Putchar(*str++); } int USART2_Getchar() { int data; /* loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); /* Write a character to the USART */ data = USART_ReceiveData(USART2); return data; } void USART2_Gets(char *string) { char c; while((c=USART2_Getchar())!='\n') { *string++=c; } *(string-1) = '\0'; } | cs |
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 | //stm32f10x_it.c (수정된부분만) /* Includes ------------------------------------------------------------------*/ #include "led.h" #include "usart.h" extern char test_rx[10]; /******************************************************************************* * Function Name : USART2_IRQHandler * Description : This function handles USART2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { led0_on(); USART2_Gets(test_rx); printf("%s\n",test_rx); led0_off(); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } } | cs |
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 | //main.c #include <stdio.h> #include "stm32f10x.h" #include "llio.h" #include "led.h" #include "key.h" #include "usart.h" #include "systick.h" char test_rx[10]; //Test int main() { char test_data; // clock initialization SystemInit(); // lov level io implementaion using USART1 llio_init(); // led port init led_init(); // key port init key_init(); // USART2 init //USART2_Init(); // USART2 interupt init (RX) USART2_NVIC_Init(); USART2_Interupt_Init(); // SystemTick timer init systick_init(); while(1) { //test_data = usart2_Getchar(); //usart2_Putchar(test_data); //delay_ms(1000); //TX //usart2_Puts("abcd1234\n"); //delay_ms(1000); //RX //usart2_Gets(test_rx); //printf("%s",test_rx); //delay_ms(500); } return 0; } | cs |
인터럽트 작동을 cortex-m3 보드 두대를 사용하여 확인완료했습니다.
|
|

첫댓글 1. usart2,3초기화함수는 분리할것-> llio.c에 있는 초기화 함수를 usart2,3버전으로만 바꾸면 됨
2. llio.c에 보면 usart1을 이용한 getchar(1문자입력), putchar(1문자출력)함수가 있잖아 그걸 usart2,3버전으로 변경만 하면 됨. 일단 한문자 입출력하는 함수만 만들어서 테스트할것.
네 알겠습니다. 수정해보도록 하겠습니다.
근데 usart3는 커넥터가 없어서 테스트가 안되겠네요
이런식으로 하면 안될까요? max3232를 꼭 거쳐야하나요?
한쪽은 ttl규격이고 한쪽은 rs232규격인데 규격이 다르면 신호크기,극성,논리값(0,1) 모두가 다른데 통신이 될리가 없지
rs232 출력은 ttl보다 전압이 높고 음전압도 있어서 바로 연결하면 탈수있는데->rs232규격을 공부해볼것
max323칩은 ttl신호와 rs232신호를 상호변환해주는 칩임, 이걸왜써야하냐하면 PC는 rs232통신을 하거든 그래서 PC랑 통신할때는 max3232같은 칩을 써야하고 보드와 보드를 바로 연결할때는 TTL신호를 바로 연결해도 됨 즉, 보드와 보드를 바로붙일때는 max3232칩이 필요없어요
usart1은 이미 쓸수있도록 되어 있으니 usart2만 살려놔도 될것같다. 2채널만있으면 나중에 테스트할때 충분하겠지요 usart1을 쓸때는 printf,scanf함수를 이용하면 되고 usart2는 니가 만들함수를 써야겠지
아... 네 알겠습니다. 실험 안해봐서 다행이네요..
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
led0_on();
USART2_Gets(test_rx);
printf("%s\n",test_rx);
led0_off();
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
-> 인터럽트는 한바이트가 도착할때마다 발생하는데 gets함수는 \n이 들어올때까지 무한루프를 도는함수잖아 저렇게 하면 인터럽트핸들러가 종료되지 않을것 같은데 종료되지 않으면 두번째 바이트부터는 수신이 안될텐데
인터럽트핸들러에서 무한루프나 printf함수같이 처리시간이 많이 걸리는 코드는 쓰면 안되구요 핸들러안에서는 수신한 한바이트만 처리하는(배열을 잡아놓고 도착한 한바이트를 꺼내서 배열에 저장하는) 코드를 작성하고 디버깅용 코드는(printf ,gets관련)는 main에서 사용 gets를 사용할려면 보내는쪽에서 \n를 보내주어야 겠지요
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
->이코드는 SystemInit()안에 있으므로 없어도 됨
usart2의 초기화와 관련된 부분만 남기고 다른장치를 건드린다던가 관련없는 코드는 지울것