STM32 UART串口驱动程序





文章原始地址: http://feotech.com/?p=56




示例1.通过UART1进行数据发送
UART 1 的初始化

/**
* @brief  UART1  Initialise.
* @param  None.
* @retval None.
*/
void UART1_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //UART1 选择对应UART的RCC时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //选择串口对应引脚的RCC时钟
  //UART1_TX   GPIOA_Pin_9
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOC_Pin_9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
  GPIO_Init(GPIOA, &GPIO_InitStructure);        //
  //UART1_RX    GPIOA_Pin_10
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //GPIOA_Pin_10
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  //UART1通讯参数配置
  USART_InitStructure.USART_BaudRate = 115200;            //通讯波特率
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8个数据位
  USART_InitStructure.USART_StopBits = USART_StopBits_1;    //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);
  USART_Cmd(USART1, ENABLE);
}

将数据0xBA通过UART1发送出

USART_SendData(USART1,0xBA); //Send Data 0xBA


----------
示例2.通过对UART1进行接收中断配置,收到外部数据后进入中断读取数据 
UART 1 的初始化 开启接收中断

/**

  • @brief UART1 Initialise.

  • @param None.

  • @retval None.

  • @brief Data transfer PC
    */
    void UART1_Init(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //UART1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //

    //UART1_TX GPIOA_Pin_9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOC_Pin_9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
    GPIO_Init(GPIOA, &GPIO_InitStructure); //

    //UART1_RX GPIOA_Pin_10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //GPIOA_Pin_10
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //UART1通讯参数配置
    USART_InitStructure.USART_BaudRate = 115200; //通讯波特率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8个数据位
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //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);

    //Uart1 NVIC 中断配置
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //数据接收中断使能
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(USART1, ENABLE);
    }


中断入口函数,变量rxdat用于存放接收到的1个字节数据

void USART1_IRQHandler(void)
{
  unsigned char  rxdat ;
  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    USART_ClearITPendingBit(USART1, USART_IT_RXNE); //Clear flag
    rxdat = USART_ReceiveData(USART1);
  }
}

你可能感兴趣的:(Embedded,Software)