初级会员

- 积分
- 55
- 金钱
- 55
- 注册时间
- 2018-7-30
- 在线时间
- 9 小时
|
使用STM32F103VG,使用UART5,但是收发数据都有问题,无法接收,也无法发送数据,不知道为什么。哪位大哥能够帮我?
代码如下:
int fputc(int ch,FILE *p)
{
USART_SendData(UART5,(u8)ch);
while(USART_GetFlagStatus(UART5,USART_FLAG_TXE)==RESET);
return ch;
}
void USART5_Card_init(u32 rate)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStruct);
USART_InitStructure.USART_BaudRate=rate;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(UART5,&USART_InitStructure);
USART_Cmd(UART5,ENABLE);
USART_ClearFlag(UART5,USART_FLAG_TC);
USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART5_IRQHandler(void)
{
u32 r;
if(USART_GetITStatus(UART5,USART_IT_RXNE)==1)
{
r=USART_ReceiveData(UART5);
USART_SendData(UART5,r);
while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=1)
;
}
USART_ClearFlag(UART5,USART_FLAG_TC);
}
|
|