STM32F4_Disvcovery之串口调试

2019-07-14 15:18发布


如下是STM32F4调试的串口程序:

#include "usart2.h"
  
  

void MyUartinit(void )
{
//NVIC_Config();                 
  STM_EVAL_COMInit();      
  USART_Configuration(115200);      
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  

}  

  
  
void STM_EVAL_COMInit(void)  
{  
  GPIO_InitTypeDef GPIO_InitStructure;  
  
  /* Enable GPIO clock */  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);  
  /* Enable UART clock */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  
   
  /* Connect PXx to USARTx_Tx*/  
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);  
  /* Connect PXx to USARTx_Rx*/  
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);  
  
  /* Configure USART Tx as alternate function  */  
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  /* Configure USART Rx as alternate function  */  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
}  
  
  

void USART_Configuration(int BaudRate)  
{  
    USART_InitTypeDef USART_InitStructure;  
  
    USART_InitStructure.USART_BaudRate = BaudRate;  
    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(USART1, &USART_InitStructure); /* Configure USART1 basic and asynchronous paramters */  
    USART_Cmd(USART1, ENABLE);   /* Enable USART1 */  
}  
  

void NVIC_Config(void)  
{  
  NVIC_InitTypeDef NVIC_InitStructure;  
  
  /* Enable the USARTx Interrupt */  
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);  
}  
  
  
int fputc(int ch, FILE *f)  
{  
  /* Place your implementation of fputc here */  
  /* e.g. write a character to the USART */  
  USART_SendData(USART1, (u8) ch);  
  
  /* Loop until the end of transmission */  
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  
  {  
  }  
  
  return ch;  
}
int fgetc(FILE *f)
{
  
  while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
  return (int)USART_ReceiveData(USART2);
}

void USART1_IRQHandler(void)
{
char c;
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
{
   c = USART_ReceiveData(USART1);
   USART_SendData(USART1,c);
   USART_ClearITPendingBit(USART1,USART_IT_RXNE);
// GPIO_ToggleBits(GPIOD,GPIO_Pin_14);
}
}

void  Usart_Test(void)
{
int i=0;
for(i=0 ; i
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。