求助!stm32f407 串口配置

2019-07-21 05:17发布

初次使用stm32的串口,参照网上找到的f1的例程改了改用在f4上。可是从超级终端上看就是没有收到数据。是我配置的有问题么?用的是usart3,配置文件如下:

void USART_RCC_Configuration(void)
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}

void USART_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  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_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
}

void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStruct;
 
  USART_RCC_Configuration();
 
  USART_InitStruct.USART_BaudRate = 115200;
  USART_InitStruct.USART_StopBits = USART_StopBits_1;
  USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  USART_InitStruct.USART_Parity = USART_Parity_No;
  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
 
  USART_Init(USART3, &USART_InitStruct);
  USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
  USART_Cmd(USART3, ENABLE);
 
  USART_GPIO_Configuration();
}

主程序:
int main(void)
{
    SystemInit();
    USART_Configuration();
    printf("x0c");printf("x0c");
    printf("33[1;40;32m");
    printf(" *******************************************************************************");
    printf(" ************************ Copyright 2009-2012, ViewTool ************************");
    printf(" *************************** http://www.viewtool.com ***************************");
    printf(" ***************************** All Rights Reserved *****************************");
    printf(" *******************************************************************************");
    printf(" ");
    while(1)
    {
           
        if(USART_GetFlagStatus(USART3, USART_IT_RXNE) != RESET)
        {
        printf("%c",USART_ReceiveData(USART3));
        }

    }
}
还请各位指导!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
subo19920716
2019-07-22 01:44
void usart3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);  
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOC, &GPIO_InitStructure);    

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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);

printf("usart3 init ok. ");
}


int fputc(int ch, FILE *f)
{
  while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  USART_SendData(USART3, (u8) ch);
  return ch;
}   


int fgetc(FILE *f)
{
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART3);
}

一周热门 更多>