-1

当我通过 USART 从另一张 Discovery 卡传输数据时,我有一个关于读取 STM32F4-Discovery 卡上的 RX 寄存器的问题。我只想在从卡 2 传输数据时读取卡 1 上的 RX 寄存器一次,然后我想清除它以便读取卡 2 传输的下一个数据。

什么 C 代码可以为我做到这一点?我可以使用哪些标志?读取 RX 寄存器后如何清除它?

4

3 回答 3

1

您必须使用另一个寄存器来确定是否RX有有效数据。

假设您使用 ST 的外设库(您应该!),使用(这意味着“RX 寄存器不为空”)标志调用该USART_GetFlagStatus()函数。USART_FLAG_RXNE

或者,当然,设置一个中断以在您收到一个字节时运行。

于 2014-10-07T11:55:39.623 回答
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.
于 2014-10-07T12:05:58.820 回答
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;
  }
}
于 2017-01-26T10:41:50.343 回答