📄 init_usart.h
字号:
#ifndef _INIT_USART_
#define _INIT_USART_
//#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf???? set to 'Yes') calls __io_putchar() */
// #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
//#else
// #define PCHAR_PROTOTYPE int fputc(int ch, FILE *f)
//#endif /* __GNUC__ UT*/
/*******************************************************************************
* 函 数 名 : USART_Configuration
* 函数功能 : 对USART1进行初始化
*******************************************************************************/
void Init_USART(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //配置串口相关引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 configuration ------------------------------------------------------*/
/* USART1 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 Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitStructure.USART_BaudRate = 115200; //设置串口波特率为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; //禁止硬件流控制,禁止RTS和CTS信号
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //允许接收和发送
/*
USART_InitStructure.USART_Clock = USART_Clock_Disable; //串口时钟低电平活动
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
*/
USART_Init(USART1, &USART_InitStructure); //初始化USART1
//USART_ClearFlag(USART1, USART_FLAG_TC); //清除发送完成标志位
USART_Cmd(USART1, ENABLE); //使能USART1
}
/*******************************************************************************
* Function Name : fputc
* Description : Retargets the C library printf function to the USART.
*******************************************************************************/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch); //发送一字节数据
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //等待发送完成
return ch;
}
/*******************************************************************************
* 函 数 名 : SendString
* 函数功能 : 字符串发送
*******************************************************************************/
//void SendString(unsigned char *s)
//{
// while(*s != '\0')// \0 表示字符串结束标志,通过检测是否字符串末尾
// {
// USART_SendData(USART1, *s); //发送一个字节
// while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //等待发送完成
// s++;
// }
//}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -