본문 바로가기

[ST_MICRO]/STM32F4

[STM32F4] UART 테스트

[STM32F4] UART 테스트



STM32F4 의 USART는 4채널이고 UART가 두채널 더 늘어 났다. 전송율도 두배 정도 증가된 10.5Mpbs로 쓰일곳이 많을 것 같다.
 
4 USARTs/2 UARTs (10.5 Mbit/s, ISO 7816 interface, LIN, IrDA, modemcontrol)

(아쉬운 점은 UART FIFO가 없다. 물론 DMA를 이요하여 SRAM에 저장해도 되지만 다른 MCU에 다 있는 FIFO없는것이 좀...)







STM32F4 UART 초기화 함수
void U0_Init(unsigned char baud)
{
    USART_InitTypeDef USART_InitStructure;

    USART_InitStructure.USART_BaudRate = baud_rate;
    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;
   
    //STM_EVAL_COMInit(COM1, &USART_InitStructure);
   
//   GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USART configuration */
  USART_Init(USART1, &USART_InitStructure);
   
  /* Enable USART */
  USART_Cmd(USART1, ENABLE);
}



STM32F4 UART 송수신 함수
//한문자 수신
unsigned char U0_GetByte(void)
{
 while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
 return (u8)USART1->DR;; 
}

//한문자 전송
void U0_PutByte(unsigned char Data)
{
 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
 USART_SendData(USART1, Data);
}




STM32F4 UART 테스트 예제 코드
//=============================================================================
// STM32F4 EVM Module Test Program
// by nexp76() http://nexp.tistory.com/
// [main.c]
//=============================================================================

/*
 - STM32 EVM  USART0 Test
*/

#include "system.h"
#include "serial.h"


//-----------------------------------------------------------------------------
int main(void)
{
    //System Init
 SystemInit();
 
    //LED Init
 Led1Init();
 Led1On();
 
 //Serial Init
 DebugInit(BAUD_115200);
 DebugPrint("Serial Test Program.\r\n");

 while (1)
 {
  switch(U0_GetByte())
  {
  case '0':
     Led1Off();
   DebugPrint("LED OFF\r\n");
     break;

  case '1':
   Led1On();

   DebugPrint("LED ON\r\n");   
     break;
  }  
 }
}
//-----------------------------------------------------------------------------

반응형