==================================Outline====================================
USART
USART를 활용한 LED ON/OFF
----------------------------------------------------------------------------
USART

- 싱크로모터 AC모터의 종류 배에 들어가는 풍향 풍속계에 사용 비쌈 사용자가 제어할 부분이 없다.
- 비동기 통신: asynchronous communication
- 통신 방식은 단방향, 양방향. 단방향의 예는 라디오. 양방향은 다시 반이중(half duplex), 전이중(full duplex)방식, 반이중은 무전기, 전이중은 전화기
- Baud rate가 4.5Mbits 이것을 바이트로 나누면 562KByte이다. 속지 말자
** MAX232
- ATmega보다 4배 빠르다. 클럭의 차이에서 온다.
- 송수신 비트가 나눠져 있다.
- 그 외의 내용은 훑어본다.
USART 코딩을 해보자. Keil 편집기를 실행하고 새 프로젝트를 만든다. 실행환경에서 'USART'를 추가적으로 체크해 주어야 한다.

//RCC = Reset and clock control


[USART 블록 다이어그램]
- RTS, CTS핀은 많이 사용되진 않는다. RS485제어에 사용된다. 반이중 통신에 사용된다.
- USART_BRR은 Baud Rate 설정에 사용되는데 firmware library의 레퍼런스를 사용하면 간단하다.
- 직렬통신 시 통신 방법은 두 가지로 나뉜다. 폴링과 인터럽트이다. 수신시에는 인터럽트 방식을 발신시에는 폴링 방식을 많이 사용한다. 데이터 수신 시 프로그램은 입력이 있을 때까지 대기하기 때문에 인터럽트 방식을 사용하는 것이 좋다.
USART를 활용한 LED On/Off
하이퍼터미널에서 ‘+’ 키를 누르면 LED(PA0 떠는 PC12)가 켜지고 ‘-’키를 누르면 LED가 꺼지도록 코딩해보자.
펌웨어 라이브러리에 존재하는 함수를 사용하여 USART를 설정하고 송/수신 함수를 만들어보자.



//펌웨어 라이브러리에 제시된 예제를 용도에 맞게 수정하면 편하다.
]
'USART_GetFlagStatus' 함수를 사용하여 송/수신 함수를 만들 것이다. 송/수신 상태를 확인하는 플래그의 상태를 확인한 후 데이터가 존재하지 않을 때 ‘while'을 사용하여 프로그램을 잡아주면 된다.

USART를 설정 후 설정된 USART채널을 사용할 수 있도록 만들어주어야 한다.


/*** 코드 ***/
#include <stm32f10x.h>
void Clocks(void) { /* Enable GPIOA, USART1 clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE); return; }
void USART1_init(void) { /* The following example illustrates how to configure the USART1 */ USART_InitTypeDef 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_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStructure); /* Enables the USART1 transmit interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); return; }
void PA9_AF_out() { /* Configure GPIO9 Input Floating mode */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); return; }
void PA10_in() { /* Configure GPIO9 Input Floating mode */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); return; }
void PA0_out() { /* Configure GPIO9 Input Floating mode */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); return; }
void Tx_Data(unsigned char Tx) { /* chekcing status of Tx buffer */ while( 0 == USART_GetFlagStatus(USART1, USART_FLAG_TXE) ); /* Send one HalfWord on USART1 */ USART_SendData(USART1, Tx); return; } unsigned char Rx_Data() { unsigned char uiRx; while( 0 == USART_GetFlagStatus(USART1, USART_FLAG_RXNE) ); uiRx = USART_ReceiveData(USART1); return uiRx; }
void USART1_IRQHandler() { unsigned char ucRx; if(USART_GetITStatus(USART1, USART_IT_RXNE) == +-SET ) { ucRx = Rx_Data(); switch(ucRx) { default: break; case '+': GPIOA->ODR = 0x01; break; case '-': GPIOA->ODR = 0x00; break; } } /* Clear the CTS interrupt pending bit */ USART_ClearITPendingBit(USART1, USART_IT_RXNE); return; }
void NVIC_init() { NVIC_InitTypeDef NVIC_InitStructure; /* Enable USART1 global interrupt with Preemption Priority 1 and Sub Priority as 5 */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5; NVIC_Init(&NVIC_InitStructure); return; }
int main(void) { unsigned char ucRx; Clocks(); PA9_AF_out(); PA10_in(); USART1_init(); NVIC_init(); PA0_out(); USART_Cmd(USART1, ENABLE);
while(1) { ; } return 0; }
|