新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32串口通信設(shè)置(查詢方式)

STM32串口通信設(shè)置(查詢方式)

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
首先是配置UART的GPIO口


void UART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure USART1_Tx as alternate 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);
}

然后是配置串口參數(shù)

void UART1_Configuration(void)
{

USART_InitTypeDef USART_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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;


USART_Init(USART1, &USART_InitStructure);


USART_Cmd(USART1, ENABLE);
}

本文引用地址:http://m.butianyuan.cn/article/201611/321209.htm

發(fā)送一個(gè)字符
[
u8 Uart1_PutChar(u8 ch)
{

USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
return ch;
}
發(fā)送一個(gè)字符串

void Uart1_PutString(u8* buf , u8 len)
{
for(u8 i="0";i {
Uart1_PutChar(*buf++);
}
}



評(píng)論


技術(shù)專區(qū)

關(guān)閉