⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user_uart.c

📁 SD卡调试所用的资料
💻 C
字号:
#include "user_uart.h"

/*******************************************************************************
* Function Name  : UART1_Configuration
* Description    : Configures the uart1 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void UART1_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  /* USART1 configured as follow:
        - BaudRate = 9600 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 USART1*/ 
  USART_Init(USART1, &USART_InitStructure);

  /* Enable USART1 Receive and Transmit interrupts */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  
  /* Enable the USART1 */
  USART_Cmd(USART1, ENABLE);  
}


/*******************************************************************************
* Function Name  : UART1_GPIO_Configuration
* Description    : Configures the uart1 GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void UART1_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  // Configure USART1_Tx as alternate function push-pull 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Configure USART1_Rx as input floating 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
}


/********************************************************
  * 功能描述:发送一个字节
  * 参数: 
        USARTx:串口设备 USART1\USART2
        Byte: 发送的字节
  * 返回值 : 无
*********************************************************/
void USART_SendByte(USART_TypeDef* USARTx, u8 Byte)
{
    USART_SendData(USARTx,Byte);
    while ( USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET );
}


/********************************************************
  * 功能描述:发送缓冲函数
  * 参数: 
        USARTx:串口设备 USART1\USART2
        _buf: 发送缓冲首地址
        buflen:缓冲长度,未知长度用0xffff
  * 返回值 : 无
*********************************************************/
void USART_SendBuffer(USART_TypeDef* USARTx, u8* _buf, u16 buflen)
{
  u16 i=0;
  while ( (*_buf!=0 || buflen<MaxBufLen) && (i<buflen) )
  {
    USART_SendByte(USARTx,*_buf);
    _buf++;
    i++;
  }    
}




/*****************************************************************************
*
*                为了配合系统中旧的串口函数,定义以下串口发送函数
*   Uart1_PutString、uart_puts、Uart1_PutChar、uart_putdw_dec
*****************************************************************************/

void Uart1_PutString(u8* Str)
{
  USART_SendBuffer(USART1,Str,MaxBufLen);  
}
void uart_puts(char* Str)
{
  USART_SendBuffer(USART1,(u8*)Str,MaxBufLen);
}
void Uart1_PutChar(u8 ch)
{
  USART_SendByte(USART1,ch); 
}

void uart_putdw_dec(u32 D)
{
  u8 Str[8];
  Int32ToStr_Hex(D,Str);
  USART_SendBuffer(USART1,"0x",2);
  USART_SendBuffer(USART1,Str,8);
  USART_SendBuffer(USART1," Bytes",MaxBufLen);
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -