在线时间1469 小时
UID123826
ST金币0
蝴蝶豆239
注册时间2009-4-22
版主
- 最后登录
- 2020-12-9
|
a0a.1 0b0c
串口,是我们日常使用最多的一部分,刚开始做电子工程师的,基本都是从这个开始的,下面的代码是我使用STM32库编写的串口输出和读取的代码。
1、串口初始化函数:void USART_Ini(USART_TypeDef* USARTx,u16 buad)
2、串口中断开启和关闭:USART_IT(USART_TypeDef* USARTx,FunctionalState NewState)
3、串口接收:u16 Getch(USART_TypeDef* USARTx)
4、串口单个字符输出:void Putch(USART_TypeDef* USARTx,u16 ch)
5、串口输出字符串:void PutStr(USART_TypeDef* USARTx,u16 *SendBuf,u16 Length)
#include "stm32f10x_lib.h"
u16 RecDateBuffer[100];
u16 RecLen;
u8 SendDateBuffer[100];
/*******************************************************************************
* Function Name : Uart_Ini
* Description : 串口初始化
* Input :
* Output : None
* Return :
*******************************************************************************/
void USART_Ini(USART_TypeDef* USARTx,u16 buad)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockIni;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_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_Mode = USART_Mode_Rx|USART_Mode_Tx; //串口模式,开始起发送和接收
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //串口硬件流
USART_ClockIni.USART_Clock = USART_Clock_Disable;
USART_ClockIni.USART_CPOL = USART_CPOL_Low;
USART_ClockIni.USART_CPHA = USART_CPHA_2Edge;
USART_ClockIni.USART_LastBit = USART_LastBit_Disable;
USART_Init(USARTx,&USART_InitStructure);
USART_ClockInit(USARTx,&USART_ClockIni);
/* Enable USART1 */
USART_Cmd(USARTx, ENABLE); //开启串口X
}
/*******************************************************************************
* Function Name : Getch
* Description : 串口中断开启或关闭
* Input : USARTx:x=串口号
NewState: ENABLE开启中断,DISABLE关闭中断
* Output : None
* Return :
*******************************************************************************/
void USART_IT(USART_TypeDef* USARTx,FunctionalState NewState)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
if(NewState==ENABLE)
{
USART_ITConfig(USARTx,USART_IT_RXNE | USART_IT_TXE,ENABLE);
}
else
{
USART_ITConfig(USARTx,USART_IT_RXNE | USART_IT_TXE,DISABLE);
}
}
/*******************************************************************************
* Function Name : Getch
* Description : 串口接收字符
* Input : USARTx:x=串口号
* Output : None
* Return :
*******************************************************************************/
u16 Getch(USART_TypeDef* USARTx)
{
u16 ch;
if (USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==SET)
{
ch=USART_ReceiveData(USARTx);
//return(ch);
}
return(ch);
}
/*******************************************************************************
* Function Name : GetStr
* Description : 接收字符串
* Input : USARTx:x=串口号
buffer:接收字符串数组
* Output : None
* Return :
*******************************************************************************/
void GetStr(USART_TypeDef* USARTx)
{
//u16 i;
while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==SET)
{
if(USART_GetFlagStatus(USARTx,USART_FLAG_ORE)==RESET)
{
if(RecLen |
|