斑竹,请看一下代码, 这个测试程序,usart3 工作,uart 4 不工作。
#elif defined USE_STM3210C_EVAL
#define GPIOx GPIOC //GPIOC used for UATR4
#define RCC_APB2Periph_GPIOx RCC_APB2Periph_GPIOC
#define GPIO_TxPin GPIO_Pin_10
#define GPIO_RxPin GPIO_Pin_11
//#define GPIOx GPIOD //GPIOD used for USART3
//#define RCC_APB2Periph_GPIOx RCC_APB2Periph_GPIOD
//#define GPIO_TxPin GPIO_Pin_8
//#define GPIO_RxPin GPIO_Pin_9
#endif /* USE_STM3210B_EVAL */
void RCC_Configuration(void)
{
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();
/* Enable GPIOx and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx | RCC_APB2Periph_AFIO, ENABLE);
/* Enable USART clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);//for UART4
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);// for USART3
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//used for USART3 full remap
GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOx, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOx, &GPIO_InitStructure);
}
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* USART2 configuration ------------------------------------------------------*/
/* USART2 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control enabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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(UART4, &USART_InitStructure);
//USART_Init(USART3, &USART_InitStructure);
/* Enable the USART */
USART_Cmd(UART4, ENABLE);
//USART_Cmd(USART3, ENABLE);
/* Send a buffer from USART to hyperterminal */
while(NbrOfDataToTransfer--)
{
USART_SendData(UART4, TxBuffer[TxCounter++]);
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
//USART_SendData(USART3, TxBuffer[TxCounter++]);
//while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
/* Receive a string (Max RxBufferSize bytes) from the Hyperterminal ended by '\r' (Enter key) */
do
{
if((USART_GetFlagStatus(UART4, USART_FLAG_RXNE) != RESET)&&(RxCounter < RxBufferSize))
{
RxBuffer[RxCounter] = USART_ReceiveData(UART4);
USART_SendData(UART4, RxBuffer[RxCounter++]);
}
//if((USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)&&(RxCounter < RxBufferSize))
//{
// RxBuffer[RxCounter] = USART_ReceiveData(USART3);
// USART_SendData(USART3, RxBuffer[RxCounter++]);
//}
}while((RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize));
while (1)
{
}
}