全雙工是串口最為常見(jiàn)的工作方式,但在某些場(chǎng)合如AX-12數(shù)字舵機(jī)的驅(qū)動(dòng)上,我們要用到單線(xiàn)半雙工串口。半雙工的特殊之處在于它只有一根線(xiàn),收和發(fā)不能同時(shí)進(jìn)行。
下面是stm32F4的半雙工串口配置方法,與普通串口配置方法有些許的不同,這里只需要使用TXD那根線(xiàn)就可以了。關(guān)鍵之處在于需要調(diào)用庫(kù)函數(shù)USART_HalfDuplexCmd()。此外串口引腳需要配置成開(kāi)漏模式。
void USART_Half_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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;
USART_Init(USART1,&USART_InitStructure);
USART_HalfDuplexCmd(USART1, ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}