《電子技術應用》
您所在的位置:首頁 > 通信與網(wǎng)絡 > 設計應用 > STM32串口多機通信
STM32串口多機通信
摘要: STM32 的UART數(shù)據(jù)寄存器是9位,數(shù)據(jù)傳輸先傳低位(LSB)--實際應用發(fā)現(xiàn)9位數(shù)據(jù)大時候有丟包錯包問題??利用USART可以進行多機處理器通信,其原理就是使從機處于靜默模式,由主機在需要的時候發(fā)送指令喚醒從機,并傳輸數(shù)據(jù)。STM
關鍵詞: STM32 UART 多機通信
Abstract:
Key words :

 STM32UART數(shù)據(jù)寄存器是9位,數(shù)據(jù)傳輸先傳低位(LSB)

--實際應用發(fā)現(xiàn)9位數(shù)據(jù)大時候有丟包錯包問題??

利用USART可以進行多機處理器通信,其原理就是使從機處于靜默模式,由主機在需要的時候發(fā)送指令喚醒從機,并傳輸數(shù)據(jù)。STM32靜默模式特點:1、所有接收狀態(tài)位都不會被設置;2、所有的接收中斷都被禁止;3、USART_CR1寄存器中的RWU位被置1,RUW可以硬件自動控制或者在某些條件下由軟件寫。

連接方法很簡單,主機的TX輸出與從機的RX端口直接相連,從機TX端口要經(jīng)過與門與主機RX端口連接。

多機通信方式有2種:空閑幀喚醒和地址喚醒。

空閑幀喚醒可以同時喚醒所有從機,在從機處于靜默模式時發(fā)送空閑幀(即所有位均為1的數(shù)據(jù)),喚醒多個從機,實現(xiàn)多個從機同步。

地址喚醒可以喚醒單個從機,從機靜默時發(fā)送地址幀,從機自動對比地址,地址配對正確則該從機喚醒,否則繼續(xù)進入靜默。這樣只有被尋址者才被激活,來接收數(shù)據(jù),減少由未被尋址的接收機器參與帶來的多余的USART服務開銷。這種模式下,MSB為1的字節(jié)被認為是地址,否則被認為是數(shù)據(jù)(MSB一般為數(shù)據(jù)傳送的最高位,8位傳送則MSB為第八位;9位傳送則MSB為第九位)。在一個地址字節(jié)中,目標接收者的地址放在低4位。這4位會被接收器拿來和設置在USART_CR2寄存器中ADD位中的自身地址比較。當接收到一個和設置地址相匹配的地址字符時,RWU被清除,后面的字節(jié)將正常接收。因為RWU位已經(jīng)被清除,RXEN位會因為接收到地址符被置1。當從機再次接收到地址符,如若地址不匹配則從機再次進入靜默模式。

程序?qū)嶋H編寫時可以注意以下3部分的設置:

一、設置靜默模式:

函數(shù)原形

void USART_WakeUpConfig(USART_TypeDef* USARTx, u16 USART_WakeUp)

其中USART_WakeUp:USART的喚醒方式:

1、USART_WakeUp_IdleLine 空閑總線喚醒

2、USART_WakeUp_AddressMark 地址標記喚醒

二、地址喚醒中地址設置:

函數(shù)原形

void USART_SetAddress(USART_TypeDef* USARTx, u8 USART_Address)

其中USART_Address:是設置USART 節(jié)點的地址。

例如:USART_SetAddress(USART2, 0x5);

三、是否進入靜默:

函數(shù)原形

void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState Newstate)

其中NewState: USART 靜默模式的新狀態(tài)這個參數(shù)可以?。篍NABLE或者DISABLE。

Address mark detection (WAKE=1)

In this mode, bytes are recognized as addresses if their MSB is a ‘1 else they are

considered as data. In an address byte, the address of the targeted receiver is put on the 4

LSB. This 4-bit word is compared by the receiver with its own address which is programmed

in the ADD bits in the USART_CR2 register.

The USART enters mute mode when an address character is received which does not

match its programmed address. In this case, the RWU bit is set by hardware. The RXNE flag

is not set for this address byte and no interrupt nor DMA request is issued as the USART

would have entered mute mode.

It exits from mute mode when an address character is received which matches the

programmed address. Then the RWU bit is cleared and subsequent bytes are received

normally. The RXNE bit is set for the address character since the RWU bit has been cleared.

The RWU bit can be written to as 0 or 1 when the receiver buffer contains no data (RXNE=0

in the USART_SR register). Otherwise the write attempt is ignored.

Bit 1 RWU: Receiver wakeup

This bit determines if the USART is in mute mode or not. It is set and cleared by software

and can be cleared by hardware when a wakeup sequence is recognized.

0: Receiver in active mode

1: Receiver in mute mode

Note: 1: Before selecting Mute mode (by setting the RWU bit) the USART must first receive a

data byte, otherwise it cannot function in Mute mode with wakeup by Idle line detection.

2: In Address Mark Detection wakeup configuration (WAKE bit=1) the RWU bit cannot

be modified by software while the RXNE bit is set.

Bit 11 WAKE: Wakeup method

This bit determines the USART wakeup method, it is set or cleared by software.

0: Idle Line

1: Address Mark

“logically ANDed ” 指的是邏輯上的關系。某些情況下,不需要另外加與門電路。

比如:板上有3個STM32,1主2從,中間未加電平變換芯片(直連),從機的發(fā)送腳都配置成open drain輸出并帶上拉,這種情況下2個從STM32芯片的TX腳可以 直接連到主STM32芯片的RX腳。

如果用了電平變換芯片比如MAX232,一般需要另外加與門電路。

此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權禁止轉(zhuǎn)載。