2014-10-07 54 views
-1

當我通過另一個Discovery卡通過USART傳輸數據時,我有一個關於讀取STM32F4-Discovery卡上的RX寄存器的問題。我只想在卡2上發送數據時讀取卡1上的RX寄存器,然後我想清除它,以便讀取卡2發送的下一個數據。只有在接收數據時纔讀USART RX寄存器STM32F4-Discovery Cortex M4

什麼C代碼可以爲我做這個?我可以使用哪些標誌?一旦我閱讀了它,我如何清除RX寄存器?

回答

0

您必須使用另一個寄存器來確定RX是否有有效數據。

假設您使用ST的外設庫(您應該!),請使用USART_FLAG_RXNE(表示「RX寄存器不爲空」)標誌調用USART_GetFlagStatus()函數。

或者,當然,設置一箇中斷來運行,只要你收到一個字節。

+0

如果我有兩張通過USARTx連接的卡(Card1(主卡)和Card2(從卡)),並且使用來自我的從屬設備的USART6_SendData()發送了一些內容,它是否會自動結束到RX寄存器我的主人?這是否會觸發中斷? – Flux 2014-10-07 12:01:44

+0

@Flux是的,假設主站上的USART配置相同(相同的波特率,字節格式等等)和(當然)啓用。你可能不希望鏈接是全雙工的,但它往往讓人頭疼得更厲害。 – unwind 2014-10-07 12:04:00

+0

還有一個問題! 什麼時候輸入USARTx_IRQHandler()函數?什麼打斷讓我在那裏? Systick從內部時鐘中斷? – Flux 2014-10-07 12:48:13

0

如果您還沒有,請從ST下載STM32CubeF4。它包含了發現板的驅動程序和示例代碼。查看Examples-> UART下的UART_TwoBoards_ComIT示例項目。

說明在示例項目:

* This sample code shows how to use STM32F4xx UART HAL API to transmit 
* and receive a data buffer with a communication process based on 
* IT transfer. 
* The communication is done using 2 Boards. 
0

您可以使用此。閱讀時我使用了這個,用stm32f4zgt從RS485接收數據。

USART3初始化命令

void RS485Init(USART_TypeDef *Usart, uint32_t BaudRate, uint16_t DataBits, uint16_t Parity, uint16_t StopBits){ 

GPIO_InitTypeDef GPIO_InitStructure; 
USART_InitTypeDef USART_InitStructure; 
NVIC_InitTypeDef NVIC_InitStruct; 

// Enable clock for GPIOD 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); 
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); 
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); 
// Initialize pins as alternating function 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
GPIO_Init(GPIOD, &GPIO_InitStructure); 


USART_InitStructure.USART_BaudRate = BaudRate; 
USART_InitStructure.USART_WordLength = DataBits; 
USART_InitStructure.USART_StopBits = StopBits; 
USART_InitStructure.USART_Parity = Parity; 
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
USART_Init(USART3, &USART_InitStructure); // USART3-> Usart 

NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn; 
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStruct); 
USART_Cmd(Usart,ENABLE); 
} 

變量

char txt_buf[100]; 
char bin_buf[]={'b','i','n','a','r','y',':',0,1,2,3,4,0x30,0x31,0x32,65,66,67,13,10}; 
uint16_t tx_len=0; 
char *tx_ptr=0; 
uint8_t send_ok=1; 

緩衝和中斷命令

void Usart_SendBuf(USART_TypeDef *USART, uint16_t length, char *buff){ 
/*********** RS485 DE ve RE pins. **********/ 
GPIO_SetBits(GPIOD,GPIO_Pin_8); 
GPIO_ResetBits(GPIOD,GPIO_Pin_9); 
send_ok=0; 
tx_len = length; // sending byte count. 
// if we'll use string,we don't need this value. 
tx_ptr = buff; // using array address-->> pointer 
USART_ITConfig(USART,USART_IT_TXE,ENABLE); // TX (DR) is empty create an interrupt 
} 

中斷

void USART3_IRQHandler(void) 
{ 
if(USART_GetITStatus(USART3,USART_IT_TXE)){ 
if(--tx_len == 0){ // End of array? 
USART_ITConfig(USART3,USART_IT_TXE,DISABLE); // TX flag is unuseful 
    USART_ITConfig(USART3,USART_IT_TC,ENABLE); // when sending is ok, create an interr. 
    } 
    USART_SendData(USART3,(uint16_t) *tx_ptr); 
    tx_ptr++; 
    USART_ClearITPendingBit(USART3,USART_IT_TXE); 
} 
if(USART_GetITStatus(USART3,USART_IT_TC)){ // Last bit arrived? 
    GPIO_ResetBits(GPIOD,GPIO_Pin_8); // take the RS485 mode (take data, buf etc.) 
    GPIO_SetBits(GPIOD,GPIO_Pin_9); 
    USART_ITConfig(USART3,USART_IT_TC,DISABLE); // all datas is over. We don't need TC Flag 
    USART_ClearITPendingBit(USART3,USART_IT_TC); 
    send_ok=1; 
    } 
}